首页  ·  知识 ·  编程语言
阻止窗口关闭的方法
网友  博客园愚翁专栏  .NET  编辑:德仔   图片来源:网络
阻止窗口关闭的方法: 1. 使关闭按钮无效变灰 const int SC_CLOSE = 0x
阻止窗口关闭的方法:
1. 使关闭按钮无效变灰
 
const int SC_CLOSE = 0xF060;
        const int MF_BYCOMMAND = 0x0;
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern IntPtr RemoveMenu(IntPtr menuHwnd, int position, int flags);
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern IntPtr GetSystemMenu(IntPtr hwnd, int bRevert);
        public static void EnableApplicationClose(System.Windows.Forms.Form form, bool val)
        {
            IntPtr menu = GetSystemMenu(form.Handle, val ? 1 : 0);
            if (!val && menu != IntPtr.Zero)
                RemoveMenu(menu, SC_CLOSE, MF_BYCOMMAND);
            else
            {
                // needed when enabling the app close box to force it to redraw.
                form.Enabled = false;
                form.Enabled = true;
            }
        }
2. 重载onclosing事件处理函数
 
Code:
        private void frmStopClose_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Close Form?", "Remind", MessageBoxButtons.YesNo) != DialogResult.Yes)
            {
                e.Cancel = true;
            }
        }
 
本文作者:网友 来源:博客园愚翁专栏
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读