图形界面编程(五) 布局容器类(2)

た 入场券 2022-07-14 23:52 230阅读 0赞

点击打开链接

3 布局中的空白区域

在.net Framework中,每个控件和其所处的容器以及容器内控件之间,都存在一个可以调整的空白区域,这个空白区域纯粹是为了布局的美观性而存在的。

容器和容器内控件之间的空白称为容器的Padding属性,容器内控件之间的空白称为控件的Margin属性。看一下示意图:

Padding和Margin示意图
图1 Padding和Margin示意图

很容易理解,蓝色箭头表示的四个空白位置为窗体容器的Padding属性;黄色箭头表示的四个空白位置为按钮控件的Margin属性。

注意:一个控件离其所在容器四周的距离,是容器的Padding属性和该控件的Margin属性之和。例如图中的按钮0,它和Form容器的左边的距离就是Form容器的Padding属性和按钮0的Margin属性之和。

Padding属性和Margin属性的值都是一个Padding类的对象,Padding类为值类型对象,具有四个整型类型属性:Left,Top,Right,Bottom,分别表左上右下四个方向的空白大小。Padding类还有一个特殊的整型类型属性:All,当Left,Top,Right,Bottom属性值相同时,All属性是Left,Top,Right,Bottom其中一个的值,否则All属性值为-1。即当Padding对象的All属性为-1时,表示其Left,Top,Right,Bottom属性值不同,当All属性值为正整数时,表示Left,Top,Right,Bottom值相同,等于All的值。

可以猜想,Padding类大致如下:

Padding.cs

