SendMessage() PostMessage() equivalents

  • Thread starter Thread starter Bob Rundle
  • Start date Start date
B

Bob Rundle

I'll can't seem to find something very basic in the docs

What are the winforms equivalents of SendMessage() and PostMessage().

I can see how to create a System.Windows.Forms.Message struct. I see how to
add a message filter. I can't seem to figure out how to send or post the
message.

Regards,
Bob Rundle
 
Bob Rundle said:
I'll can't seem to find something very basic in the docs

What are the winforms equivalents of SendMessage() and PostMessage().

I can see how to create a System.Windows.Forms.Message struct. I see how to
add a message filter. I can't seem to figure out how to send or post the
message.

You usually just use Control.Invoke or Control.BeginInvoke instead.
 
Yes, thanks.

I'm not sure I can use Control.Invoke to do what I want.

Here's what I am trying to accomplish.

I have a function in the main form which update a status bar...

// Status bar update
public void OnStatusMessage(string message)
{
this.statusBar1.Text = message;
}

I would like to call this from the mdichild. Naturally I can simply cast
the child's mdi parent to the main form object and then call
OnStatusMessage(). Perhaps this is the right way of doing things.

I am simply used to (in MFC) creating a message handler in a CCmdTarget and
then crafting a WM_SETMYSTATUSBARMESSAGE message and invoking SendMessage()

Regards,

Bob Rundle
 
Bob Rundle said:
Yes, thanks.

I'm not sure I can use Control.Invoke to do what I want.

Here's what I am trying to accomplish.

I have a function in the main form which update a status bar...

// Status bar update
public void OnStatusMessage(string message)
{
this.statusBar1.Text = message;
}

I would like to call this from the mdichild. Naturally I can simply cast
the child's mdi parent to the main form object and then call
OnStatusMessage(). Perhaps this is the right way of doing things.

Yes, it is. Or you could pass in a reference to the main form when you
create the child in the first place.
I am simply used to (in MFC) creating a message handler in a
CCmdTarget and then crafting a WM_SETMYSTATUSBARMESSAGE message and
invoking SendMessage()

Fortunately, you don't have to go through all of that in .NET.
 
Fair enough...

The thing about messages however is that you don't have to know anything
about the target to send it a message. You send the target a message and it
either handles it or ignores it.

Thanks for the help.

Bob Rundle
 
The thing about messages however is that you don't have to know anything
about the target to send it a message. You send the target a message and it
either handles it or ignores it.

It is not ccompletely true. With messages you have to know the target hwnd.
In the case of .NET you can decalre and event in the sourceclass. Target can
hook up this event and in this case the sourve doesn't have to know
absolutely nothing about the class that is going to handle the event. Event
handler, though, has to do all necessary work of thread switching
(Control.Invoke) if need it
 
Let me be more precise...

You can use OnCmdMsg() with any command target. The command target has a
message map and that is the way to dispatch any sort of WM_COMMAND message.

For messages other than WM_COMMAND you need a target that is either a window
or a thread.

The point is that you don't have to know anything about them to send a
message.

In .NET if I have a method I want to call in the target I have to have
access to either the class or one of its interfaces.

In MFC you only need to know something is a CCmdTarget, you can fire (in
effect) any WM_COMMAND message at it.

Perhaps this is seen as a bad thing. No strong typing and so forth. Well I
can live without the anonymous command dispatching -- its just that I will
miss it.

Bob Rundle
 
If that's the kind of functionality you're aiming for then use .Net Events.
That way the sender doesn't need to know anything about who (if anyone) is
listening. You just raise an event and anything that is interested in
responding to it.

AndyC
 
Back
Top