【发布时间】:2016-10-06 08:22:15
【问题描述】:
我有一个带有TableLayoutPanel 控件的 WinForm。我的代码将检测屏幕上连接的监视器的数量,为每个监视器创建一个列,然后在TableLayoutControl 的每个单独列中为每个显示添加一个按钮,这样我就可以确保无论连接了多少个监视器,按钮将在表单中“居中”显示。一/两个监视器渲染得很好,但是三个监视器导致端列分布不均。
这是我的代码:
int count = Screen.AllScreens.Count();
this.monitorLayoutPanel.ColumnCount = count;
ColumnStyle cs = new ColumnStyle(SizeType.Percent, 100 / count);
this.monitorLayoutPanel.ColumnStyles.Add(cs);
this.monitorLayoutPanel.AutoSize = true;
var buttonSize = new Size(95, 75);
int z = 0;
foreach (var screen in Screen.AllScreens.OrderBy(i => i.Bounds.X))
{
Button monitor = new Button
{
Name = "Monitor" + screen,
AutoSize = true,
Size = buttonSize,
BackgroundImageLayout = ImageLayout.Stretch,
BackgroundImage = Properties.Resources.display_enabled,
TextAlign = ContentAlignment.MiddleCenter,
Font = new Font("Segoe UI", 10, FontStyle.Bold),
ForeColor = Color.White,
BackColor = Color.Transparent,
Text = screen.Bounds.Width + "x" + screen.Bounds.Height,
Anchor = System.Windows.Forms.AnchorStyles.None
};
this.monitorLayoutPanel.Controls.Add(monitor, z, 0);
z++;
monitor.MouseClick += new MouseEventHandler(monitor_Click);
}
我尝试过缩小按钮并增加表单大小,但最后一列总是比前两列小。我无法理解!
【问题讨论】:
-
要使您的动态控件在表单的底部中心对齐,请查看第二个链接的帖子。您可以将按钮控件添加到自动调整大小的
FlowLayoutPanel,其锚点设置为Top, Bottom,并托管在单列停靠按钮TableLayoutPanel中。 -
如果您对内容使用合适的锚点(例如
None或Top, Bottom),则单个单元格TableLayoutPanel会将自动调整大小的内容保持在中心位置:Keep a Control vertically and horizontally at Center of Container
标签: c# winforms tablelayoutpanel