windows程序里新窗体不在任务栏显示,无标题拖动,键盘事件,始终显示在主窗体上面,单实例运行等

时间:2024-09-20 12:35:26
  1. 不显示在任务栏,Alt+Tab也不显示
            protected override CreateParams CreateParams
            {
                get
                {
                    const int WS_EX_APPWINDOW = 0x40000;
                    const int WS_EX_TOOLWINDOW = 0x80;
                    CreateParams cp = base.CreateParams;
                    cp.ExStyle &= (~WS_EX_APPWINDOW);    // 不显示在TaskBar
                    cp.ExStyle |= WS_EX_TOOLWINDOW;      // 不显示在Alt-Tab
                    return cp;
                }
            }
    
  2. 无标题拖动
            [DllImport("user32.dll")]
            public static extern bool ReleaseCapture();
            [DllImport("user32.dll")]
            public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
            public const int SC_MOVE = 0xF010;
            public const int HTCAPTION = 0x0002;
            public const Int32 WM_SYSCOMMAND = 274;
            private void picMain_MouseDown(object sender, MouseEventArgs e)
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
    
  3. 键盘事件
            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
            {
                int WM_KEYDOWN = 256;
                int WM_SYSKEYDOWN = 260;
                if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
                {
                    switch (keyData)
                    {
                        case Keys.Escape:
                            this.WindowState = FormWindowState.Normal;
                            return false;
                        case Keys.Enter:
                            this.WindowState = FormWindowState.Maximized;
                            return false;
                        case Keys.Up:
                        case Keys.Left:
                            return false;
                        case Keys.Right:
                        case Keys.Down:
                            return false;
                    }
                }
                return base.ProcessCmdKey(ref msg, keyData);
            }
    
  4. 新建Form始终显示在主窗体上面
            [DllImport("user32 ")]
            public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
    
                Form frm = new Form();
                SetParent(frm.Handle, this.Handle);
    

    检测是否有实例

                bool createNew;
                using (System.Threading.Mutex m = new System.Threading.Mutex(true, "Global\\" + Application.ProductName, out createNew))
                {
                    if (createNew)
                    {
    /// 执行创建工作
    }
     else
                    {
                        /// 激活现有实例
                    }
                }
    
  5. 激活现有实例
            [DllImport("user32.dll")]
            public static extern bool IsIconic(IntPtr hWnd);
            //隐藏窗口,活动状态给令一个窗口
            private const int SW_HIDE = 0;
            //用原来的大小和位置显示一个窗口,同时令其进入活动状态
            private const int SW_SHOWNORMAL = 1;
            //最小化窗口,并将其激活
            private const int SW_SHOWMINIMIZED = 2;
            //最大化窗口,并将其激活
            private const int SW_SHOWMAXIMIZED = 3;
            //用最近的大小和位置显示一个窗口,同时不改变活动窗口
            private const int SW_SHOWNOACTIVATE = 4;
            //用原来的大小和位置显示一个窗口,同时令其进入活动状态
            private const int SW_RESTORE = 9;
            //根据默认 创建窗口时的样式 来显示
            private const int SW_SHOWDEFAULT = 10;
            [DllImport("User32.dll")]
            public static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
            [DllImport("User32.dll")]
            public static extern bool SetForegroundWindow(System.IntPtr hWnd);
    
            public static void RaiseOtherProcess()
            {
                Process proc = Process.GetCurrentProcess();
                Process.GetProcesses();
                foreach (Process otherProc in
                    Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName))
                {
                    if (proc.Id != otherProc.Id)
                    {
                        IntPtr hWnd = otherProc.MainWindowHandle;
                        if (IsIconic(hWnd))
                        {
                            ShowWindowAsync(hWnd, SW_RESTORE);
                        }
                        SetForegroundWindow(hWnd);
                        break;
                    }
                }
            }