Does anyone know how I can limit the resizing of a form?
I tried using the Form_Resize event but it triggers only after the user resizes the form.
Any ideas?
Printable View
Does anyone know how I can limit the resizing of a form?
I tried using the Form_Resize event but it triggers only after the user resizes the form.
Any ideas?
you can use subclassing, which i think is mentioned in this web siote....
or you can set the width and height in the resize event programmitically...
Try this:
Code:Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
Dim LockWindow As Boolean
Dim iWidth As Integer
Dim iHeight As Integer
Private Sub Form_Load()
'Save the initial dimensions
iWidth = Me.Width
iHeight = Me.Height
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'Unlock the Window when the Mouse is moved over it
LockWindowUpdate 0&
End Sub
Private Sub Form_Resize()
If Me.WindowState <> vbMinimized Then
Static FirstLoad
'If this is the First time the Window is loading then don't lock it
If FirstLoad = False Then
FirstLoad = True
LockWindow = True
Exit Sub
End If
'Lock the Window when the User tries to resize it
If LockWindow = True Then
'Restore the dimensions
Me.Width = iWidth
Me.Height = iHeight
LockWindowUpdate Me.hwnd
End If
Else: FirstLoad = False
End If
End Sub
Or you can Subclass it (a little better than using LockWindowUpdate)
Code for a Module.
Code for Form.Code:Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
Const GWL_WNDPROC = (-4)
Const WM_SYSCOMMAND = &H112
Const WM_SIZE = &H5
Const WM_ENTERSIZEMOVE = &H231
Const WM_SIZING = &H214
Const WM_EXITSIZEMOVE = &H232
Public fHeight As Integer
Public fWidth As Integer
Global WndProcOld As Long
Public Function WindProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If wMsg = WM_SIZING Then
LockWindowUpdate hwnd
End If
If wMsg& = WM_EXITSIZEMOVE Then
Form1.Width = fWidth
Form1.Height = fHeight
LockWindowUpdate 0&
End If
WindProc = CallWindowProc(WndProcOld&, hwnd&, wMsg&, wParam&, lParam&)
End Function
Sub SubClassWnd(hwnd As Long)
WndProcOld& = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindProc)
End Sub
Sub UnSubclassWnd(hwnd As Long)
SetWindowLong hwnd, GWL_WNDPROC, WndProcOld&
WndProcOld& = 0
End Sub
Code:Private Sub Form_Load()
fHeight = Me.Height
fWidth = Me.Width
SubClassWnd Me.hwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnSubclassWnd Me.hwnd
End Sub
this would require a little work as you would need to add ctrl buttons, but you could just use a form with no border.
I thought he meant limiting the resizing when the BorderStyle is set to 2. Otherwise, you can just change it to Fixed Single or Fixed Dialog.
I don't know exactly what he means either.....I've just used the fixed or no border from time to time so it was just a thought.
V(ery) Basic:
In the code you pasted above, what type should lParam be in the WindowProc?
Because if I say "lParam As MINMAXINFO" then I can't CallWindowProc if I get other messages that their lParam is of Long type.