方法一:重载CreateParams
        private const int CP_NOCLOSE_BUTTON = 0x200;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams mdiCp = base.CreateParams;
                mdiCp.ClassStyle = mdiCp.ClassStyle | CP_NOCLOSE_BUTTON;
                return mdiCp;
            }
        }
方法二:在窗体Load事件中调用如下CloseButtonEnable()函数
        [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 hMenu = GetSystemMenu(this.Handle, false);
            int count = GetMenuItemCount(hMenu);
            RemoveMenu(hMenu, count - 1, MF_BYPOSITION);
            RemoveMenu(hMenu, count - 2, MF_BYPOSITION);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CloseButtonEnable();
        }
方法三:设置不出现所有按钮,包括关闭按钮
        public Form1()
        {
            InitializeComponent();
            this.ControlBox = false;//设置不出现按钮
        }

方法四:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr = new DialogResult();
            dr = MessageBox.Show("关闭", "关闭", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                MessageBox.Show("关闭");
            }
            else
            {
                MessageBox.Show("不关闭");
                e.Cancel = true;
            }
        }

方法五:
Windows Style里头的第一个 ControlBox=false
posted on 2011-02-12 14:10  Berthing  阅读(438)  评论(0)    收藏  举报