C#winform让用户点击关闭按钮时不允许关闭窗体

来源:互联网 发布:南京田家炳中学知乎 编辑:程序博客网 时间:2024/05/21 10:19

         /// <summary>
        /// 用户点击关闭按钮,重写窗体接收信息处理函数
        /// </summary>
        /// <param name="msg"></param>
        protected override void WndProc(ref Message msg)
        {
            const int WM_SYSCOMMAND = 0x112;//命令操作
            const int SC_CLOSE = 0xF060;//命令类型
            if (msg.Msg == WM_SYSCOMMAND && (int)msg.WParam == SC_CLOSE)
            {
                if (MessageBox.Show("不能直接关闭该窗口,缩小到任务栏中吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {


                    //this.notifyIcon.Visible = true;
                    //this.Hide();
                    if (isSizeChanged != null)
                    {
                        isSizeChanged();
                    }
                    this.ShowInTaskbar = false;
                    this.notifyIcon.Visible = true;
                    this.Hide();

                }
                else
                {

                }
                return;
            }

            //如果不是我们要拒接的消息,我们就让窗体处理

            base.WndProc(ref msg);
        }

原创粉丝点击