본문 바로가기
VB.net

Form Size 동적으로 변화시 컨트롤 동적으로 변하게 하기

by 호야호잇 2019. 2. 13.

<<<<<<<<<<<<<   1번째 방법   >>>>>>>>>>>>>>>>

같은 속성 Anchor및 Docking유용 할 수 있지만, 컨트롤의 크기는 초기 비율을 유지하지 않습니다. 


다음 코드를 사용하면 동일한 비율로 컨트롤의 크기를 조정하는 동시에 폼의 크기를 원하는 값으로 변경할 수 있습니다.  


  Dim CW As Integer = Me.Width ' Current Width
    Dim CH As Integer = Me.Height ' Current Height
    Dim IW As Integer = Me.Width ' Initial Width
    Dim IH As Integer = Me.Height ' Initial Height

    Private Sub Form1_Resize(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles Me.Resize

        Dim RW As Double = (Me.Width - CW) / CW ' Ratio change of width
        Dim RH As Double = (Me.Height - CH) / CH ' Ratio change of height

        For Each Ctrl As Control In Controls
            Ctrl.Width += CInt(Ctrl.Width * RW)
            Ctrl.Height += CInt(Ctrl.Height * RH)
            Ctrl.Left += CInt(Ctrl.Left * RW)
            Ctrl.Top += CInt(Ctrl.Top * RH)
        Next

        CW = Me.Width
        CH = Me.Height

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load

        IW = Me.Width
        IH = Me.Height

    End Sub


<<<<<<<<<<<<<   2번째 방법   >>>>>>>>>>>>>>>>



1. 모듈에 소스 추가


    '================================ Form 동적으로 변경 가능하게 하는 로직 =================================

    Public Class Resizer


        Private Structure ControlInfo

            Public name As String

            Public parentName As String

            Public leftOffsetPercent As Double

            Public topOffsetPercent As Double

            Public heightPercent As Double

            Public originalHeight As Integer

            Public originalWidth As Integer

            Public widthPercent As Double

            Public originalFontSize As Single

        End Structure


        Private ctrlDict As Dictionary(Of String, ControlInfo) = New Dictionary(Of String, ControlInfo)


        Public Sub FindAllControls(thisCtrl As Control)

            For Each ctl As Control In thisCtrl.Controls

                Try

                    If Not IsNothing(ctl.Parent) Then

                        Dim parentHeight = ctl.Parent.Height

                        Dim parentWidth = ctl.Parent.Width

                        Dim c As New ControlInfo

                        c.name = ctl.Name

                        c.parentName = ctl.Parent.Name

                        c.topOffsetPercent = Convert.ToDouble(ctl.Top) / Convert.ToDouble(parentHeight)

                        c.leftOffsetPercent = Convert.ToDouble(ctl.Left) / Convert.ToDouble(parentWidth)

                        c.heightPercent = Convert.ToDouble(ctl.Height) / Convert.ToDouble(parentHeight)

                        c.widthPercent = Convert.ToDouble(ctl.Width) / Convert.ToDouble(parentWidth)

                        c.originalFontSize = ctl.Font.Size

                        c.originalHeight = ctl.Height

                        c.originalWidth = ctl.Width

                        ctrlDict.Add(c.name, c)

                    End If

                Catch ex As Exception

                    Debug.Print(ex.Message)

                End Try

                If ctl.Controls.Count > 0 Then

                    FindAllControls(ctl)

                End If

            Next '-- For Each

        End Sub


        Public Sub ResizeAllControls(thisCtrl As Control)

            Dim fontRatioW As Single

            Dim fontRatioH As Single

            Dim fontRatio As Single

            Dim f As Font


            For Each ctl As Control In thisCtrl.Controls

                Try

                    If Not IsNothing(ctl.Parent) Then

                        Dim parentHeight = ctl.Parent.Height

                        Dim parentWidth = ctl.Parent.Width

                        Dim c As New ControlInfo

                        Dim ret As Boolean = False

                        Try

                            ret = ctrlDict.TryGetValue(ctl.Name, c)

                            If (ret) Then

                                '-- Size

                                ctl.Width = Int(parentWidth * c.widthPercent)

                                ctl.Height = Int(parentHeight * c.heightPercent)

                                '-- Position

                                ctl.Top = Int(parentHeight * c.topOffsetPercent)

                                ctl.Left = Int(parentWidth * c.leftOffsetPercent)

                                '-- Font

                                f = ctl.Font

                                fontRatioW = ctl.Width / c.originalWidth

                                fontRatioH = ctl.Height / c.originalHeight

                                fontRatio = (fontRatioW +

                                    fontRatioH) / 2 '-- average change in control Height and Width

                                ctl.Font = New Font(f.FontFamily,

                                    c.originalFontSize * fontRatio, f.Style)

                            End If

                        Catch

                        End Try

                    End If

                Catch ex As Exception

                End Try

                If ctl.Controls.Count > 0 Then

                    ResizeAllControls(ctl)

                End If

            Next

        End Sub

    End Class

    '================================================================================================


2. 적용할 폼에 소스 추가


전역함수 : Dim rs As New Resizer


 Private Sub form1_Load(sender As Object, e As EventArgs) Handles Me.Load

              rs.FindAllControls(Me)

    End Sub

    Private Sub form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize

        rs.ResizeAllControls(Me)

    End Sub