Microsoft KB Archive/118376

= How to Lock a Form so it Cannot Be Moved =

Article ID: 118376

Article Last Modified on 12/9/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q118376



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:

 Start a new project in Visual Basic. Form1 is created by default. Place a command button (Command1) on Form1. Change its Caption property to Lock Form.  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.   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

 Press the F5 key to run the program. 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.</ol>

<div class="references_section">