[c-sharp] view plain copy

  1. public struct Padding {
  2. // 字段
  3. private bool all;
  4. private int top;
  5. private int left;
  6. private int right;
  7. private int bottom;
  8. // 静态只读字段, 表示一个四周为0的Padding对象
  9. public static readonly Padding Empty = new Padding(0);
  10. ///
  11. /// 构造器, 通过设置All属性, 设置上下左右四个属性值
  12. ///
  13. public Padding(int all) {
  14. // 设置all标志为true
  15. this.all = true;
  16. // 设置上下左右四个字段, 值相同
  17. this.top = this.left = this.right = this.bottom = all;
  18. }
  19. ///
  20. /// 设置上下左右四个属性值
  21. ///
  22. public Padding(int left, int top, int right, int bottom) {
  23. // 设置上下左右四个属性值
  24. this.top = top;
  25. this.left = left;
  26. this.right = right;
  27. this.bottom = bottom;
  28. // 根据上下左右四个属性值是否相同设置all标志
  29. this.all = (this.top == this.left) &&
  30. (this.left == this.right) &&
  31. (this.right == this.bottom) &&
  32. (this.bottom == this.top);
  33. }
  34. ///
  35. /// All属性, 如果上下左右属性值相同, 则返回其中一个值, 否则返回-1
  36. ///
  37. public int All {
  38. get {
  39. // 根据all标志是否为true, 决定返回上下左右任意值或-1
  40. return this.all ? this.top : -1;
  41. }
  42. set {
  43. // 设置all标志为true
  44. this.all = true;
  45. // 设置上下左右四个字段, 值相同
  46. this.top = this.left = this.right = this.bottom = value;
  47. }
  48. }
  49. ///
  50. /// 设置或获取底部空白
  51. ///
  52. public int Bottom {
  53. get {
  54. return this.bottom;
  55. }
  56. set {
  57. this.bottom = value;
  58. // 根据上下左右四个属性值是否相同设置all标志
  59. this.all = (this.top == this.left) &&
  60. (this.left == this.right) &&
  61. (this.right == this.bottom) &&
  62. (this.bottom == this.top);
  63. }
  64. }
  65. ///
  66. /// 设置或获取左边空白
  67. ///
  68. public int Left {
  69. get {
  70. return this.left;
  71. }
  72. set {
  73. this.left = value;
  74. this.all = (this.top == this.left) &&
  75. (this.left == this.right) &&
  76. (this.right == this.bottom) &&
  77. (this.bottom == this.top);
  78. }
  79. }
  80. ///
  81. /// 设置或获取右边空白
  82. ///
  83. public int Right {
  84. get {
  85. return this.right;
  86. }
  87. set {
  88. this.right = value;
  89. this.all = (this.top == this.left) &&
  90. (this.left == this.right) &&
  91. (this.right == this.bottom) &&
  92. (this.bottom == this.top);
  93. }
  94. }
  95. ///
  96. /// 设置或获取顶部空白
  97. ///
  98. public int Top {
  99. get {
  100. return this.top;
  101. }
  102. set {
  103. this.top = value;
  104. this.all = (this.top == this.left) &&
  105. (this.left == this.right) &&
  106. (this.right == this.bottom) &&
  107. (this.bottom == this.top);
  108. }
  109. }
  110. ///
  111. /// 获取水平空白总和
  112. ///
  113. public int Horizontal {
  114. get {
  115. return this.right + this.left;
  116. }
  117. }
  118. ///
  119. /// 获取垂直空白总和
  120. ///
  121. public int Vertical {
  122. get {
  123. return this.top + this.bottom;
  124. }
  125. }
  126. ///
  127. /// 获取以空白值为属性的Size类型对象
  128. ///
  129. public Size Size {
  130. get {
  131. return new Size(this.Horizontal, this.Vertical);
  132. }
  133. }
  134. ///
  135. /// 静态方法, 获取两个Padding对象的和
  136. /// (即将两个对象的上下左右属性各自相加后得到的Padding对象)
  137. ///
  138. public static Padding Add(Padding p1, Padding p2) {
  139. // 调用重载的+运算符
  140. return (p1 + p2);
  141. }
  142. ///
  143. /// 静态方法, 获取两个Padding对象的差
  144. /// (即将两个对象的上下左右属性各自想减后得到的Padding对象)
  145. ///
  146. public static Padding Subtract(Padding p1, Padding p2) {
  147. // 调用重载的-运算符
  148. return (p1 - p2);
  149. }
  150. ///
  151. /// 覆盖对象的比较方法
  152. ///
  153. public override bool Equals(object other) {
  154. // 判断对象的类型后调用对象重载的==运算符
  155. return ((other is Padding) && (((Padding)other) == this));
  156. }
  157. ///
  158. /// 重载+运算符, 表示两个Padding类型对象相加
  159. ///
  160. public static Padding operator +(Padding p1, Padding p2) {
  161. // 将两个对象的上下左右属性各自相加后得到的新的Padding对象
  162. return new Padding(p1.Left + p2.Left,
  163. p1.Top + p2.Top,
  164. p1.Right + p2.Right,
  165. p1.Bottom + p2.Bottom);
  166. }
  167. ///
  168. /// 重载-运算符, 表示两个Padding类型对象相减
  169. ///
  170. public static Padding operator -(Padding p1, Padding p2) {
  171. // 将两个对象的上下左右属性各自相减后得到的新的Padding对象
  172. return new Padding(p1.Left - p2.Left,
  173. p1.Top - p2.Top,
  174. p1.Right - p2.Right,
  175. p1.Bottom - p2.Bottom);
  176. }
  177. ///
  178. /// 重载==运算符
  179. ///
  180. public static bool operator ==(Padding p1, Padding p2) {
  181. if (p1.all && p2.all) { // 如果p1和p2的all字段都为true, 则比较它们的Top属性
  182. return p1.Top == p2.Top;
  183. } else { // 否则将它们对应属性一一比较
  184. return (p1.Left == p2.Left) &&
  185. (p1.Top == p2.Top) &&
  186. (p1.Right == p2.Right) &&
  187. (p1.Bottom == p2.Bottom);
  188. }
  189. }
  190. ///
  191. /// 重载!=运算符
  192. ///
  193. public static bool operator !=(Padding p1, Padding p2) {
  194. return !(p1 == p2);
  195. }
  196. ///
  197. /// 覆盖GetHashCode方法(因为覆盖了Equals方法)
  198. ///
  199. public override int GetHashCode() {
  200. return this.left ^ this.Right ^ this.Top ^ this.Bottom;
  201. }
  202. ///
  203. /// 覆盖ToString方法, 返回字符串表示
  204. ///
  205. public override string ToString() {
  206. return string.Format(“{ Left={0}, Top={1}, Right={2}, Bottom={3} }“,
  207. this.Left, this.Top, this.Right, this.Bottom);
  208. }
  209. }

