Resizing form

Posted on 2004-09-29
Last Modified: 2010-05-02
I am using Vb6 and created a SDI form. i would like my form to be resized and Controls in the form to be arranged and positioned properly according to the form size. i have written code for arranging and positioning controls. But i need to restrict the users from resizing(reducing) the form after a specified Hieght and width. i tried the following statements in the Form resize event
    If Me.Width <= 9000 Then Me.Width = 9000
    If Me.Height < 8000 Then Me.Height = 8000

it works, but when Form is tried to resize below this range, Form continues to blink and only on the release of mouse drag it retains to size Width =9000 and height =8000.
Hoping for a solution

Question by:riyasjef
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12182810
' -------------------------------------------------------
' Form1
' -------------------------------------------------------
Option Explicit

Private Sub Form_Load()
    HookWindow Me.hWnd
End Sub

Private Sub Form_Unload(Cancel As Integer)
    HookWindow Me.hWnd
End Sub

' -------------------------------------------------------
' Module1
' -------------------------------------------------------
Option Explicit

Public Type POINTAPI
    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 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 Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)

Public Const GWL_WNDPROC As Long = (-4)
Public Const WM_GETMINMAXINFO = &H24

Public lPrevProc As Long

Public Sub HookWindow(ByVal lHandle As Long)
    If lPrevProc = 0 Then
        lPrevProc = SetWindowLong(lHandle, GWL_WNDPROC, AddressOf HookProc)
        Call SetWindowLong(lHandle, GWL_WNDPROC, lPrevProc)
    End If
End Sub

Public Function HookProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Dim minMax As MINMAXINFO

    Select Case uMsg
            CopyMemory minMax, ByVal lParam, LenB(minMax)
            ' don't allow form to be sized below minimum size
            minMax.ptMinTrackSize.x = 400                                         ' <-------------- Change these values
            minMax.ptMinTrackSize.y = 250
            CopyMemory ByVal lParam, minMax, LenB(minMax)
            HookProc = 1
            Exit Function
    End Select
    HookProc = CallWindowProc(lPrevProc, hWnd, uMsg, wParam, lParam)
End Function
LVL 86

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 12182820
The values are measured in Pixels, not Twips.


Author Comment

ID: 12183182

Author Comment

ID: 12187496

With that, program gets hanged when  tried to use break points  Any idea?

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12189567
What we have done is subclassed the form.  This means that we have replaced the forms normal message processing loop with our own.  You have to be careful when working with subclassed forms and the IDE as you may get funny results.  Do not press the Stop button in the IDE as this will prevent the old message loop from being placed back into the system.  Since the IDE is tied in the the app in debug mode with breakpoints, you can cause the app to crash.  Instead, always use the close button on the form.

When working with subclassing/hooking, always save your work before running the app since it may cause the app and the IDE to crash and close immediately.  Sometimes the only way to do something is with subclassing so you just have to learn to live with it.

What are you troubleshooting?  Perhaps I can help...


Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question