How to Lock a Form so it Cannot Be Moved

Last reviewed: June 21, 1995
Article ID: Q118376
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0

SUMMARY

This article shows you how to remove the Move and Size menu items from the Visual Basic Control menu (also known as the System Menu) to prevent the form from being moved or sized by the user.

MORE INFORMATION

If you do not want the user to be able to move or size your form, you can remove both or either of the Move or Size menu items from the Visual Basic control menu by using Windows API function calls. The GetSystemMenu API Function returns the handle to the Control menu. Then you can use that handle with the DeleteMenu API function to modify or delete specific menu items.

Step-by-Step Example

The following steps demonstrate how to remove the Move and Size menu items from the Visual Basic control menu:

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Place a command button (Command1) on Form1. Change its Caption property to Lock Form.

  3. Add the following declarations and constants to the general Declarations section of Form1:

       ' Enter each Declare statement as one, single line:
       Declare Function GetSystemMenu Lib "User" (ByVal hWnd%,
          ByVal bRevert%) As Integer
       Declare Function DeleteMenu Lib "user" (ByVal hMenu%, ByVal iditem%,
          ByVal wflags%) As Integer
    
       Const SC_SIZE = &HF000
       Const SC_MOVE = &HF010
       Const MF_BYCOMMAND = &H0
    
       NOTE: Other constants to remove other menu items in the Control
       menu are described in the WIN30API.TXT text file found in the
       \VB\WINAPI directory.
    
    

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

       Sub Command1_Click ()
          Dim hWnd%, hMenu%, Success%
          hWnd% = Form1.hWnd
          hMenu% = GetSystemMenu(hWnd%, 0)
          Success% = deletemenu(hMenu%, SC_SIZE, MF_BYCOMMAND)
          Success% = deletemenu(hMenu%, SC_MOVE, MF_BYCOMMAND)
       End Sub
    
    

  5. Press the F5 key to run the program.

  6. Click the command button to remove the menu items. Now attempt to move or size the form. You will not be able to move or size the form. However, you will be able to maximize it or minimize it or change its position and size in code.

REFERENCES

Another method, similar to the one presented here is presented in the following article in the Microsoft Knowledge Base:

ARTICLE-ID: Q82876

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


Additional reference words: 2.00 3.00
KBCategory: kbprg kbcode
KBSubcategory: APrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.