子类化窗口,截获SC_CLOSE试试!!!!!!A window receives this message when the user chooses a command from the window menu (also known as the System menu or Control menu) or when the user chooses the Maximize button or Minimize button.WM_SYSCOMMAND uCmdType = wParam; // type of system command requested xPos = LOWORD(lParam); // horizontal postion, in screen coordinates yPos = HIWORD(lParam); // vertical postion, in screen coordinates ParametersuCmdTypeSpecifies the type of system command requested. This can be one of these values:Value Meaning SC_CLOSE Closes the window. SC_CONTEXTHELP Changes the cursor to a question with a pointer. If the user then clicks a control in the dialog box, the control receives a WM_HELP message. SC_DEFAULT Selects the default item; the user double-clicked the window menu. SC_HOTKEY Activates the window associated with the application-specified hot key. The low-order word of lParam identifies the window to activate. SC_HSCROLL Scrolls horizontally. SC_KEYMENU Retrieves the window menu as a result of a keystroke. SC_MAXIMIZE (or SC_ZOOM) Maximizes the window. SC_MINIMIZE (or SC_ICON) Minimizes the window. SC_MONITORPOWER Windows 95 only: Sets the state of the display. This command supports devices that have power-saving features, such as a battery-powered personal computer. SC_MOUSEMENU Retrieves the window menu as a result of a mouse click. SC_MOVE Moves the window. SC_NEXTWINDOW Moves to the next window. SC_PREVWINDOW Moves to the previous window. SC_RESTORE Restores the window to its normal position and size. SC_SCREENSAVE Executes the screen saver application specified in the [boot] section of the SYSTEM.INI file. SC_SIZE Sizes the window. SC_TASKLIST Executes or activates Windows Task Manager. SC_VSCROLL Scrolls vertically. xPosSpecifies the horizontal position of the cursor, in screen coordinates, if a window menu command is chosen with the mouse. Otherwise, the xPos parameter is not used.yPosSpecifies the vertical position of the cursor, in screen coordinates, if a window menu command is chosen with the mouse. This parameter is -1 if the command is chosen using a system accelerator, or zero if using a mnenomic. Return ValuesAn application should return zero if it processes this message.ResThe DefWindowProc function carries out the window menu request for the predefined actions specified in the previous table. In WM_SYSCOMMAND messages, the four low-order bits of the uCmdType parameter are used internally by Windows. To obtain the correct result when testing the value of uCmdType, an application must combine the value 0xFFF0 with the uCmdType value by using the bitwise AND operator. The menu items in a window menu can be modified by using the GetSystemMenu, AppendMenu, InsertMenu, ModifyMenu, InsertMenuItem, and SetMenuItem functions. Applications that modify the window menu must process WM_SYSCOMMAND messages.An application can carry out any system command at any time by passing a WM_SYSCOMMAND message to DefWindowProc. Any WM_SYSCOMMAND messages not handled by the application must be passed to DefWindowProc. Any command values added by an application must be processed by the application and cannot be passed to DefWindowProc. Accelerator keys that are defined to choose items from the window menu are translated into WM_SYSCOMMAND messages; all other accelerator keystrokes are translated into WM_COMMAND messages.
很简单啦,往下看:必须先把Form1的KeyPreview属性设置为True然后打入代码Public Key1 As Boolean Public Key2 As Boolean'先定义两个全局性变量,存储击键情况Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) ' 在KeyDown事件打入代码:If KeyCode = 18 Then Key1 = True End IfIf KeyCode = 115 Then Key2 = True End IfEnd Sub然后在Form_Unload事件打入如下代码:Private Sub Form_Unload(Cancel As Integer)
If Key1 = True And Key2 = True Then
Cancel = 1 Key1 = False Key2 = False
End If
End Sub
使用Form_QueryUnload就很简单,我有程序实例。 判断UnloadMode的目的是可以正常退出。 Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) If UnloadMode = vbFormControlMenu Then Cancel = True Else Unload Me End End If End Sub
uCmdType = wParam; // type of system command requested
xPos = LOWORD(lParam); // horizontal postion, in screen coordinates
yPos = HIWORD(lParam); // vertical postion, in screen coordinates
ParametersuCmdTypeSpecifies the type of system command requested. This can be one of these values:Value Meaning
SC_CLOSE Closes the window.
SC_CONTEXTHELP Changes the cursor to a question with a pointer. If the user then clicks a control in the dialog box, the control receives a WM_HELP message.
SC_DEFAULT Selects the default item; the user double-clicked the window menu.
SC_HOTKEY Activates the window associated with the application-specified hot key. The low-order word of lParam identifies the window to activate.
SC_HSCROLL Scrolls horizontally.
SC_KEYMENU Retrieves the window menu as a result of a keystroke.
SC_MAXIMIZE (or SC_ZOOM) Maximizes the window.
SC_MINIMIZE (or SC_ICON) Minimizes the window.
SC_MONITORPOWER Windows 95 only: Sets the state of the display. This command supports devices that have power-saving features, such as a battery-powered personal computer.
SC_MOUSEMENU Retrieves the window menu as a result of a mouse click.
SC_MOVE Moves the window.
SC_NEXTWINDOW Moves to the next window.
SC_PREVWINDOW Moves to the previous window.
SC_RESTORE Restores the window to its normal position and size.
SC_SCREENSAVE Executes the screen saver application specified in the [boot] section of the SYSTEM.INI file.
SC_SIZE Sizes the window.
SC_TASKLIST Executes or activates Windows Task Manager.
SC_VSCROLL Scrolls vertically.
xPosSpecifies the horizontal position of the cursor, in screen coordinates, if a window menu command is chosen with the mouse. Otherwise, the xPos parameter is not used.yPosSpecifies the vertical position of the cursor, in screen coordinates, if a window menu command is chosen with the mouse. This parameter is -1 if the command is chosen using a system accelerator, or zero if using a mnenomic. Return ValuesAn application should return zero if it processes this message.ResThe DefWindowProc function carries out the window menu request for the predefined actions specified in the previous table.
In WM_SYSCOMMAND messages, the four low-order bits of the uCmdType parameter are used internally by Windows. To obtain the correct result when testing the value of uCmdType, an application must combine the value 0xFFF0 with the uCmdType value by using the bitwise AND operator.
The menu items in a window menu can be modified by using the GetSystemMenu, AppendMenu, InsertMenu,
ModifyMenu, InsertMenuItem, and SetMenuItem functions. Applications that modify the window menu must process WM_SYSCOMMAND messages.An application can carry out any system command at any time by passing a WM_SYSCOMMAND message to DefWindowProc. Any WM_SYSCOMMAND messages not handled by the application must be passed to DefWindowProc. Any command values added by an application must be processed by the application and cannot be passed to DefWindowProc.
Accelerator keys that are defined to choose items from the window menu are translated into WM_SYSCOMMAND messages; all other accelerator keystrokes are translated into WM_COMMAND messages.
Public Key2 As Boolean'先定义两个全局性变量,存储击键情况Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) ' 在KeyDown事件打入代码:If KeyCode = 18 Then
Key1 = True
End IfIf KeyCode = 115 Then
Key2 = True
End IfEnd Sub然后在Form_Unload事件打入如下代码:Private Sub Form_Unload(Cancel As Integer)
If Key1 = True And Key2 = True Then
Cancel = 1
Key1 = False
Key2 = False
End If
End Sub
判断UnloadMode的目的是可以正常退出。
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormControlMenu Then
Cancel = True
Else
Unload Me
End
End If
End Sub