Form Resizing Limits

Hello, regarding question: 
how can I do it another way,The reasion why i don't want to use this, is because 1) its subclassing, but mostly 2) i am using this on a child form in a mdiform that makes it look strange when i maximize everything, I am pretty sure its incompatible with my other resize modules, however i just wanted to see if anyone can give me a diffrent metthod, (here is the code i have, but i don't want)
'===============Bas module code============
     x As Long
     y As Long
End Type

     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
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 Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)

Public Const GWL_WNDPROC = (-4)

Public OldProc As Long
Public MinWidth As Long
Public MinHeight As Long

Public Function WndProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Select Case wMsg
         CopyMemory MMinfo, ByVal lParam, LenB(MMinfo)
       ' Max/Min size when tracking
         MMinfo.ptMinTrackSize.x = MinWidth
         MMinfo.ptMinTrackSize.y = MinHeight
'         MMinfo.ptMaxTrackSize.x = MaxWidth
'         MMinfo.ptMaxTrackSize.y = MaxHeight
         CopyMemory ByVal lParam, MMinfo, LenB(MMinfo)
         Exit Function
    End Select
    WndProc = CallWindowProc(OldProc, hwnd, wMsg, wParam, lParam)
End Function

'===========Form code===========
Private Sub Form_Load()
   MinWidth = 128
   MinHeight = 128
   Me.AutoRedraw = True
   Me.Print "Minimum size of this form" & vbCrLf & "is limited to 128x128 pixels"
   OldProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WndProc)
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Call SetWindowLong(hwnd, GWL_WNDPROC, OldProc)
End Sub
Note: this code use subclassing, so don't stop it with IDE stop button, use form [x] button to close form.
Who is Participating?
Dang123Connect With a Mentor Commented:
    I use it in a few programs to prevent the form from becoming too small and causing problems in my control resizing logic. I have had no problems, or complaints from my users. The only oddity I noticed was that you could see the resize line move smaller than allowed then "snap" back to the minimum size when the mouse was released. I would suggest you try it in your specific situation and see if you have problems. It is a small, simple snippet of code that should only take a few minutes to try.


   It may be simpler to use VB to lock the form size

Private Sub Form_Resize()
    ' Enforcing either minimum or maximum is optional
    ' You can do one without the other
    'Enforce minimum size
    If Me.Height < 128 Then Me.Height = 128
    If Me.Width < 128 Then Me.Width = 128
    'Enforce maximum size
    If Me.Height > 3000 Then Me.Height = 3000
    If Me.Width > 3000 Then Me.Width = 3000
End Sub


kwickwayAuthor Commented:
I think i remember doing that, but it was messy looking.  Was it for you?
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

kwickwayAuthor Commented:
kwickwayAuthor Commented:
do you know of any way that the form doesnt auto re-draw while your doing this?
Sorry, I don't. I didn't need to do before.
kwickwayAuthor Commented:
Ok, I accepted your awnser anyway, but if you do figure that out:
Thank you.
I'll listen in on the other question so I'll know in the future.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.