[英]Form that was hidden on creation does not receive broadcast messages
我創建了一個旨在運行單個實例的程序,該程序具有隱藏的形式,直到收到廣播消息為止。 錯誤是除非創建時顯示表單,否則不會收到消息。 為什么在現階段需要顯示表格? 我一起敲了一個榜樣。 程序的第一個運行實例創建表格,其他實例向其廣播消息。
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace HiddenProgram
{
public class Program : ApplicationContext
{
[DllImport("user32")]
static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
[DllImport("user32")]
static extern int RegisterWindowMessage(string message);
const int HWND_BROADCAST = 0xffff;
public static readonly int WM_SHOWME = Program.RegisterWindowMessage("com.holroyd.Gateway.Show");
public static Program Instance;
static Mutex mutex = new Mutex(true, "{9BFB3652-CCE9-42A2-8CDE-BBC40A0F5213}");
MyForm form;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (!mutex.WaitOne(TimeSpan.Zero, true))
{
// Show the single instance window
PostMessage(
(IntPtr)HWND_BROADCAST,
WM_SHOWME,
IntPtr.Zero,
IntPtr.Zero);
}
else
{
// Create the hidden window
Instance = new Program();
Application.Run(Instance);
mutex.ReleaseMutex();
}
}
Program()
{
form = new MyForm();
form.FormClosing += form_FormClosing;
// One of the following two seem necessary to get the broadcast message
form.Show();
//MainForm = form;
}
void form_FormClosing(object sender, FormClosingEventArgs e)
{
ExitThread();
}
}
public class MyForm : Form
{
protected override void WndProc(ref Message m)
{
if (m.Msg == Program.WM_SHOWME)
Visible = !Visible;
else
base.WndProc(ref m);
}
}
}
要創建在收到廣播消息之前一直隱藏的窗體,請從窗體的構造函數中調用CreateHandle(),以便創建基礎窗口,以便接收該程序其他實例的廣播消息。
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace HiddenProgram
{
public class Program : ApplicationContext
{
[DllImport("user32")]
static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
[DllImport("user32")]
static extern int RegisterWindowMessage(string message);
const int HWND_BROADCAST = 0xffff;
public static readonly int WM_SHOWME = Program.RegisterWindowMessage("com.holroyd.Gateway.Show");
static Mutex mutex = new Mutex(true, "{9BFB3652-CCE9-42A2-8CDE-BBC40A0F5213}");
MyForm form;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (!mutex.WaitOne(TimeSpan.Zero, true))
{
// Show the single instance window
PostMessage(
(IntPtr)HWND_BROADCAST,
WM_SHOWME,
IntPtr.Zero,
IntPtr.Zero);
}
else
{
// Create the hidden window
Application.Run(new Program());
mutex.ReleaseMutex();
}
}
Program()
{
form = new MyForm();
form.FormClosing += form_FormClosing;
}
void form_FormClosing(object sender, FormClosingEventArgs e)
{
ExitThread();
}
}
public class MyForm : Form
{
public MyForm()
{
CreateHandle();
}
protected override void WndProc(ref Message m)
{
if (m.Msg == Program.WM_SHOWME)
Visible = !Visible;
else
base.WndProc(ref m);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.