创建时隐藏的表单不接收广播消息

Form that was hidden on creation does not receive broadcast messages

我创建了一个旨在 运行 单个实例的程序,其表单在收到广播消息之前是隐藏的。 错误是除非在创建时显示表单,否则不会收到消息。 为什么要在这个阶段展示形式? 我一起敲了一个例子。该程序的第一个 运行ning 实例创建表单,其他实例向它广播一条消息。

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(),以创建底层 window 以便从程序的其他实例广播消息已收到。

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