PostMessage


J

Jon Berry

I'm having a little trouble getting PostMessage to work.

I want to ensure a single instance of my app and bring it to the foreground
if it's already running. Even though PostMessage is called, I'm not
receving
the message in WndProc.

Can anyone see what I might be doing wrong?

internal class PostIt
{
public const int HWND_BROADCAST = 0xffff;
public static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME");
[DllImport("user32")]
public static extern bool PostMessage(int hwnd, int msg, int wparam, int
lparam);
[DllImport("user32")]
public static extern int RegisterWindowMessage(string message);
}

// Post this message in Main if app is already running
PostIt.PostMessage((int)PostIt.HWND_BROADCAST,PostIt.WM_SHOWME,0,0);

// override this method in Form1
protected override void WndProc(ref Message m)
{
if (m.Msg == PostIt.WM_SHOWME)
{
MessageBox.Show("Show me!"); // test - never happens
}
base.WndProc(ref m);
}
 
Ad

Advertisements

I

Ignacio Machin ( .NET/ C# MVP )

I'm having a little trouble getting PostMessage to work.

I want to ensure a single instance of my app and bring it to the foreground
if it's already running.  Even though PostMessage is called, I'm not
receving
the message in WndProc.

Can anyone see what I might be doing wrong?

internal class PostIt
{
public const int HWND_BROADCAST = 0xffff;
public static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME");
[DllImport("user32")]
public static extern bool PostMessage(int hwnd, int msg, int wparam, int
lparam);
[DllImport("user32")]
public static extern int RegisterWindowMessage(string message);

}

// Post this message in Main if app is already running
PostIt.PostMessage((int)PostIt.HWND_BROADCAST,PostIt.WM_SHOWME,0,0);

// override this method in Form1
protected override void WndProc(ref Message m)
{
if (m.Msg == PostIt.WM_SHOWME)
{
MessageBox.Show("Show me!");  // test - never happens}

base.WndProc(ref m);

}

Hi,

I do not think that is the correct approach, what if some other
application also has defined the WM_SHOWME message (the int value)?
You should use something like a named Pipe to make sure that there is
only one instance (if you check the archives you will find plenty of
posts about this). Then you can use FindWindow and only then you post
a message to the correct window.
Broadcasting a custom message is NEVER a good idea to start with
 
J

Jon Berry

Off the top of my head, I don't see an obvious error in the code you
posted. But, you should look at the SetForegroundWindow() function
instead. For this scenario -- a process wanting to bring a
previously-started instance of the same executable to the foreground
before exiting -- it does exactly what you want.

You'll have to do a little extra work to find the window, but there are
other window management functions to do that for you.

Pete

Thanks. I'll have a look at that function.

However, I found the problem I was having with PostMessage.

If I have the Form property "ShowInTaskbar" set to False
I don't get the Message in WndProc.

Why is that?
 
Ad

Advertisements

J

Jon Berry

Because to not be shown in the taskbar means you're not a top-level
window, and window message broadcasts only get sent to top-level windows.

Pete

Okay. Thanks Pete!
 

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