天天看點

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