Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: VB5,VB6
Expertise: Advanced
Jan 1, 2000



Application Security Testing: An Integral Part of DevOps

Determine maximum size and the position of a maximized form

When the user resizes a form with the mouse or the keyboard, or maximizes it, Windows sends the form a WM_GETMINMAXINFO message, with lParam pointing to a MINMAXINFO structure that the form must fill with information about the minimum and maximum size that it is willing to be resized. Using a subclassing technique you can trap this message and fill the structure yourself, instead of letting VB fill it with default values.

The following example shows how you can prevent a form from being resized larger than 600x400 pixels, and smaller than 300x200 pixels. Moreover, when the form is maximized, it will be centered on the screen:

Private 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
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, _
    source As Any, ByVal numBytes As Long)

Dim WithEvents FormHook As MsgHook

Private Sub Form_Load()
    ' start form subclassing 
    Set FormHook = New MsgHook
    FormHook.StartSubclass hWnd
End Sub

Private Sub FormHook_AfterMessage(ByVal uMsg As Long, ByVal wParam As Long, _
    ByVal lParam As Long, retValue As Long)
        ' Windows is querying the form for its
        ' minimum and maximum size and position.
        Dim mmInfo As MINMAXINFO
        ' Read contents of structure pointed to by lParam.
        CopyMemory mmInfo, ByVal lParam, Len(mmInfo)
        With mmInfo
            ' ptMaxSize is the size of the maximized form.
            .ptMaxSize.X = 600
            .ptMaxSize.Y = 400
            ' ptMaxPosition is the position of the maximized form.
            .ptMaxPosition.X = ((Screen.Width / Screen.TwipsPerPixelX) - 600) \ _
            .ptMaxPosition.Y = ((Screen.Height / Screen.TwipsPerPixelY) - 400) \ _
            ' ptMinTrackSize is the minimum size of a form when resizing with 
            ' the mouse.
            .ptMinTrackSize.X = 300
            .ptMinTrackSize.Y = 200
            ' ptMinTrackSize is the maximum size of a form when resizing with 
            ' the mouse.
            ' (It is usually equal to ptMaxSize.)
            .ptMaxTrackSize.X = 600
            .ptMaxTrackSize.Y = 400
        End With
        ' Copy back in the original structure in memory.
        CopyMemory ByVal lParam, mmInfo, Len(mmInfo)
        ' Must return zero to inform that the structure has been modified.
        retValue = 0
    End If
End Sub
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date