VB.Net/Windows System/WndProc

Материал из VB Эксперт
Перейти к: навигация, поиск

Overrides WndProc(ByRef m As System.Windows.Forms.Message)

Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.ruponentModel
Imports System.Windows.Forms

Public Class MainClass
    
    Shared Sub Main(ByVal args As String())
        Dim myform As Form = New Form1()
        Application.Run(myform)
    End Sub
End Class

Public Class Form1
    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 System.Windows.Forms.Message)
        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
        Static fixed_aspect_ratio As Double = 0
        Dim new_aspect_ratio As Double
        If m.Msg = WM_SIZING And m.HWnd.Equals(Me.Handle) Then
            Dim r As Rect
            r = DirectCast( _
            Marshal.PtrToStructure(m.LParam, GetType(Rect)), _
            Rect)
            Dim width As Double = r.right - r.left
            Dim height As Double = r.bottom - r.top
            new_aspect_ratio = height / width
            If fixed_aspect_ratio = 0 Then
                fixed_aspect_ratio = new_aspect_ratio
            End If
            If fixed_aspect_ratio <> new_aspect_ratio Then
                If m.WParam.ToInt32 = WMSZ_TOPLEFT Or _
                m.WParam.ToInt32 = WMSZ_TOPRIGHT Or _
                m.WParam.ToInt32 = WMSZ_BOTTOMLEFT Or _
                m.WParam.ToInt32 = WMSZ_BOTTOMRIGHT _
                Then
                    If new_aspect_ratio > fixed_aspect_ratio Then
                        width = height / fixed_aspect_ratio
                    Else
                        height = width * fixed_aspect_ratio
                    End If
                ElseIf m.WParam.ToInt32 = WMSZ_LEFT Or _
                m.WParam.ToInt32 = WMSZ_RIGHT _
                Then
                    height = width * fixed_aspect_ratio
                ElseIf m.WParam.ToInt32 = WMSZ_TOP Or _
                m.WParam.ToInt32 = WMSZ_BOTTOM _
                Then
                    width = height / fixed_aspect_ratio
                End If
                If m.WParam.ToInt32 = WMSZ_TOP Or _
                m.WParam.ToInt32 = WMSZ_TOPLEFT Or _
                m.WParam.ToInt32 = WMSZ_TOPRIGHT _
                Then
                    r.top = r.bottom - CInt(height)
                Else
                    r.bottom = r.top + CInt(height)
                End If
                If m.WParam.ToInt32 = WMSZ_LEFT Or _
                m.WParam.ToInt32 = WMSZ_TOPLEFT Or _
                m.WParam.ToInt32 = WMSZ_BOTTOMLEFT _
                Then
                    r.left = r.right - CInt(width)
                Else
                    r.right = r.left + CInt(width)
                End If
                Marshal.StructureToPtr(r, m.LParam, True)
            End If
        End If
        MyBase.WndProc(m)
    End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.SuspendLayout()
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Name = "Form1"
        Me.Text = "FixedAspectRatio"
        Me.ResumeLayout(False)
    End Sub
End Class