Subclassing

Using messages - MAXMIN INFO

One good example of using the messages sent to you is setting the Maximimum width and height for your form. You can do this using the Form_Resize event, however this occurs after the form has been resized, so there will be a large amount of flickering. The message that is sent to you is WM_GETMINMAXINFO. Use the code above to start the subclassing, and add the following code to the module.

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

'// extra type declarations
Type POINTAPI
    x As Long
    y As Long
End Type

Type MINMAXINFO
    ptReserved As POINTAPI
    ptMaxSize As POINTAPI
    ptMaxPosition As POINTAPI
    ptMinTrackSize As POINTAPI
    ptMaxTrackSize As POINTAPI
End Type

'// the message we will subclass
Public Const WM_GETMINMAXINFO As Long = &H24

'// use this WindowProc procedure
Public Function WindowProc(ByVal hWnd As Long, ByVal iMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
    '// ----WARNING----
    '// do not attempt to debug this procedure!!
    '// ----WARNING----

    '// this is our implementation of the message handling routine
    '// determine which message was recieved
    Select Case iMsg
    Case WM_GETMINMAXINFO
        '// dimention a variable to hold the structure passed from Windows in lParam
        Dim udtMINMAXINFO As MINMAXINFO
        Dim nWidthPixels As Long, nHeightPixels As Long
       
        nWidthPixels = Screen.Width * Screen.TwipsPerPixelX
        nHeightPixels = Screen.Height * Screen.TwipsPerPixelY
       
        '// copy the struct to our UDT variable
        CopyMemory udtMINMAXINFO, ByVal lParam, 40&
       
        With udtMINMAXINFO
            '// set the width of the form when it's maximized
            .ptMaxSize.x = nWidthPixels - (nWidthPixels 4)
            '// set the height of the form when it's maximized
            .ptMaxSize.y = nHeightPixels - (nHeightPixels 4)
           
            '// set the Left of the form when it's maximized
            .ptMaxPosition.x = nWidthPixels * 8
            '// set the Top of the form when it's maximized
            .ptMaxPosition.y = nHeightPixels * 8
           
            '// set the max width that the user can drag the form
            .ptMaxTrackSize.x = .ptMaxSize.x
            '// set the max height that the user can drag the form
            .ptMaxTrackSize.y = .ptMaxSize.y
           
            '// set the min width that the user can drag the form
            .ptMinTrackSize.x = nWidthPixels * 4
            '// set the min width that the user can drag the form
            .ptMinTrackSize.y = nHeightPixels * 4
        End With
       
        '// copy our modified struct back to the Windows struct
        CopyMemory ByVal lParam, udtMINMAXINFO, 40&
       
        '// return zero indicating that we have acted on this message
        WindowProc = False
       
        '// exit the function without letting VB get it's grubby little hands on the message
        Exit Function
    End Select
    '// pass all messages on to VB and then return the value to windows
    WindowProc = CallWindowProc(ProcOld, hWnd, iMsg, wParam, lParam)
End Function

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” - Bill Gates