简体   繁体   English

如何在调整窗体大小时自动调整窗体上的控件大小或重新定位控件?

[英]How to automatically resize or reposition controls on a form when the form is resized?

So I'm trying to make my form fit to all monitors.所以我试图让我的表格适合所有显示器。 Some have different display resolution and scale.有些具有不同的显示分辨率和比例。

I can resize my form to fit to the display but all properties of its contents don't adjust to that new size.我可以调整我的表单大小以适应显示,但其内容的所有属性都不会调整到新的大小。

What I want is if the form is scaled to fit to the display, the controls on the Form should adjust as well.我想要的是,如果窗体被缩放以适合显示,窗体上的控件也应该调整。 Specifically properties like Left , Top , Width , Height , and so one, on every control.特别是每个控件上的LeftTopWidthHeight等属性。

The size could be scaled down or up.尺寸可以缩小或放大。

It's possible to iterate through all of the controls on the form (mostly) programmatically, rather than having to explicitly adjust each control.可以(主要)以编程方式遍历表单上的所有控件,而不必显式调整每个控件。 You may have to put in some exceptions for some types of controls (such as timers, which I've put in the example), but generally you can use something like:您可能需要为某些类型的控件(例如计时器,我在示例中放置)添加一些例外,但通常您可以使用类似的东西:

  Option Explicit

  Private Type ControlInfo_type
    Left As Single
    Top As Single
    Width As Single
    Height As Single
    FontSize As Single
  End Type
  Dim ControlInfos() As ControlInfo_type

Private Sub Form_Load()

  Dim ThisControl As Control
  
  ReDim Preserve ControlInfos(0 To 0)
  ControlInfos(0).Width = Me.Width
  ControlInfos(0).Height = Me.Height
  For Each ThisControl In Me.Controls
    ReDim Preserve ControlInfos(0 To UBound(ControlInfos) + 1)
    With ControlInfos(UBound(ControlInfos))
      .Left = ThisControl.Left
      .Top = ThisControl.Top
      .Width = ThisControl.Width
      .Height = ThisControl.Height
      .FontSize = ThisControl.FontSize
    End With
  Next

End Sub

Private Sub Form_Resize()

  Dim ThisControl As Control, HorizRatio As Single, VertRatio As Single, Iter As Integer
  
  If Me.WindowState = vbMinimized Then Exit Sub
  
  HorizRatio = Me.Width / ControlInfos(0).Width
  VertRatio = Me.Height / ControlInfos(0).Height
  
  Iter = 0
  For Each ThisControl In Me.Controls
    Iter = Iter + 1
    With ThisControl
      .Left = ControlInfos(Iter).Left * HorizRatio
      .Top = ControlInfos(Iter).Top * VertRatio
      .Width = ControlInfos(Iter).Width * HorizRatio
      .Height = ControlInfos(Iter).Height * VertRatio
      .FontSize = ControlInfos(Iter).FontSize * HorizRatio
    End With
  Next
  
End Sub

I tested this with the default form with a CommandButton, Frame, Timer, and TextBox, and it seemed to work OK.我使用带有命令按钮、框架、计时器和文本框的默认表单对此进行了测试,它似乎工作正常。 You'll probably want to tune the limits on the minimum and maximum sizes for appearance, and my handling of the font is very crude;您可能想要调整外观的最小和最大尺寸的限制,而我对字体的处理非常粗糙; this could be optimized also.这也可以优化。 But perhaps this could be a starting point.但也许这可能是一个起点。

This code depends upon the controls iterating the same way each time, which could conceivably break.此代码取决于控件每次都以相同的方式迭代,这可能会中断。 One way around this would be to use a Collection or other data structure with the name of the control as a key;解决此问题的一种方法是使用 Collection 或其他以控件名称作为键的数据结构; when iterating in the.Resize event, each control would be looked up by name.在.Resize 事件中进行迭代时,将按名称查找每个控件。 Additional structure will be necessary if any of the controls are themselves arrays, and even more if controls are loaded or unloaded dynamically.如果任何控件本身是 arrays,则需要额外的结构,如果动态加载或卸载控件,则需要更多结构。

Google found this post for me when looking for a VB6 autosize solution.谷歌在寻找 VB6 自动调整大小解决方案时为我找到了这篇文章。 As its only a year from the last activity you may be interested in my experience.由于距上次活动仅一年,您可能会对我的经历感兴趣。

This code is brilliant.这段代码很棒。 I tested it in a new project and simply added the code from here unchanged.我在一个新项目中对其进行了测试,并简单地从这里添加了代码。 The form has 18 controls (including two large Listviews) on a relatively small window but it can be resized at will and even go to full screen.该窗体在相对较小的 window 上有 18 个控件(包括两个大的 Listview),但可以随意调整大小,甚至 go 可以全屏显示。 The controls all move and/or change size in a very effective way as does the font size.所有控件都以非常有效的方式移动和/或更改大小,字体大小也是如此。 I tweaked size and position of a couple of labels to fit large text perfectly at full screen but otherwise it is simply my intended "static" design.我调整了几个标签的大小和 position 以在全屏下完美地适应大文本,但除此之外它只是我想要的“静态”设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM