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 600×400 pixels, and smaller than 300×200 pixels. Moreover, when the form is maximized, it will be centered on the screen:

' REQUIRES THE MSGHOOK.DLL COMPONENT'Const WM_GETMINMAXINFO = &H24Private Type POINTAPI    X As Long    Y As LongEnd TypePrivate Type MINMAXINFO    ptReserved As POINTAPI    ptMaxSize As POINTAPI    ptMaxPosition As POINTAPI    ptMinTrackSize As POINTAPI    ptMaxTrackSize As POINTAPIEnd TypeDeclare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, _    source As Any, ByVal numBytes As Long)Dim WithEvents FormHook As MsgHookPrivate Sub Form_Load()    ' start form subclassing     Set FormHook = New MsgHook    FormHook.StartSubclass hWndEnd SubPrivate Sub FormHook_AfterMessage(ByVal uMsg As Long, ByVal wParam As Long, _    ByVal lParam As Long, retValue As Long)    If uMsg = WM_GETMINMAXINFO Then        ' 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)  _                2            .ptMaxPosition.Y = ((Screen.Height / Screen.TwipsPerPixelY) - 400)  _                2            ' 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 IfEnd Sub

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job