How to Lock a Form so it Cannot Be Moved

ID: Q118376


The information in this article applies to:


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:

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

Additional query words: 2.00 3.00


Keywords          : 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: June 25, 1999