Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows
Visual Basic > Other sample source codes
Preventing a form from being moved
Preventing a form from being moved 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. 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
Privacy Policy
|
Link to Us
|
Links