如果另一个实例尝试打开,我使用PostMessage向我的应用程序发送消息:
(CUSTOMTEXT
替换为我的应用程序名称)NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_CUSTOMTEXT_SHOWME, IntPtr.Zero, IntPtr.Zero);
在 WndProc 中,我接收到了这条消息:
protected override void WndProc(ref Message m)
{
if (m.Msg == NativeMethods.WM_CUSTOMTEXT_SHOWME)
{
MessageBox.Show("Message received");
}
base.WndProc(ref m);
}
还有NativeMethods类:
class NativeMethods
{
public const int HWND_BROADCAST = 0xffff;
public static readonly int WM_CUSTOMTEXT_SHOWME = RegisterWindowMessage("WM_CUSTOMTEXT_SHOWME");
[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
[DllImport("user32")]
public static extern int RegisterWindowMessage(string message);
}
一切都运行良好,但当我隐藏应用程序的任务栏(this.ShowInTaskbar = false;
)时,我的应用程序停止接收该消息。
为什么呢?有没有解决方法?