c# 创建只接收消息的窗口

如果用自带的form,必须show后再hide,不是很好用,用以下代码即可。

 public class NotifierNativeWindow : NativeWindow
    {
        const string WindowName = "MessageOnlyWindow";
        const int WM_USER = 0x0400;
        const int WM_MYMSG = WM_USER + 1;

        public NotifierNativeWindow()
        {
            CreateParams cp = new CreateParams();
            cp.Style = 0;
            cp.ExStyle = 0;
            cp.ClassStyle = 0;
            cp.Caption = WindowName;
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                cp.Parent = (IntPtr)(-3);
            CreateHandle(cp);
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_MYMSG + 1)
            {

                return;
            }
            base.WndProc(ref m);
        }
    }


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页