如何取消窗体标题栏关闭按钮

转自:http://dev.mjxy.cn/a-296.aspx

c#如何取消窗体标题栏关闭

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class Disable
{
    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr GetSystemMenu(IntPtr hwnd, bool bRevert);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern int GetMenuItemCount(IntPtr hMenu);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern int RemoveMenu(IntPtr hMenu, int uPosition, int uFlags);

    private const int MF_BYPOSITION = 0x00000400;

    public static void DisableFormSysMenuCloseButton(Form form)
    {
        IntPtr hWindow = form.Handle;
        IntPtr hMenu = GetSystemMenu(hWindow, false);
        int count = GetMenuItemCount(hMenu);
        RemoveMenu(hMenu, count - 1, MF_BYPOSITION);
        RemoveMenu(hMenu, count - 2, MF_BYPOSITION);
    }

posted @ 2011-07-12 00:47  敏捷学院  阅读(509)  评论(0编辑  收藏  举报