【问题标题】:How do I automatically move and resize buttons and labels when a form is expanded?展开表单时,如何自动移动和调整按钮和标签的大小?
【发布时间】:2016-03-16 10:13:41
【问题描述】:

所以我正在使用一个包含按钮和标签的表单应用程序 (Picture),我想知道如何在最大化表单时正确调整它们的大小。这些问题中的锚点或/和停靠属性:C# form auto resize windowAutomatic resizing of the Windows Forms controls 没有帮助,因为它仅扩展 1 个项目。

我需要它在不超出原始比例的情况下移动和调整大小。所以就像假设我有两个按钮。我希望它们始终保持在右侧,它们分别是 10*12 像素和距离边缘 2 像素。当我将原始窗口大小扩大三倍时,每个按钮仍将距边缘 2 像素,但大小为 30*36 像素。

【问题讨论】:

  • OnResize事件中编写自己的代码
  • 我知道 OnResize 但我不知道如何让它完全动态
  • 好吧,我建议你保留原来的布局信息,每次都从头开始计算。您还将使用锚定信息来决定要计算的内容。您可以尝试将原始大小保存在控件的标记属性中..

标签: c# winforms visual-studio-2015


【解决方案1】:

您需要在窗体上的每个控件上设置锚属性。请注意,如果您锚定两个相对的侧边,控件将被拉伸/挤压,但如果您只锚定到一侧,则控件将改为 移动

请注意,Winforms 应用程序让表单上的所有控件按比例调整大小是不正常的行为 - 您选择需要拉伸的控件(面板、树视图、图像、列表框等),然后选择标准控件(输入、按钮、下拉菜单等)应保持相同的大小但可以四处移动(即锚定到顶部和/或右侧)。

如果您尝试为您的应用执行某种“信息亭模式”,那么您通常会以较低的分辨率和/或较高的 DPI 运行以获得成比例的调整大小效果,这不需要更改只要您在表单中应用了惯用的锚定样式,您的应用程序就可以了。

【讨论】:

  • 是的。如果您锚定到左右,它将通过水平拉伸来调整大小。如果您锚定到顶部和底部,它将通过垂直拉伸来调整大小。您可以锚定到顶部、底部、左侧和右侧的任意组合,以获得您想要的。如果这对您不起作用,我可能无法理解您的要求,因为在我使用 WinForms 的所有这些年中,锚定一直足以满足我的需求。
【解决方案2】:

您可以将按钮等放在tableLayoutPanel 中,一列多行(您要放置的元素数量)。将 tableLayout 的锚点设置为“顶部”和“底部”。然后在每一行中放置一个按钮/标签。

用百分比值定义每行大小:

tableLayoutPanel1.RowStyles[i].SizeType = SizeType.Percent;
tableLayoutPanel1.RowStyles[i].Height = 20.0F; //=20%

最后将每个元素的锚点设置为“top”和“bottom”。

【讨论】:

    【解决方案3】:

    这是 12 个按钮 (4x3) 的示例代码。与按钮大小相比,它还会更改字体大小。

    1. 打开一个窗体
    2. 创建 12 个按钮
    3. 使用下面的代码:

      公共 Form1() { 初始化组件(); 设置组件(); }

      void SetComponents()
      {
          foreach (Control item in Controls)
          {
              if (item is Button)
              {
                  item.Width = Size.Width / 4 - 16;
                  item.Height = Size.Height / 4;
                  item.Font = new Font(Font.FontFamily, item.Height / 5);
              }
      
              button1.Left = 10;
              button1.Top = 10;
      
              button2.Left = button1.Right + 10;
              button2.Top = button1.Top;
      
              button3.Left = button2.Right + 10;
              button3.Top = button2.Top;
      
              button4.Left = button3.Right + 10;
              button4.Top = button3.Top;
      
              button5.Left = button1.Left;
              button5.Top = button1.Bottom + 10;
      
              button6.Left = button5.Right + 10;
              button6.Top = button2.Bottom + 10;
      
              button7.Left = button6.Right + 10;
              button7.Top = button3.Bottom + 10;
      
              button8.Left = button7.Right + 10;
              button8.Top = button4.Bottom + 10;
      
              button9.Left = button1.Left;
              button9.Top = button5.Bottom + 10;
      
              button10.Left = button9.Right + 10;
              button10.Top = button6.Bottom + 10;
      
              button11.Left = button10.Right + 10;
              button11.Top = button7.Bottom + 10;
      
              button12.Left = button11.Right + 10;
              button12.Top = button8.Bottom + 10;
      
          }
      }
      
      private void Form1_Resize(object sender, EventArgs e)
      {
          SetComponents();
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 2011-03-08
      • 2014-06-07
      • 2019-11-20
      • 2019-05-01
      • 2015-08-13
      相关资源
      最近更新 更多