1 const int WM_SYSCOMMAND = 0x112; 2 //窗体关闭消息 3 const int SC_CLOSE = 0xf060; 4 //窗体最小化消息 5 const int SC_MINIMIZE = 0xf020; 6 //窗体最大化消息 7 const int SC_MAXIMIZE = 0xf030; 8 //窗体还原消息 9 const int SC_NOMAL = 0xf120; 10 //窗体还原消息 11 const int SC_RESTORE = 61728; 12 13 //窗体按钮的拦截函数 14 protected override void WndProc(ref Message m) 15 { 16 17 if (m.Msg == WM_SYSCOMMAND) 18 { 19 //If m.WParam.ToInt32() = SC_RESTORE Then 20 // ‘拦截还原按钮 21 // Exit Sub 22 //End If 23 24 if (m.WParam.ToInt32() == SC_NOMAL) 25 { 26 //拦截还原按钮 27 28 return; 29 } 30 //if (m.WParam.ToInt32() == SC_MINIMIZE) 31 //{ 32 // //拦截最小化按钮 33 // //这里写操作代码 34 35 // return; 36 //} 37 if (m.WParam.ToInt32() == SC_MAXIMIZE) 38 { 39 //拦截窗体最大化按钮 40 //..................... 41 42 return; 43 } 44 45 46 //窗体关闭消息 47 if (m.WParam.ToInt32() == SC_CLOSE) 48 { 49 if (MessageBox.Show("您确认要退出吗?", "", MessageBoxButtons.OKCancel) == DialogResult.OK) 50 { 51 System.Environment.Exit(System.Environment.ExitCode); 52 } 53 else 54 { 55 return; 56 } 57 58 } 59 } 60 base.WndProc(ref m); 61 62 } 63 64 65 }
VB
1 Const WM_SYSCOMMAND As Integer = &H112 2 Const SC_CLOSE As Integer = &HF060 ‘窗体关闭消息 3 Const SC_MINIMIZE As Integer = &HF020 ‘窗体最小化消息 4 Const SC_MAXIMIZE As Integer = &HF030 ‘窗体最大化消息 5 Const SC_NOMAL As Integer = &HF120 ‘窗体还原消息 6 Const SC_RESTORE As Integer = 61728 ‘窗体还原消息 7 8 ‘窗体按钮的拦截函数 9 Protected Overrides Sub WndProc(ByRef m As Message) 10 11 If m.Msg = WM_SYSCOMMAND Then 12 ‘If m.WParam.ToInt32() = SC_RESTORE Then 13 ‘ ‘拦截还原按钮 14 ‘ Exit Sub 15 ‘End If 16 17 If m.WParam.ToInt32() = SC_NOMAL Then 18 ‘拦截还原按钮 19 20 Exit Sub 21 End If 22 If m.WParam.ToInt32() = SC_MINIMIZE Then 23 ‘拦截最小化按钮 24 ‘这里写操作代码 25 26 Exit Sub 27 End If 28 If m.WParam.ToInt32() = SC_MAXIMIZE Then 29 ‘拦截窗体最大化按钮 30 ‘..................... 31 32 Exit Sub 33 End If 34 ‘拦截双击标题栏、移动窗体的系统消息 35 If m.Msg <> &HA3 AndAlso m.Msg <> &H3 AndAlso m.WParam <> &HF012 Then 36 MyBase.WndProc(m) 37 End If 38 39 If m.WParam.ToInt32() = SC_CLOSE Then 40 41 Exit Sub 42 43 End If 44 End If 45 MyBase.WndProc(m) 46 47 End Sub
最小化、最大化、关闭按钮不显示
this.ControlBox = false; // 设置不出现关闭按钮