Results 1 to 9 of 9

Thread: Stopping the form from going below a certain width and height?

  1. #1

    Thread Starter
    Addicted Member hypnos's Avatar
    Join Date
    Aug 2000
    Location
    UK
    Posts
    183

    Thumbs up 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

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    how about this:
    VB Code:
    1. Private Sub Form_Resize()
    2.  
    3. If Form1.WindowState <> vbMinimized And Form1.WindowState <> vbMaximized Then
    4.     If Form1.Width / Screen.TwipsPerPixelX < 320 Then
    5.         Form1.Width = 320 * Screen.TwipsPerPixelX
    6.     End If
    7.  
    8.     If Form1.Height / Screen.TwipsPerPixelY < 200 Then
    9.         Form1.Height = 200 * Screen.TwipsPerPixelY
    10.     End If
    11. End If
    12.  
    13. End Sub

  3. #3
    New Member pjak's Avatar
    Join Date
    Feb 2000
    Location
    UK
    Posts
    7
    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

  4. #4

    Thread Starter
    Addicted Member hypnos's Avatar
    Join Date
    Aug 2000
    Location
    UK
    Posts
    183
    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.

  5. #5
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    ...or forget that and use my very simple code...

  6. #6

    Thread Starter
    Addicted Member hypnos's Avatar
    Join Date
    Aug 2000
    Location
    UK
    Posts
    183

    Thumbs up

    Thanks pjak, I'll look in to that.

  7. #7
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    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?

  8. #8
    Megatron
    Guest
    Add to a Module
    VB Code:
    1. Public Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
    2. 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
    3. Public Const GWL_WNDPROC = (-4)
    4. Public Const WM_SIZE = &H5
    5. Public WndProcOld As Long
    6.  
    7. Public Function WindProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    8.        
    9.     If wMsg = WM_SIZE Then
    10.         If Form1.Width < 3000 Then
    11.             Form1.Width = 3000
    12.             Exit Function
    13.         End If
    14.         If Form1.Height < 3000 Then
    15.             Form1.Height = 3000
    16.             Exit Function
    17.         End If
    18.     End If
    19.    
    20.     WindProc = CallWindowProc(WndProcOld&, hwnd&, wMsg&, wParam&, lParam&)
    21.    
    22. End Function
    23.  
    24. Sub SubClassWnd(hwnd As Long)
    25.     WndProcOld& = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindProc)
    26. End Sub
    27.  
    28. Sub UnSubclassWnd(hwnd As Long)
    29.     SetWindowLong hwnd, GWL_WNDPROC, WndProcOld&
    30.     WndProcOld& = 0
    31. End Sub

    Add to a Form.
    VB Code:
    1. Private Sub Form_Load()
    2.     SubClassWnd hwnd
    3. End Sub
    4.  
    5. Private Sub Form_Unload(Cancel As Integer)
    6.     UnSubclassWnd hwnd
    7. End Sub

  9. #9
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Its pretty easy to achieve it with subclassing. Add a module to your form.

    Module code
    VB Code:
    1. Public g_lngDefWindowProc As Long
    2. Public g_lngMinX As Long
    3. Public g_lngMinY As Long
    4. Public g_lngMaxX As Long
    5. Public g_lngMaxY As Long
    6.  
    7. Public p_Form As Form
    8.  
    9. Public Const GWL_WNDPROC As Long = (-4)
    10. Public Const WM_GETMINMAXINFO As Long = &H24
    11.  
    12. Public Type POINTAPI
    13.     x As Long
    14.     y As Long
    15. End Type
    16.  
    17. Type MINMAXINFO
    18.     ptReserved As POINTAPI
    19.     ptMaxSize As POINTAPI
    20.     ptMaxPosition As POINTAPI
    21.     ptMinTrackSize As POINTAPI
    22.     ptMaxTrackSize As POINTAPI
    23. End Type
    24.  
    25. Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    26. 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
    27. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    28.  
    29.  
    30. Public Sub SubClass(hwnd As Long)
    31.    On Error Resume Next
    32.    g_lngDefWindowProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
    33. End Sub
    34.  
    35.  
    36. Public Sub UnSubClass(hwnd As Long)
    37.    If g_lngDefWindowProc Then
    38.       SetWindowLong hwnd, GWL_WNDPROC, g_lngDefWindowProc
    39.       g_lngDefWindowProc = 0
    40.    End If
    41. End Sub
    42.  
    43. Public Function WindowProc(ByVal p_lngHwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    44.     Dim udtMMI As MINMAXINFO
    45.  
    46.     On Error Resume Next
    47.  
    48.     Select Case p_lngHwnd
    49.         Case p_Form.hwnd
    50.  
    51.             Select Case uMsg
    52.                 Case WM_GETMINMAXINFO
    53.                  
    54.                     CopyMemory udtMMI, ByVal lParam, LenB(udtMMI)
    55.      
    56.                     'set the MINMAXINFO data to the
    57.                     'minimum and maximum values set
    58.                     'by the option choice
    59.                     With udtMMI
    60.                         .ptMinTrackSize.x = g_lngMinX
    61.                         .ptMinTrackSize.y = g_lngMinY
    62.                         .ptMaxTrackSize.x = g_lngMaxX
    63.                         .ptMaxTrackSize.y = g_lngMaxY
    64.                     End With
    65.      
    66.                     CopyMemory ByVal lParam, udtMMI, LenB(udtMMI)
    67.                    
    68.                     'according to MSDN we have to return 0 if we
    69.                     'processed the message ourselves
    70.                     WindowProc = 0
    71.                    
    72.                 Case Else
    73.              
    74.                     'take care of other messages
    75.                     WindowProc = CallWindowProc(g_lngDefWindowProc, p_lngHwnd, uMsg, wParam, lParam)
    76.             End Select
    77.     End Select
    78. End Function
    Form code
    VB Code:
    1. Private Sub Form_Load()
    2.    
    3.     'setup the max and min form sizes
    4.     g_lngMaxX = 600
    5.     g_lngMaxY = 600
    6.     g_lngMinX = 320
    7.     g_lngMinY = 200
    8.    
    9.     Set p_Form = Me
    10.     Call SubClass(Me.hwnd)
    11. End Sub
    12.  
    13.  
    14. Private Sub Form_Unload(Cancel As Integer)
    15.     Call UnSubClass(Me.hwnd)
    16. 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
  •  



Click Here to Expand Forum to Full Width