HOWTO: Disable Close Command in VB Control Menu (System Menu)ID: Q82876
|
To modify an item in the Visual Basic Control menu (also known as the System menu), you need to call the API functions GetSystemMenu and ModifyMenu. This article describes how to disable the Close command in the Control menu.
If you do not want the user to be able to choose the Close command from
the Control menu or to be able to double-click the Control-menu box to end
the application, you can disable the Close command. GetSystemMenu returns
the handle to the Control menu. That handle can be used by ModifyMenu to
change the control menu.
The following code example disables (grays out) the Close command in
the Visual Basic Control menu.
' Enter each Declare statement as one, single line:
Declare Function GetSystemMenu Lib "User" (ByVal hWnd%,
ByVal bRevert%) as Integer
Declare Function ModifyMenu Lib "User" (ByVal hMenu%, ByVal nPosition%,
ByVal wFlags%, ByVal wIDNewItem%, ByVal lpNewItem as Any) as Integer
Const MF_BYCOMMAND = &H0
Const MF_GRAYED = &H1
Const SC_CLOSE = &HF060
NOTE: Other constants to disable other menu items in the Control
menu are described in the CONSTANT.TXT file.
Sub Command1_Click ()
' See the notes at the end of this article for important additional
' information about this code.
nPosition% = SC_CLOSE
idNewItem%=-10
s$ = "Close"
hMenu% = GetSystemMenu(hWnd, 0)
wFlags% = MF_BYCOMMAND Or MF_GRAYED
success% = ModifyMenu(hMenu%, nPosition%, wFlags%, idNewItem%, s$)
End Sub
Sub Command2_Click ()
End
End Sub
ModifyMenu(hMenu, SC_CLOSE, MF_BYCOMMAND|MF_GRAYED, SC_CLOSE, "Close")
Here's the syntax for EnableMenuItem():
EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND|MF_GRAYED)
Both functions work. However it appears that Visual Basic re-enables the
menu item whose ID is SC_CLOSE. This is why it may appear as if the
ModifyMenu() or EnableMenuItem() function failed.
ModifyMenu(hMenu, SC_CLOSE, MF_BYCOMMAND|MF_GRAYED, -10, "Close")
This works because Visual Basic looks for a menu item with ID SC_CLOSE to
re-enable and cannot find one because it has been changed to 0 or -10. So
Visual Basic can't re-enable the Close menu item.
ModifyMenu(hMenu, -10, MF_BYCOMMAND|MF_GRAYED, SC_CLOSE, "Close")
This is a good workaround to Visual Basic's re-enabling of Close. Don't use
0 because menu separators also have the ID 0 and you will run into problems
when you try to re-enable Close. 0xFFFF is a good ID to use.
"Microsoft Windows Programmer's Reference Book and Online Resource"
(Visual Basic Add-on kit number 1-55615-413-5)
For information on the 32-bit equivalent, please see the following article in the Microsoft Knowledge Base:
Q184686 : HOWTO: Disable the Close Option on Control Menu of a VB Form
Additional query words: control box controlbox
Keywords : kbVBp kbVBp300
Version : WINDOWS:1.0,2.0,3.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: June 8, 1999