How to Disable Close Command in VB Control Menu (System Menu)Last reviewed: August 8, 1996Article ID: Q82876 |
The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0- Microsoft Visual Basic programming system for Windows, version 1.0
SUMMARYTo 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.
MORE INFORMATIONIf 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.
Notes on the Use of ModifyMenu() in the CodeThe code listed above uses the ModifyMenu() function, but the EnableMenuItem() may be more appropriate in your particular situation. Here's the syntax for ModifyMenu():
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. To work around this problem in the code listed above, the second to last argument (idNewItem%) is set to -10 (0 would also work):
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. However, because of this workaround, another limitation is introduced. The problem is that the ID of the Close menu item is changed to -10. If you want the program to be able to re-enable the Close item, you'll need to use this alternative version:
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. Another alternative solution is to use DeleteMenu to remove Close and the separator above it, and use InsertMenu to add the Close and the separator.
REFERENCES"Microsoft Windows Programmer's Reference Book and Online Resource" (Visual Basic Add-on kit number 1-55615-413-5)
|
Additional reference words: 1.00 2.00 3.00 control box controlbox
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |