天天看点

C#禁用关闭按钮

界面

C#禁用关闭按钮

代码

private void OK_btn_Click(object sender, EventArgs e)//验证通过关闭窗体
        {
            string select = "SELECT * FROM log_in where username='" + Form1.name + "'and password='" + Pawss_txt.Text + "'";
            
            int count = GMManage.SqlServerRecordCount(select);//返回符合的结果数量        

            if (count > 0)
            {
                this.Close();
            }
            else
            {
                MessageBox.Show("密码错误");
            }

        }

        private void Lock_Load(object sender, EventArgs e)
        {
            CloseButtonEnable();
            //不显示在任务栏中
            this.ShowInTaskbar = false;
        }

        [DllImport("user32.dll")]
        internal static extern IntPtr GetSystemMenu(IntPtr hwnd, bool bRevert);

        [DllImport("user32.dll")]
        internal static extern int GetMenuItemCount(IntPtr hMenu);

        [DllImport("user32.dll")]
        internal static extern int RemoveMenu(IntPtr hMenu, int uPosition, int uFlags);

        ///   <summary>  
        ///   窗体的关闭按钮失效  
        ///   </summary>  
        protected void CloseButtonEnable()
        {
            //   默认窗口去除关闭按钮  
            const int MF_BYPOSITION = 0x00000400;

            IntPtr hWindow = this.Handle;
            IntPtr hMenu = GetSystemMenu(hWindow, false);
            int count = GetMenuItemCount(hMenu);
            RemoveMenu(hMenu, count - 1, MF_BYPOSITION);
            RemoveMenu(hMenu, count - 2, MF_BYPOSITION);
        }