AllApi.net

[an error occurred while processing this directive]
 

Howto limit a window's minimum and maximum size

To accomplish that, we used the code from a Knowledge Base article.

Steps to Create a Sample Application:

  1. Create a new Standard EXE project.
  2.  
  3. Paste the following code into Form1's code module:
          Option Explicit
    
          Private Sub Form_Load()
              'Save handle to the form.
              gHW = Me.hwnd
    
              'Begin subclassing.
              Hook
          End Sub
    
          Private Sub Form_Unload(Cancel As Integer)
              'Stop subclassing.
              Unhook
          End Sub
     
  4. Add a standard module to the project.
  5.  
  6. Paste the following code into the module:
          Option Explicit
    
          Private Const GWL_WNDPROC = -4
          Private Const WM_GETMINMAXINFO = &H24
    
          Private Type POINTAPI
              x As Long
              y As Long
          End Type
    
          Private Type MINMAXINFO
              ptReserved As POINTAPI
              ptMaxSize As POINTAPI
              ptMaxPosition As POINTAPI
              ptMinTrackSize As POINTAPI
              ptMaxTrackSize As POINTAPI
          End Type
    
          Global lpPrevWndProc As Long
          Global gHW As Long
    
          Private Declare Function DefWindowProc Lib "user32" Alias _
             "DefWindowProcA" (ByVal hwnd As Long, ByVal wMsg As Long, _
              ByVal wParam As Long, ByVal lParam As Long) As Long
          Private 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
          Private Declare Function SetWindowLong Lib "user32" Alias _
             "SetWindowLongA" (ByVal hwnd As Long, _
              ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
          Private Declare Sub CopyMemoryToMinMaxInfo Lib "KERNEL32" Alias _
             "RtlMoveMemory" (hpvDest As MINMAXINFO, ByVal hpvSource As Long, _
              ByVal cbCopy As Long)
          Private Declare Sub CopyMemoryFromMinMaxInfo Lib "KERNEL32" Alias _
             "RtlMoveMemory" (ByVal hpvDest As Long, hpvSource As MINMAXINFO, _
              ByVal cbCopy As Long)
    
          Public Sub Hook()
              'Start subclassing.
              lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, _
                 AddressOf WindowProc)
          End Sub
    
          Public Sub Unhook()
              Dim temp As Long
    
              'Cease subclassing.
              temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
          End Sub
    
          Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, _
             ByVal wParam As Long, ByVal lParam As Long) As Long
              Dim MinMax As MINMAXINFO
    
              'Check for request for min/max window sizes.
              If uMsg = WM_GETMINMAXINFO Then
                  'necesary for the caption of an MDI child (when maximized)
                  '(Thanks to Marvin Chinchilla for this information)
                  WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, _
    		wParam, lParam)
                  'Retrieve default MinMax settings
                  CopyMemoryToMinMaxInfo MinMax, lParam, Len(MinMax)
    
                  'Specify new minimum size for window.
                  MinMax.ptMinTrackSize.x = 200
                  MinMax.ptMinTrackSize.y = 200
    
                  'Specify new maximum size for window.
                  MinMax.ptMaxTrackSize.x = 500
                  MinMax.ptMaxTrackSize.y = 500
    
                  'Copy local structure back.
                  CopyMemoryFromMinMaxInfo lParam, MinMax, Len(MinMax)
    
                  WindowProc = DefWindowProc(hw, uMsg, wParam, lParam)
              Else
                  WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, _
                     wParam, lParam)
              End If
          End Function
     
  7. Save and run the sample application.
  8.  
  9. Attempt to resize Form1.

    RESULT: The form is not allowed to be sized smaller than 200 by 200 pixels or larger than 500 by 500 pixels.
 
Copyright © 1998-2000, The KPD-Team.
Send mail to KPDTeam@Allapi.net with comments about this web site.
This site is located at
http://www.Allapi.net/