C# 通过消息捕获处理窗体最大化/最小化
通过以下的一些代码可以实现捕获相关的一些消息事件;
以及可以通过调用 SetCloseMenu();实现关闭一些按钮功能如屏蔽关闭按钮功能等;
需要添加命名空间:using System.Runtime.InteropServices;
private const int WM_SYSCOMMAND = 0x112; private const int MF_REMOVE = 0x1000; private const int SC_RESTORE = 0xF120; //还原 private const int SC_MOVE = 0xF010; //移动 private const int SC_SIZE = 0xF000; //大小 private const int SC_MINIMIZE = 0xF020; //最小化 private const int SC_MAXIMIZE = 0xF030; //最大化 private const int SC_CLOSE = 0xF060; //关闭 [DllImport("USER32.DLL")] public static extern int GetSystemMenu(int hwnd, int bRevert); [DllImport("USER32.DLL")] public static extern int RemoveMenu(int hMenu, int nPosition, int wFlags); private void SetCloseMenu() { int hMenu = GetSystemMenu(this.Handle.ToInt32(), 0);//移除关闭按钮 RemoveMenu(hMenu, SC_CLOSE, MF_REMOVE); } /// <summary> /// /// </summary> /// <param name="m"></param> protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_SYSCOMMAND: switch (m.WParam.ToInt32()) { case SC_MINIMIZE: base.WndProc(ref m); //捕获最小化消息 break; case SC_RESTORE: base.WndProc(ref m); //捕获还原消息 break; case SC_MAXIMIZE: base.WndProc(ref m); //捕获最大化消息 break; default: base.WndProc(ref m); break; } break; default: base.WndProc(ref m); break; } }