通过Padding类的源码(猜想,非官方源码),可以了解Padding五个属性的工作方式,顺便复习一下值类型构造、比较、Empty字段以及运算符重载的知识。

4 流式布局

我们已经了解,控件都具备Dock方位布局能力,可以按照“上下左右中”五个方位将自身锚定在容器上进行布局。但我们也看到,如果要让容器内的控件进行其它形式的布局(例如上一节中控件成行列布局),则一般需要在容器的Resize事件处理方法内,为容器内的所有控件使用算法进行布局。其复杂度和代码量都不好控制。

除非有特殊需要,一般情况下我们尽量不要自己书写大段的控件布局代码,这些代码很不经济。.net Framework提供了若干个专门用来负责布局控件的容器,使用起来非常方便。

FlowLayoutPanel称为流式布局面板容器,它的作用是将容器内控件按照从左到右(或从右到左)以及从上到下(或从下到上)的顺序排列布局。几点重要属性:

  • FlowDirection属性:FlowDirection枚举类型。表示容器内控件布局方向。分别为:LeftToRight(从左到右),TopDown(从上到下),RightToLeft(从右到左),BottomUp(从下到上)四个枚举项。
  • WrapContents属性:bool类型。表示是否为容器内控件布局自动换行。对于属性值为true,当控件超出容器范围,则将控件自动排列到下一行;对于false,则忽略超出部分的显示。
  • AutoScroll属性:bool类型。表示当WrapContents属性为false时,当控件超出容器范围时,容器是否显示一个滚动条。

好了,该容器使用起来很简单,我们通过实例来说明:

界面显示效果图如下:

程序界面显示效果图图2 程序界面显示效果图

代码如下:

Program.cs

