How to Remove Menu Items from a Form's Control-Menu Box

ID: Q110393


The information in this article applies to:


SUMMARY

The Control-menu box is located in the upper-left corner of a Visual Basic form. You can remove certain menu items from the Control-menu box by using the using form's MaxButton, MinButton, and BorderStyle properties. You can also remove Control-menu items by using Windows API functions, as shown in a sample program in the More Information section below.

To completely remove the Control-menu box, set the form's ControlBox property to False.


MORE INFORMATION

The default Control-menu box in the upper left-hand corner of a Visual Basic form contains the following nine entries, including separators:


   Restore
   Move
   Size
   Minimize
   Maximize
   -----------------------
   Close            Alt+F4
   -----------------------
   Switch to...   Ctrl+Esc 

You can remove certain menu items from the Control-menu box by using a form's MaxButton, MinButton, and BorderStyle properties:

MaxButton property:

Setting the MaxButton property to False at design time removes the Maximize item in the Control-menu box and also removes the Maximize arrow in the upper right corner of the form. Setting MaxButton to be False also prevents a double-click on the title bar from maximizing the form.

MinButton property:

Setting the MinButton property to False at design time removes the Minimize item in the Control-menu box and also removes the minimize arrow in the upper right corner of the form.

BorderStyle property:

   Setting    Description
   ----------------------------------------------------------------------
     0        No border and no related border elements.

     1        Fixed Single. Can include Control-menu box, title bar,
              Maximize button, and Minimize button. Resizable only using
              Maximize and Minimize buttons.

     2        (Default) Sizable. Resizable using any of the optional border
              elements listed for setting 1.

     3        Fixed Double. Can include Control-menu box and title bar;
              cannot include Maximize or Minimize buttons. Not resizable. 

Example Uses API Functions to Remove Control-Menu Items

The following program invokes Windows API functions to remove all items in the Control-menu box except for Restore and Minimize.
  1. Start a new project in Visual Basic. Form1 is created by default.

    NOTE: In this program, the following Form properties should be left with their design-time defaults: ControlBox = True, MaxButton = True, MinButton = True. The API functions will make the necessary changes to the form's properties.


  2. Add the following to the Form Load event code:
    
       Sub Form_Load ()
    
          Dim hSysMenu%, r%, j%, dw&, rr&
          Const MF_BYPOSITION = &H400
          ' Me refers to the form where code is currently executing:
          hSysMenu = GetSystemMenu(Me.hWnd, 0)
          For j = 8 To 4 Step -1
             r = RemoveMenu(hSysMenu, j, MF_BYPOSITION)
          Next j
          For j = 2 To 1 Step -1
             r = RemoveMenu(hSysMenu, j, MF_BYPOSITION)
          Next j
          ' Leave the Restore and Minimize items.
          dw& = GetWindowLong(Me.hWnd, -16)   'Window style
          dw& = dw& And &HFFFEFFFF    'Turn off bits for Maximize arrow button
          rr& = SetWindowLong(Me.hWnd, -16, dw&)
    
       End Sub
     
    The default Control-menu items are numbered 0 through 8 from the top down. You may remove any or all items using Windows API functions. Be sure to remove items in reverse sequence, from 8 to 0, or else the numbering will become confused.


  3. Add a command button to the form. Double-click the command button and add the following code to the Command1 click event:
    
       Sub Command1_Click ()
          End
       End Sub
     
    This button lets you end the program, since Close is removed from the Control-menu box.


  4. Add the following Declare statements to the general declarations section:
    
       ' Enter each of the following Declare statements as one, single line:
    
       Declare Function RemoveMenu% Lib "User" (ByVal hMenu%, ByVal nPosition%,
          ByVal wFlags%)
       Declare Function GetSystemMenu% Lib "User" (ByVal hWnd%, ByVal revert%)
       Declare Function GetWindowLong Lib "User" (ByVal hWnd As Integer,
          ByVal nIndex As Integer) As Long
       Declare Function SetWindowLong Lib "User" (ByVal hWnd As Integer,
          ByVal nIndex As Integer, ByVal dwNewLong As Long) As Long
     


  5. Start the program, or press the F5 key.


The form's Control menu shows Restore (grayed) and Minimize. Double- clicking the title-bar doesn't maximize the form, as desired.

Clicking the Minimize arrow or choosing the Minimize menu item minimizes the form to an icon. A single-click on that icon does not open a control- menu, unlike normal Visual Basic application icons. A double-click is required to restore the form to its full-screen state.

Creating a Form with No Title Bar

To create a Microsoft Visual Basic for Windows form with a border but with no title bar, the Caption property of a form must be set to a zero-length string; the BorderStyle property must be set to Fixed Single (1), Sizable (2) or Fixed Double; and the ControlBox, MaxButton and MinButton properties must be set to False (0).

If any text (including spaces) exists for the Caption property or if the ControlBox, MaxButton, or MinButton property is set to True, a title bar will appear on the form. Note that setting the BorderStyle property to None (0) will always make a form with no title bar.


REFERENCES

Additional query words: 3.00


Keywords          : kbcode PrgOther 
Version           : 3.00
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: June 22, 1999