|
|
Title | Restrict a form so the user can only move it vertically |
Description | This example shows how to restrict a form so the user can only move it vertically in Visual Basic 6. It subclasses to watch for WM_WINDOWPOSCHANGING messages and resets the control's X coordinate. |
Keywords | form, move, vertical |
Categories | API, Controls, Tips and Tricks |
|
|
This program subclasses its form. The first time it sees the WM_WINDOWPOSCHANGED message, the WindowProc saves the form's X coordinate. After that, when the WindowProc sees the WM_WINDOWPOSCHANGING message, it resets the form's X coordinate to the saved value.
Warning: Subclassing is dangerous. The debugger does not work well when a new WindowProc is installed. If you halt the program instead of unloading its forms, it will crash and so will the Visual Basic IDE. Save your work often! Don't say you weren't warned.
|
|
Public Function NewWindowProc(ByVal hwnd As Long, ByVal msg _
As Long, ByVal wParam As Long, lParam As WINDOWPOS) As _
Long
Const WM_NCDESTROY = &H82
Const WM_WINDOWPOSCHANGING = &H46
Const WM_WINDOWPOSCHANGED = &H47
Static done_before As Boolean
Static left_edge As Integer
' If we're being destroyed,
' restore the original WindowProc.
If msg = WM_NCDESTROY Then
SetWindowLong _
hwnd, GWL_WNDPROC, _
OldWindowProc
End If
' Save the original x value.
If msg = WM_WINDOWPOSCHANGED And Not done_before Then
left_edge = lParam.x
done_before = True
End If
' Set the x value.
If msg = WM_WINDOWPOSCHANGING Then lParam.x = left_edge
' Continue normal processing. VERY IMPORTANT!
NewWindowProc = CallWindowProc( _
OldWindowProc, hwnd, msg, wParam, _
lParam)
End Function
|
|
|
|
|
|