[c-sharp] view plain copy

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace Edu.Study.Graphics.FlowLayout {
  5. ///
  6. /// 窗体类
  7. ///
  8. class MyForm : Form {
  9. /************ 流式布局面板 ************/
  10. // 顶部流式布局面板
  11. private FlowLayoutPanel topPane;
  12. // 底部流式布局面板
  13. private FlowLayoutPanel bottomPane;
  14. /************ 文字标签 ************/
  15. // 布局方向标签
  16. private Label flowDirectionComboBoxLabel;
  17. // 容器Padding尺寸标签
  18. private Label panddingTrackBarLabel;
  19. // 控件Margin尺寸标签
  20. private Label marginTrackBarLabel;
  21. /************ 组合下拉列表框 ************/
  22. // 选择流式布局方向的下拉列表框
  23. private ComboBox flowDirectionComboBox;
  24. /************ 复选框 ************/
  25. // 布局容器是否自动换行复选框
  26. private CheckBox wrapCheckBox;
  27. // 容器是否自动换行复选框
  28. private CheckBox autoScrollCheckBox;
  29. /************ 数字调节块 ************/
  30. // 设置容器Padding属性的调节块
  31. private TrackBar panddingTrackBar;
  32. // 设置容器内控件Margin属性的调节块
  33. private TrackBar marginTrackBar;
  34. /************ 文本框 ************/
  35. // 显示容器Padding属性值的调节块
  36. private TextBox pandingTrackBarValueTextBox;
  37. // 显示容器内控件Margin属性值的调节块
  38. private TextBox marginTrackBarValueTextBox;
  39. /************ 按钮 ************/
  40. // bottomPane内放置的按钮控件, 是一个Button类数组
  41. private Button[] buttons;
  42. ///
  43. /// 构造器, 初始化所有控件
  44. ///
  45. public MyForm() {
  46. /************ topPane控件初始化 ************/
  47. this.topPane = new FlowLayoutPanel();
  48. // 设置topPane容器停靠在窗体顶部
  49. this.topPane.Dock = DockStyle.Top;
  50. // 设置topPane容器布局方向: 从左到右
  51. this.topPane.FlowDirection = FlowDirection.LeftToRight;
  52. // 设置topPane容器的Padding属性
  53. this.topPane.Padding = new Padding(30);
  54. // 设置topPane容器的边框属性, 呈现3D效果
  55. this.topPane.BorderStyle = BorderStyle.Fixed3D;
  56. // 设置topPane容器不自动换行
  57. this.topPane.WrapContents = false;
  58. // 设置topPane容器具有自动滚动条
  59. this.topPane.AutoScroll = true;
  60. /************ flowDirectionComboBoxLabel控件初始化 ************/
  61. this.flowDirectionComboBoxLabel = new Label();
  62. // 设置flowDirectionComboBoxLabel控件Margin属性
  63. this.flowDirectionComboBoxLabel.Margin = new Padding(2, 6, 0, 3);
  64. // 设置flowDirectionComboBoxLabel控件呈现文本
  65. this.flowDirectionComboBoxLabel.Text = “布局方向:”;
  66. // 设置flowDirectionComboBoxLabel控件自动调整尺寸
  67. this.flowDirectionComboBoxLabel.AutoSize = true;
  68. // 将flowDirectionComboBoxLabel控件增加在topPane容器内
  69. this.topPane.Controls.Add(this.flowDirectionComboBoxLabel);
  70. /************ flowDirectionComboBox控件初始化 ************/
  71. this.flowDirectionComboBox = new ComboBox();
  72. // 设置flowDirectionComboBox控件Margin属性
  73. this.flowDirectionComboBox.Margin = new Padding(0, 3, 0, 3);
  74. // 设置flowDirectionComboBox控件下拉列表内容
  75. this.flowDirectionComboBox.Items.AddRange(new object[] {
  76. FlowDirection.LeftToRight,
  77. FlowDirection.RightToLeft,
  78. FlowDirection.TopDown,
  79. FlowDirection.BottomUp
  80. });
  81. // 设置flowDirectionComboBox控件下拉默认选中项
  82. this.flowDirectionComboBox.SelectedIndex = 0;
  83. // 设置flowDirectionComboBox控件选择项改变后通知事件
  84. this.flowDirectionComboBox.SelectedIndexChanged +=
  85. new EventHandler(FlowDirectionComboBoxSelectedValueChanged);
  86. // 将flowDirectionComboBox控件增加在topPane容器内
  87. this.topPane.Controls.Add(this.flowDirectionComboBox);
  88. /************ wrapCheckBox控件初始化 ************/
  89. this.wrapCheckBox = new CheckBox();
  90. // 设置wrapCheckBox控件Margin属性
  91. this.wrapCheckBox.Margin = new Padding(20, 5, 0, 5);
  92. // 设置wrapCheckBox控件自动调整尺寸
  93. this.wrapCheckBox.AutoSize = true;
  94. // 设置wrapCheckBox控件呈现文本
  95. this.wrapCheckBox.Text = “是否自动换行”;
  96. // 设置wrapCheckBox控件选中状态改变通知事件
  97. this.wrapCheckBox.CheckedChanged += new EventHandler(WarpCheckBoxCheckedChanged);
  98. // 将wrapCheckBox控件增加在topPane容器内
  99. this.topPane.Controls.Add(this.wrapCheckBox);
  100. /************ autoScrollCheckBox控件初始化 ************/
  101. this.autoScrollCheckBox = new CheckBox();
  102. // 设置autoScrollCheckBox控件Margin属性
  103. this.autoScrollCheckBox.Margin = new Padding(20, 5, 0, 5);
  104. // 设置autoScrollCheckBox控件自动调整尺寸
  105. this.autoScrollCheckBox.AutoSize = true;
  106. // 设置autoScrollCheckBox控件呈现文本
  107. this.autoScrollCheckBox.Text = “是否使用滚动条”;
  108. // 设置autoScrollCheckBox控件选中状态改变通知事件
  109. this.autoScrollCheckBox.CheckedChanged += new EventHandler(AutoScrollCheckBoxChanged);
  110. // 将autoScrollCheckBox控件增加在topPane容器内
  111. this.topPane.Controls.Add(this.autoScrollCheckBox);
  112. /************ panddingTrackBarLabel控件初始化 ************/
  113. this.panddingTrackBarLabel = new Label();
  114. // 设置panddingTrackBarLabel控件自动调整尺寸
  115. this.panddingTrackBarLabel.AutoSize = true;
  116. // 设置panddingTrackBarLabel控件Margin属性
  117. this.panddingTrackBarLabel.Margin = new Padding(20, 5, 0, 5);
  118. // 设置panddingTrackBarLabel控件呈现文本
  119. this.panddingTrackBarLabel.Text = “更改容器的 Padding:”;
  120. // 将panddingTrackBarLabel控件增加在topPane容器内
  121. this.topPane.Controls.Add(this.panddingTrackBarLabel);
  122. /************ panddingTrackBar控件初始化 ************/
  123. this.panddingTrackBar = new TrackBar();
  124. // 设置panddingTrackBar控件Margin属性
  125. this.panddingTrackBar.Margin = new Padding(0, 3, 3, 3);
  126. // 设置panddingTrackBar控件宽度属性
  127. this.panddingTrackBar.Width = 120;
  128. // 设置panddingTrackBar控件数值最小值
  129. this.panddingTrackBar.Minimum = 0;
  130. // 设置panddingTrackBar控件数值最大值
  131. this.panddingTrackBar.Maximum = 100;
  132. // 设置panddingTrackBar控件单位数值
  133. this.panddingTrackBar.TickFrequency = 1;
  134. // 设置panddingTrackBar控件数值改变时通知事件
  135. this.panddingTrackBar.ValueChanged += new EventHandler(PandingTrackBarValueChanged);
  136. // 将panddingTrackBar控件增加在topPane容器内
  137. this.topPane.Controls.Add(this.panddingTrackBar);
  138. /************ pandingTrackBarValueTextBox控件初始化 ************/
  139. this.pandingTrackBarValueTextBox = new TextBox();
  140. // 设置pandingTrackBarValueTextBox控件Margin属性
  141. this.pandingTrackBarValueTextBox.Margin = new Padding(0, 3, 3, 3);
  142. // 设置pandingTrackBarValueTextBox控件只读
  143. this.pandingTrackBarValueTextBox.ReadOnly = true;
  144. // 设置pandingTrackBarValueTextBox控件宽度属性
  145. this.pandingTrackBarValueTextBox.Width = 80;
  146. // 设置pandingTrackBarValueTextBox控件背景色属性
  147. this.pandingTrackBarValueTextBox.BackColor = SystemColors.Window;
  148. // 将pandingTrackBarValueTextBox控件增加在topPane容器内
  149. this.topPane.Controls.Add(this.pandingTrackBarValueTextBox);
  150. /************ marginTrackBarLabel控件初始化 ************/
  151. this.marginTrackBarLabel = new Label();
  152. // 设置marginTrackBarLabel控件自动调整尺寸
  153. this.marginTrackBarLabel.AutoSize = true;
  154. // 设置marginTrackBarLabel控件Margin属性
  155. this.marginTrackBarLabel.Margin = new Padding(20, 5, 0, 5);
  156. // 设置marginTrackBarLabel控件呈现文本
  157. this.marginTrackBarLabel.Text = “更改容器内控件的 Margin:”;
  158. // 将marginTrackBarLabel控件增加在topPane容器内
  159. this.topPane.Controls.Add(this.marginTrackBarLabel);
  160. /************ marginTrackBar控件初始化 ************/
  161. this.marginTrackBar = new TrackBar();
  162. // 设置marginTrackBar控件Margin属性
  163. this.marginTrackBar.Margin = new Padding(0, 3, 3, 3);
  164. // 设置marginTrackBar控件宽度属性
  165. this.marginTrackBar.Width = 120;
  166. // 设置marginTrackBar控件数值最小值
  167. this.marginTrackBar.Minimum = 0;
  168. // 设置marginTrackBar控件数值最大值
  169. this.marginTrackBar.Maximum = 100;
  170. // 设置marginTrackBar控件单位数值
  171. this.marginTrackBar.TickFrequency = 1;
  172. // 设置marginTrackBar控件数值改变时通知事件
  173. this.marginTrackBar.ValueChanged += new EventHandler(MarginTrackBarValueChanged);
  174. // 将marginTrackBar控件增加在topPane容器内
  175. this.topPane.Controls.Add(this.marginTrackBar);
  176. /************ marginTrackBarValueTextBox控件初始化 ************/
  177. this.marginTrackBarValueTextBox = new TextBox();
  178. // 设置marginTrackBarValueTextBox控件Margin属性
  179. this.marginTrackBarValueTextBox.Margin = new Padding(0, 3, 3, 3);
  180. // 设置marginTrackBarValueTextBox控件只读
  181. this.marginTrackBarValueTextBox.ReadOnly = true;
  182. // 设置marginTrackBarValueTextBox控件宽度属性
  183. this.marginTrackBarValueTextBox.Width = 80;
  184. // 设置marginTrackBarValueTextBox控件背景色属性
  185. this.marginTrackBarValueTextBox.BackColor = SystemColors.Window;
  186. // 将marginTrackBarValueTextBox控件增加在topPane容器内
  187. this.topPane.Controls.Add(this.marginTrackBarValueTextBox);
  188. // 将topPane容器增加到窗体上
  189. this.Controls.Add(this.topPane);
  190. /************ bottomPane容器初始化 ************/
  191. this.bottomPane = new FlowLayoutPanel();
  192. // 设置bottomPane容器停靠在窗体底部
  193. this.bottomPane.Dock = DockStyle.Bottom;
  194. // 设置bottomPane容器布局方向: 从左到右
  195. this.bottomPane.FlowDirection = FlowDirection.LeftToRight;
  196. // 设置bottomPane容器的Padding属性
  197. this.bottomPane.Padding = new Padding(30);
  198. // 设置bottomPane容器的边框属性, 呈现3D效果
  199. this.bottomPane.BorderStyle = BorderStyle.Fixed3D;
  200. /************ wrapCheckBox控件初始化 ************/
  201. // 初始化200个Button对象的数组
  202. this.buttons = new Button[200];
  203. // 初始化数组中的每一项
  204. for (int i = 0; i < this.buttons.Length; i++) {
  205. Button btn = new Button();
  206. // 设置按钮呈现文字
  207. btn.Text = i.ToString();
  208. // 设置按钮的尺寸
  209. btn.Size = new Size(100, 80);
  210. buttons[i] = btn;
  211. }
  212. // 将所有的按钮增加到bottomPane容器上
  213. this.bottomPane.Controls.AddRange(buttons);
  214. // 将bottomPane容器增加到窗体上
  215. this.Controls.Add(this.bottomPane);
  216. // 设置窗体最大化
  217. this.WindowState = FormWindowState.Maximized;
  218. // 设置窗体最小尺寸
  219. this.MinimumSize = new Size(800, 600);
  220. }
  221. ///
  222. /// 覆盖窗体OnLoad方法, 处理窗体第一次加载通知消息
  223. ///
  224. protected override void OnLoad(EventArgs e) {
  225. base.OnLoad(e);
  226. // 设置wrapCheckBox复选框Checked属性和bottomPane容器WrapContents属性值一致
  227. this.wrapCheckBox.Checked = this.bottomPane.WrapContents;
  228. // 设置autoScrollCheckBox复选框Checked属性和bottomPane容器AutoScroll属性值一致
  229. this.autoScrollCheckBox.Checked = this.bottomPane.AutoScroll;
  230. // 设置pandingTrackBarValueTextBox文本框文本为bottomPane容器Padding属性值
  231. this.pandingTrackBarValueTextBox.Text = this.bottomPane.Padding.All.ToString();
  232. // 设置marginTrackBarValueTextBox文本框文本为按钮控件Margin属性值
  233. this.marginTrackBarValueTextBox.Text = this.buttons[0].Margin.All.ToString();
  234. // 设置panddingTrackBar控件当前数值为bottomPane容器Padding属性值
  235. this.panddingTrackBar.Value = this.bottomPane.Padding.All;
  236. // 设置marginTrackBar控件当前数值为按钮控件Margin属性值
  237. this.marginTrackBar.Value = this.buttons[0].Margin.All;
  238. }
  239. ///
  240. /// 处理flowDirectionComboBox下拉列表控件选项改变事件
  241. ///
  242. private void FlowDirectionComboBoxSelectedValueChanged(object sender, EventArgs e) {
  243. // 设置bottomPane容器布局方向与flowDirectionComboBox下拉列表选项一致
  244. this.bottomPane.FlowDirection = (FlowDirection)this.flowDirectionComboBox.SelectedItem;
  245. }
  246. ///
  247. /// 处理wrapCheckBox复选框选择状态改变事件
  248. ///
  249. private void WarpCheckBoxCheckedChanged(object sender, EventArgs e) {
  250. // 设置bottomPane容器是否自动换行属性与wrapCheckBox复选框选中状态一致
  251. this.bottomPane.WrapContents = this.wrapCheckBox.Checked;
  252. }
  253. ///
  254. /// 处理autoScrollCheckBox复选框选择状态改变事件
  255. ///
  256. private void AutoScrollCheckBoxChanged(object sender, EventArgs e) {
  257. // 设置bottomPane容器是否具备滚动条属性与autoScrollCheckBox复选框选中状态一致
  258. this.bottomPane.AutoScroll = this.autoScrollCheckBox.Checked;
  259. }
  260. ///
  261. /// 处理panddingTrackBar控件数值改变事件
  262. ///
  263. private void PandingTrackBarValueChanged(object sender, EventArgs e) {
  264. // 设置bottomPane容器Padding属性与panddingTrackBar当前数值一致
  265. this.bottomPane.Padding = new Padding(this.panddingTrackBar.Value);
  266. // 设置pandingTrackBarValueTextBox文本框文本内容为bottomPane容器Padding属性值
  267. this.pandingTrackBarValueTextBox.Text = this.bottomPane.Padding.All.ToString();
  268. }
  269. ///
  270. /// 处理marginTrackBarValueTextBox控件数值改变事件
  271. ///
  272. private void MarginTrackBarValueChanged(object sender, EventArgs e) {
  273. // 设置所有按钮控件的Margin属性与marginTrackBar当前数值一致
  274. foreach (Button btn in this.buttons) {
  275. btn.Margin = new Padding(this.marginTrackBar.Value);
  276. }
  277. // 设置marginTrackBarValueTextBox文本框文本内容为按钮Margin属性值
  278. this.marginTrackBarValueTextBox.Text = this.buttons[0].Margin.All.ToString();
  279. }
  280. ///
  281. /// 覆盖父类OnResize方法, 处理窗体尺寸变化消息通知
  282. ///
  283. protected override void OnResize(EventArgs e) {
  284. base.OnResize(e);
  285. // 设置topPane容器高度为窗体客户区高度1/6
  286. this.topPane.Height = this.ClientSize.Height / 6;
  287. // 设置bottomPane容器高度为窗体客户区高度5/6
  288. this.bottomPane.Height = this.ClientSize.Height - this.ClientSize.Height / 6;
  289. }
  290. }
  291. ///
  292. /// 包含住方法的类
  293. ///
  294. static class Program {
  295. ///
  296. /// 应用程序的主入口点。
  297. ///
  298. static void Main() {
  299. Application.EnableVisualStyles();
  300. Application.SetCompatibleTextRenderingDefault(false);
  301. Application.Run(new MyForm());
  302. }
  303. }
  304. }

本节代码下载

本次代码中,我们用到了一些其它控件,包括:

  • 文本标签控件(Label类);
  • 文本框控件(TextBox类);
  • 组合下拉列表框控件(ComboBox类);
  • 数值调节滑块控件(TrackBar类)

代码中除了展示FlowLayoutPanel容器和上述控件的用法外(特别注意这些控件的事件处理),还展示了容器的Padding属性和控件(以Button为例)的Margin属性,仔细阅读代码,灵活的掌握上述内容。

发表评论

表情:
评论列表 (有 0 条评论,230人围观)

还没有评论,来说两句吧...

相关阅读