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);
}
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);
}