2013年9月23日 星期一

Resize form and all controls in proportion to their startup size/location

參考引用
--


Public Class Form1

    Public NameControlDesign() As String
    Public LocationControlDesign() As Point
    Public SizeControlDesign() As Size
    Public SizeFormDesign As Size
    Public LastSize As Size
    Private font_size As Double
    Private new_font_size As Double
    Public heightratio As Double
    Public widthratio As Double
    Dim origWidth As Integer
    Dim origHeight As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LastSize = Me.Size
        font_size = Me.Font.Size
        new_font_size = Me.Font.Size
        origWidth = Me.Width
        origHeight = Me.Height
        ReDim NameControlDesign(Me.Controls.Count)
        ReDim LocationControlDesign(Me.Controls.Count)
        ReDim SizeControlDesign(Me.Controls.Count)
        SizeFormDesign = Me.Size


        For i As Integer = 0 To Me.Controls.Count - 1
            NameControlDesign(i) = Me.Controls(i).Name
            LocationControlDesign(i) = Me.Controls(i).Location
            SizeControlDesign(i) = Me.Controls(i).Size
        Next i
    End Sub

    Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        heightratio = Me.Height / origHeight
        widthratio = Me.Width / origWidth
        new_font_size = Math.Max(4, Math.Round(font_size * _
        IIf(Math.Abs(heightratio) < Math.Abs(widthratio), _
        heightratio, widthratio), 0))


        For a As Integer = 0 To sender.Controls.Count - 1
            sender.Controls(a).Location = New Point(sender.Size.Width / SizeFormDesign.Width * LocationControlDesign(a).X, sender.Size.Height / SizeFormDesign.Height * LocationControlDesign(a).Y)
            sender.Controls(a).Size = New Size(sender.Size.Width / SizeFormDesign.Width * SizeControlDesign(a).Width, sender.Size.Height / SizeFormDesign.Height * SizeControlDesign(a).Height)
        Next a
        Me.Refresh()
    End Sub
End Class

沒有留言:

張貼留言