What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a form resize in increments of 50 pixels in VB .NET
DescriptionThis example shows how to make a form resize in increments of 50 pixels in VB .NET. It overrides the form's WndProc subroutine and looks for WM_SIZING messages.
Keywordsapplication icon, icon, VB .NET
CategoriesVB.NET, Controls
The program overrides the form's WndProc subroutine to look for Windows messages. When it sees a WM_SIZING message, it uses Marshal.PtrToStructure to convert the LParam parameter into a Rect structure containing information about the resizing.

The program then gets the form's desired width and height and rounds them to the nearest multiple of 50 pixels. It then adjusts the Rect structure to use the new width and height while not moving the form's left or top edges.

The program then uses Marshal.StructureToPtr to copy the Rect structure back into the LParam parameter.

Finally, WndProc calls the parent's class's version of WndProc to process the message whether it is WM_SIZING or something else.

Imports System.Runtime.InteropServices
Public Structure Rect
    Public left As Integer
    Public top As Integer
    Public right As Integer
    Public bottom As Integer
End Structure

Protected Overrides Sub WndProc(ByRef m As _
    Const WM_SIZING As Long = &H214
    Const WMSZ_LEFT As Integer = 1
    Const WMSZ_RIGHT As Integer = 2
    Const WMSZ_TOP As Integer = 3
    Const WMSZ_TOPLEFT As Integer = 4
    Const WMSZ_TOPRIGHT As Integer = 5
    Const WMSZ_BOTTOM As Integer = 6
    Const WMSZ_BOTTOMLEFT As Integer = 7
    Const WMSZ_BOTTOMRIGHT As Integer = 8

    If m.Msg = WM_SIZING And m.HWnd.Equals(Me.Handle) Then
        ' Turn the message's lParam into a Rect.
        Dim r As Rect
        r = DirectCast( _
            Marshal.PtrToStructure(m.LParam, _
                GetType(Rect)), _

        ' Get the desired height and width.
        Dim hgt As Integer = r.bottom - r.top
        Dim wid As Integer = r.right - r.left

        ' Make height and width multiples of 50 pixels.
        hgt = 50 * (CInt(hgt / 50))
        wid = 50 * (CInt(wid / 50))

        ' See if the user is dragging the top edge.
        If m.WParam.ToInt32 = WMSZ_TOP Or _
           m.WParam.ToInt32 = WMSZ_TOPLEFT Or _
           m.WParam.ToInt32 = WMSZ_TOPRIGHT _
            ' Reset the top.
            r.top = r.bottom - hgt
            ' Reset the height to the saved value.
            r.bottom = r.top + hgt
        End If

        ' See if the user is dragging the left edge.
        If m.WParam.ToInt32 = WMSZ_LEFT Or _
           m.WParam.ToInt32 = WMSZ_TOPLEFT Or _
           m.WParam.ToInt32 = WMSZ_BOTTOMLEFT _
            ' Reset the left.
            r.left = r.right - wid
            ' Reset the width to the saved value.
            r.right = r.left + wid
        End If

        ' Update the Message object's LParam field.
        Marshal.StructureToPtr(r, m.LParam, True)
    End If

End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.