学习.NET Winform开发 - 布局学习笔记
创建日期:2016/04/25
更新日期:2016/04/25
发布地址:http://www.cnblogs.com/gibbonnet/p/5431418.html
任务场景
基础
- 四行表格布局(菜单、工具栏、操作区、状态栏)
- 第1、2、4行固定高度,中间随窗口改变而改变。
高阶
- 中间操作区分为两列,可以改变宽度
DemoTableLayout.cs
命名空间:System.Windows.Forms
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
/// 样式表格布局
class DemoTableLayout: Form
{
public DemoTableLayout()
{
this.Text = "窗口的标题";
this.StartPosition = FormStartPosition.CenterScreen;
this.Size = new Size(500, 600);
TableLayoutPanel demoLayoutPanel = new TableLayoutPanel();
// 使布局面板填充整个Form
demoLayoutPanel.Dock = DockStyle.Fill;
this.Controls.Add(demoLayoutPanel);
// 布局方式:第一、三行随子元素,第二行填充剩余位置
demoLayoutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
demoLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
demoLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 10)); // 此处设置10或100都一样
demoLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
// 添加子元素
for(int i=0; i<3; i++) {
Button btn = new Button();
btn.Text = "Button";
demoLayoutPanel.Controls.Add(btn);
demoLayoutPanel.SetRow(btn, i);
demoLayoutPanel.SetColumn(btn, 0);
// 使子元素填充行
btn.Dock = DockStyle.Fill;
}
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new DemoTableLayout());
}
}
类 TableLayout
Represents a panel that dynamically lays out its contents in a grid composed of rows and columns.
- 列数 ColumnCount
- 行数 RowCount
- 填充到窗口 Dock
- 行的高度 RowStyles
枚举 DockStyle
指定控件停靠的位置和方式
{Bottom, Fill, Left, None, Right, Top}
布局的自身高度
AutoSize = true
true if the control automatically resizes based on its contents; otherwise, false. The default is true.
行的高度 RowStyle
- 行高固定:SizeType.Absolute
- 行高填充:SizeType.Percent?
- 行高随子元素: SizeType.AutoSize?
SizeType {Absolute, Percent, AutoSize}
单元格边框 CellBorderStyle
TableLayoutPanelCellBorderStyle {Inset, InsetDouble, None, Outset, OutsetDouble, OutsetPartial, Single}