|
-
Aug 4th, 2001, 11:58 AM
#1
Thread Starter
Addicted Member
Stopping the form from going below a certain width and height?
Hi all,
I've got a very basic question here. How would I stop the user from lowering the width and height below a certain size? Lets say I don't want the user the drag the form's size below 320 (width)and 200 (height). How would I accomplish this?
Cheers
-
Aug 4th, 2001, 12:07 PM
#2
PowerPoster
how about this:
VB Code:
Private Sub Form_Resize()
If Form1.WindowState <> vbMinimized And Form1.WindowState <> vbMaximized Then
If Form1.Width / Screen.TwipsPerPixelX < 320 Then
Form1.Width = 320 * Screen.TwipsPerPixelX
End If
If Form1.Height / Screen.TwipsPerPixelY < 200 Then
Form1.Height = 200 * Screen.TwipsPerPixelY
End If
End If
End Sub
-
Aug 4th, 2001, 12:09 PM
#3
New Member
use the min max size locking hook found on many vb tips sites on the internet
now help me. Look here.
http://www.vbforums.com/showthread.p...threadid=94019
-
Aug 4th, 2001, 12:12 PM
#4
Thread Starter
Addicted Member
Yes that would work but...
It would still allow you to drag the form beyond 320 pixels wide. The resize wouldn't take effect when you released the mouse I know. But I've seen programs where the mouse will just refuse to go another pixel when trying to resize it beyond a certain width and height.
-
Aug 4th, 2001, 12:12 PM
#5
PowerPoster
...or forget that and use my very simple code...
-
Aug 4th, 2001, 12:14 PM
#6
Thread Starter
Addicted Member
Thanks pjak, I'll look in to that.
-
Aug 4th, 2001, 12:19 PM
#7
PowerPoster
Originally posted by hypnos
Yes that would work but...
It would still allow you to drag the form beyond 320 pixels wide. The resize wouldn't take effect when you released the mouse I know.
Does that matter?
-
Aug 4th, 2001, 12:28 PM
#8
Add to a Module
VB Code:
Public Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Public 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
Public Const GWL_WNDPROC = (-4)
Public Const WM_SIZE = &H5
Public 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_SIZE Then
If Form1.Width < 3000 Then
Form1.Width = 3000
Exit Function
End If
If Form1.Height < 3000 Then
Form1.Height = 3000
Exit Function
End If
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
Add to a Form.
VB Code:
Private Sub Form_Load()
SubClassWnd hwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnSubclassWnd hwnd
End Sub
-
Aug 4th, 2001, 12:31 PM
#9
Its pretty easy to achieve it with subclassing. Add a module to your form.
Module code
VB Code:
Public g_lngDefWindowProc As Long
Public g_lngMinX As Long
Public g_lngMinY As Long
Public g_lngMaxX As Long
Public g_lngMaxY As Long
Public p_Form As Form
Public Const GWL_WNDPROC As Long = (-4)
Public Const WM_GETMINMAXINFO As Long = &H24
Public Type POINTAPI
x As Long
y As Long
End Type
Type MINMAXINFO
ptReserved As POINTAPI
ptMaxSize As POINTAPI
ptMaxPosition As POINTAPI
ptMinTrackSize As POINTAPI
ptMaxTrackSize As POINTAPI
End Type
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Public Sub SubClass(hwnd As Long)
On Error Resume Next
g_lngDefWindowProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub UnSubClass(hwnd As Long)
If g_lngDefWindowProc Then
SetWindowLong hwnd, GWL_WNDPROC, g_lngDefWindowProc
g_lngDefWindowProc = 0
End If
End Sub
Public Function WindowProc(ByVal p_lngHwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim udtMMI As MINMAXINFO
On Error Resume Next
Select Case p_lngHwnd
Case p_Form.hwnd
Select Case uMsg
Case WM_GETMINMAXINFO
CopyMemory udtMMI, ByVal lParam, LenB(udtMMI)
'set the MINMAXINFO data to the
'minimum and maximum values set
'by the option choice
With udtMMI
.ptMinTrackSize.x = g_lngMinX
.ptMinTrackSize.y = g_lngMinY
.ptMaxTrackSize.x = g_lngMaxX
.ptMaxTrackSize.y = g_lngMaxY
End With
CopyMemory ByVal lParam, udtMMI, LenB(udtMMI)
'according to MSDN we have to return 0 if we
'processed the message ourselves
WindowProc = 0
Case Else
'take care of other messages
WindowProc = CallWindowProc(g_lngDefWindowProc, p_lngHwnd, uMsg, wParam, lParam)
End Select
End Select
End Function
Form code
VB Code:
Private Sub Form_Load()
'setup the max and min form sizes
g_lngMaxX = 600
g_lngMaxY = 600
g_lngMinX = 320
g_lngMinY = 200
Set p_Form = Me
Call SubClass(Me.hwnd)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call UnSubClass(Me.hwnd)
End Sub
NOTICE: When you test this code, do not stop the project by pressing the End button on the toolbar, instead, close the form by pressing the X button or add a button with the code Unload frmMyForm
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|