How to Disable Close Command in VB Control Menu (System Menu)

ID Number: Q82876

1.00

WINDOWS

Summary:

To modify an item in the Visual Basic Control menu (also referred to

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.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

If you do not want the user to be able to select the Close command

from the Control menu or to be able to double-click on the

Control-menu box to end the application, you can disable the Close

command. GetSystemMenu returns the handle to the Control menu, which

can be used by ModifyMenu to change the Control menu.

The following code example disables and grays out the Close command in

the Visual Basic Control menu.

1. Run Visual Basic, or from the File menu, choose New Project (ALT,

F, N) if Visual Basic is already running. Form1 is created by

default.

2. Place a command button (Command1) on Form1. Change its Caption

property to "Disable Close" (without the quotation marks).

3. Place another command button (Command2) on Form1. Change its

caption to "Exit".

4. Add the following declarations and constants to the general

Declarations section of Form1:

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

' Each Declare (above) must be on one line.

Const MF_BYCOMMAND = &H0

Const MF_GRAYED = &H1

Const SC_CLOSE = &HF060

Note that other constants to disable other menu items in the

Control menu are described in the CONSTANT.TXT file.

5. Add the following code to the Command1 Click event:

Sub Command1_Click ()

nPosition% = SC_CLOSE

s$ = "Close"

hMenu% = GetSystemMenu(hWnd, 0)

wFlags% = MF_BYCOMMAND Or MF_GRAYED

success% = ModifyMenu(hMenu%, nPosition%, wFlags%, 0, s$)

End Sub

6. Add the following code to the Command2 Click event:

Sub Command2_Click ()

End

End Sub

7. Press F5 to run the program.

8. Click on the Control-menu box to see that all the menu items are

available.

9. Click on the Disable Close command button, then on the Control-menu

box. Notice that the Close menu command is unavailable.

The user cannot end the application by either choosing Close from the

Control menu or by double-clicking on the Control-menu box. The only

way to end this program is to choose the Exit command button.

Reference(s):

"Microsoft Windows Programmer's Reference Book and Online Resource"

(Visual Basic Add-on kit number 1-55615-413-5)

Additional reference words: 1.00