Formの閉じるボタンを無効化するサンプルを掲載します。VB番は下記LiveSpaceのページに掲載しています。

フォームの[X]閉じるメニューを無効化する方法

確認環境

  • Windows Vista Enterprise
  • 開発環境:Visual Studio 2008 Professional
  • .NET 3.5

.NETで開発していますが、実際にはpinvokeを使っています。GetSystemMenuメソッドで、メニューのハンドルを取得して、無効化しています。

プログラムの作成

フォームプロジェクトを作成して、フォームを次のように編集します。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace DisableMaxMenu
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        // pinvokeで列挙されている定数
        // list from http://www.pinvoke.net/default.aspx/user32/EnableMenuItem.html
        internal const UInt32 MF_INSERT = 0x00000000;
        internal const UInt32 MF_CHANGE = 0x00000080;
        internal const UInt32 MF_APPEND = 0x00000100;
        internal const UInt32 MF_DELETE = 0x00000200;
        internal const UInt32 MF_REMOVE = 0x00001000;

        internal const UInt32 MF_BYCOMMAND = 0x00000000;
        internal const UInt32 MF_BYPOSITION = 0x00000400;

        internal const UInt32 MF_SEPARATOR = 0x00000800;

        internal const UInt32 MF_ENABLED = 0x00000000;
        internal const UInt32 MF_GRAYED = 0x00000001;
        internal const UInt32 MF_DISABLED = 0x00000002;

        internal const UInt32 MF_UNCHECKED = 0x00000000;
        internal const UInt32 MF_CHECKED = 0x00000008;
        internal const UInt32 MF_USECHECKBITMAPS = 0x00000200;

        internal const UInt32 MF_STRING = 0x00000000;
        internal const UInt32 MF_BITMAP = 0x00000004;
        internal const UInt32 MF_OWNERDRAW = 0x00000100;

        internal const UInt32 MF_POPUP = 0x00000010;
        internal const UInt32 MF_MENUBARBREAK = 0x00000020;
        internal const UInt32 MF_MENUBREAK = 0x00000040;

        internal const UInt32 MF_UNHILITE = 0x00000000;
        internal const UInt32 MF_HILITE = 0x00000080;

        internal const UInt32 MF_DEFAULT = 0x00001000;
        internal const UInt32 MF_SYSMENU = 0x00002000;
        internal const UInt32 MF_HELP = 0x00004000;
        internal const UInt32 MF_RIGHTJUSTIFY = 0x00004000;

        internal const UInt32 MF_MOUSESELECT = 0x00008000;
        internal const UInt32 MF_END = 0x00000080;  /* Obsolete -- only used by old RES files */

        internal const UInt32 MFT_STRING = MF_STRING;
        internal const UInt32 MFT_BITMAP = MF_BITMAP;
        internal const UInt32 MFT_MENUBARBREAK = MF_MENUBARBREAK;
        internal const UInt32 MFT_MENUBREAK = MF_MENUBREAK;
        internal const UInt32 MFT_OWNERDRAW = MF_OWNERDRAW;
        internal const UInt32 MFT_RADIOCHECK = 0x00000200;
        internal const UInt32 MFT_SEPARATOR = MF_SEPARATOR;
        internal const UInt32 MFT_RIGHTORDER = 0x00002000;
        internal const UInt32 MFT_RIGHTJUSTIFY = MF_RIGHTJUSTIFY;

        internal const UInt32 MFS_GRAYED = 0x00000003;
        internal const UInt32 MFS_DISABLED = MFS_GRAYED;
        internal const UInt32 MFS_CHECKED = MF_CHECKED;
        internal const UInt32 MFS_HILITE = MF_HILITE;
        internal const UInt32 MFS_ENABLED = MF_ENABLED;
        internal const UInt32 MFS_UNCHECKED = MF_UNCHECKED;
        internal const UInt32 MFS_UNHILITE = MF_UNHILITE;
        internal const UInt32 MFS_DEFAULT = MF_DEFAULT;

        internal const UInt32 SC_CLOSE = 0x0000F060;
        [DllImport("user32.dll")]
        static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
        [DllImport("user32.dll")]
        static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        [DllImport("user32.dll")]
        static extern int GetMenuItemCount(IntPtr hMenu);
        [DllImport("user32.dll")]
        static extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);

        private void Form1_Load(object sender, EventArgs e)
        {
            //「X」閉じるメニュー無効化
            IntPtr hMenu = GetSystemMenu(this.Handle, false);
            if (hMenu != IntPtr.Zero)
            {
                EnableMenuItem(hMenu, SC_CLOSE, MF_GRAYED);

                int menuItemCount = GetMenuItemCount(hMenu);
                RemoveMenu(hMenu, (uint)menuItemCount - 1, MF_REMOVE | MF_BYPOSITION);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

 定数はpinvole.netに記載してあった内容をそのまま使っています。

間違い等がありましたら、ご指摘ください。