How to send message to all window(include child window)

H

harvie wang

Hi,

How to send a message to every window(include child window),
I use SendMessage ,but It can't do that.

class frmA
{
public const int WM_test = 0x400 + 1;
protected override void WndProc(ref Message m)
{
// Listen for operating system messages.
switch (m.Msg)
{
case WM_test:
MessageBox.Show(WM_test.ToString()); break;
}
base.WndProc(ref m);
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
IntPtr nullhandle = IntPtr.Zero;
int i = SendMessage((int)this.Handle,WM_test,0,0);
//send ok,i == 1,popup messagebox
int i = SendMessage((int)nullhandle,WM_test,0,0);
//i == 0,do nothing
}
catch(Exception ep)
{
MessageBox.Show(ep.Message);
}
}
}

void func()
{
frmA frmTestA = new frmA();
frmA frmTestB = new frmA();
frmTestA.show();
frmTestB.show();
}
//now when i push the frmTestA button1,I want to send the WM_test msg to
frmTestA and frmTestB,how to do this?
Is there any designed patterns can implement my request?

best regarsd,

Harvie
 
N

Nicholas Paldino [.NET/C# MVP]

Harvie,

Sending a message to ALL windows in an application will be incredibly
inefficient. Rather, you should send a message to one control, which knows
the controls it has to send the message to, and then do that.

Or, since you are working in windows forms, why not just have an event
off your main form (or whatever is firing the event, or a class which acts
as a manager) and have subscribers subscribe to it as needed?

Hope this helps.
 
H

harvie wang

Hi,cheers
In frmTestA window,I create a map,when mouse move on it,I want another
frmTestB window map move mouse,so I want to implement it by sendmessage on
map_mousemove(),

I think the Observer pattern can do it,but I want to know how to implement
by use sendmessage .

thanks!

Harvie


Hello Ignacio Machin ( .NET/ C# MVP )" ignacio.machin AT dot.state.fl.us,
 
N

Nicholas Paldino [.NET/C# MVP]

Harvie,

You really should expose an event from frmTestA, and then have frmTestB
subscribe to the event. Sending a window message to every window in your
application is a HUGE waste of resources.
 
H

harvie wang

Hello Nicholas Paldino [.NET/C# MVP],
thanks:)
I do not understand your means.
would you give me a sample codes,to implement the event?

thanks very much!

Harvie
 
N

Nicholas Paldino [.NET/C# MVP]

Harvie,

It's simple. frmTestA does this:

// Expose the event.
public event EventHandler MyCustomEvent;

When you want to subscribe to the event, frmTestB does this:

// frmA is an INSTANCE of frmTestA, so you have to pass it to the instance
// of frmTestB
frmA.MyCustomEvent += new EventHandler(this.OnFormAMyCustomEvent);

OnFormAMyCustomEvent is the method you want executed when frmTestA fires
it's event.

In frmTestA, when you want to fire the event, you do this:

// Assign the event handlers to a temp variable.
EventHandler temp = this.MyCustomEvent;

// Fire if there are events.
if (temp != null)
temp(this, EventArgs.Empty);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

harvie wang said:
Hello Nicholas Paldino [.NET/C# MVP],
thanks:)
I do not understand your means.
would you give me a sample codes,to implement the event?

thanks very much!

Harvie

Harvie,

You really should expose an event from frmTestA, and then have
frmTestB subscribe to the event. Sending a window message to every
window in your application is a HUGE waste of resources.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top