【问题标题】:adjust form controls according to the size of the window根据窗口大小调整表单控件
【发布时间】:2018-01-03 00:50:12
【问题描述】:

我正在尝试根据表单的大小调整控件的大小/重新定位控件。这是我正在使用的课程: 公共类大小调整器

    '----------------------------------------------------------
    ' ControlInfo
    ' Structure of original state of all processed controls
    '----------------------------------------------------------
    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

    '-------------------------------------------------------------------------
    ' ctrlDict
    ' Dictionary of (control name, control info) for all processed controls
    '-------------------------------------------------------------------------
    Private ctrlDict As Dictionary(Of String, ControlInfo) = New Dictionary(Of String, ControlInfo)

    '----------------------------------------------------------------------------------------
    ' FindAllControls
    ' Recursive function to process all controls contained in the initially passed
    ' control container and store it in the Control dictionary
    '----------------------------------------------------------------------------------------
    Public Sub FindAllControls(thisCtrl As Control)

        '-- If the current control has a parent, store all original relative position
        '-- and size information in the dictionary.
        '-- Recursively call FindAllControls for each control contained in the
        '-- current 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

    '----------------------------------------------------------------------------------------
    ' ResizeAllControls
    ' Recursive function to resize and reposition all controls contained in the Control
    ' dictionary
    '----------------------------------------------------------------------------------------
    Public Sub ResizeAllControls(thisCtrl As Control, Optional wform As Form = Nothing)

        Dim fontRatioW As Single
        Dim fontRatioH As Single
        Dim fontRatio As Single
        Dim f As Font
        If IsNothing(wform) = False Then wform.Opacity = 0

        '-- Resize and reposition all controls in the passed 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

                    Dim ret As Boolean = False
                    Try
                        '-- Get the current control's info from the control info dictionary
                        ret = ctrlDict.TryGetValue(ctl.Name, c)

                        '-- If found, adjust the current control based on control relative
                        '-- size and position information stored in the dictionary
                        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

            '-- Recursive call for controls contained in the current control
            If ctl.Controls.Count > 0 Then
                ResizeAllControls(ctl)
            End If
        Next '-- For Each
        If IsNothing(wform) = False Then wform.Opacity = 1

    End Sub

End Class

这段代码的问题在于 1)在调整控件大小或移动它们时闪烁很多。 2) 一些标签和按钮被移动到表单上的随机位置,并且, 3)背景图片的大小不响应表单的大小(关于如何执行的知识不多。)

感谢任何形式的帮助。

【问题讨论】:

  • 看起来像 TableLayoutPanel 会比这更容易。
  • 您能详细说明一下吗? @Plutonix
  • 他不需要详细说明。他给了你关键字:TableLayoutPanel。它的工作原理是您现在打开 Google 或 Bing 或其他一些搜索引擎并输入该词以查找有关它的更多信息。随着您了解更多,您可以细化搜索词以查找更具体的信息。不要坐在你的手上,期待别人解释你可以轻松找到的事情。似乎我们获得的顶级信息越多,许多人准备投入的精力就越少。
  • @jmcilhinney 是的。
  • 就这样吧

标签: vb.net controls


【解决方案1】:

不要手动执行此操作,而是查看表单上的属性,例如“Dock”、“Anchor”。

例子:

  1. 将扩展坞设置为顶部(或底部)将允许模块始终覆盖 表单的整个顶部(或底部),高度保持不变。
  2. 将停靠栏设置为左侧(或右侧)将允许模块始终覆盖表单的整个左侧(或右侧),同时保持高度 一样。
  3. 将锚属性设置为所有 Top、Left、Right、Down 将允许控件随着窗体的增长而扩展,但保持它的 x/y 左上角位置。
  4. 将锚属性设置为 Top、Left、Right 将允许控件随着窗体的增长向右扩展,但保持它的 x/y 左上角位置
  5. 将锚属性设置为 Bottom, Right 将允许控件保持在相对于窗体底角的固定位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 2012-03-19
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多