VB and VBA Users Source Code: Preventing a form from being moved
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Preventing a form from being moved
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Saturday, June 08, 2002
Hits:
710
Category:
Windows API
Article:
The following code prevents a form from being moved. Option Explicit Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long 'Purpose : Prevents (or enables) a form from being moved around the screen 'Inputs : lhwndForm The handle to the form (eg. Form1.hwnd) ' [bMove] If False {default} the form can't be moved, else the form can be moved. 'Outputs : Returns True on success. 'Author : Andrew Baker 'Date : 30/05/2000 'Notes : 'Revisions : Function FormDisableMove(lhwndForm As Long, Optional bMove As Boolean = False) As Boolean Const MF_BYCOMMAND = &H0&, SC_MOVE = &HF010 Dim lhSysMenu As Long On Error GoTo ErrFailed If bMove Then 'Pass True to the bRevert argument Call GetSystemMenu(lhwndForm, True) Else lhSysMenu = GetSystemMenu(lhwndForm, False) Call DeleteMenu(lhSysMenu, SC_MOVE, MF_BYCOMMAND) End If FormDisableMove = True Exit Function ErrFailed: Debug.Print "Error in FormDisableMove: " & Err.Description Debug.Assert False FormDisableMove = False End Function 'Demostration code, add two buttons on a form - "cmdFreeze" and "cmdMove". Private Sub cmdFreeze_Click() FormDisableMove Me.hwnd, False MsgBox "The form cannot be moved..." End Sub Private Sub cmdMove_Click() FormDisableMove Me.hwnd, True MsgBox "The form can be moved..." End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder