第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

創(chuàng)建時(shí)隱藏的表單不接收廣播消息

創(chuàng)建時(shí)隱藏的表單不接收廣播消息

C#
蝴蝶刀刀 2021-11-14 14:47:08
我創(chuàng)建了一個(gè)旨在運(yùn)行單個(gè)實(shí)例的程序,其中包含一個(gè)隱藏的表單,直到收到廣播消息。錯(cuò)誤是除非在創(chuàng)建時(shí)顯示表單,否則不會(huì)收到消息。為什么要在這個(gè)階段展示表格?我舉了一個(gè)例子。程序的第一個(gè)運(yùn)行實(shí)例創(chuàng)建表單,其他實(shí)例向它廣播消息。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);    }}
查看完整描述

1 回答

?
SMILET

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

要?jiǎng)?chuàng)建一個(gè)在收到廣播消息之前保持隱藏的窗體,從窗體的構(gòu)造函數(shù)調(diào)用 CreateHandle(),以創(chuàng)建底層窗口,以便接收來自程序的其他實(shí)例的廣播消息。


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

    }

}

}


查看完整回答
反對(duì) 回復(fù) 2021-11-14
  • 1 回答
  • 0 關(guān)注
  • 151 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)