原文: http://zetcode.com/gui/csharpwinforms/layout/

Mono Winforms 教程继续进行控件的布局管理。 在将控件放置在其父容器上之后,我们必须确保其布局正确。

Anchor

控件的Anchor属性确定如何使用其父控件调整其大小。 锚是海洋世界中的一个术语。 当船锚掉入水中时,船就固定在某个地方。 Winforms 控件也是如此。

Winforms 中的每个控件都可以具有以下AnchorStyles值之一:

  • Top
  • Left
  • Right
  • Bottom

注意,控件不限于一个值。 他们可以使用|组合这些值。 运算符。

Anchor基本示例

下面的示例显示一个非常基本的示例,演示Anchor属性。

anchor.cs

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. class MForm : Form {
  5. public MForm() {
  6. Text = "Anchor";
  7. Size = new Size(210, 210);
  8. Button btn1 = new Button();
  9. btn1.Text = "Button";
  10. btn1.Parent = this;
  11. btn1.Location = new Point(30, 30);
  12. Button btn2 = new Button();
  13. btn2.Text = "Button";
  14. btn2.Parent = this;
  15. btn2.Location = new Point(30, 80);
  16. btn2.Anchor = AnchorStyles.Right;
  17. CenterToScreen();
  18. }
  19. }
  20. class MApplication {
  21. public static void Main() {
  22. MForm mf = new MForm();
  23. Application.Run(mf);
  24. }
  25. }

这是一个非常基本的代码示例,清楚地显示了Anchor属性的含义。 我们在表单上有两个按钮。 第一个按钮具有默认的AnchorStyles值,即AnchorStyles.Left。 第二个按钮明确设置了AnchorStyles.Right

  1. btn2.Anchor = AnchorStyles.Right;

我们将第二个按钮的Anchor属性明确设置为AnchorStyles.Right值。

现在看看以下两个图像。 左边的是开始时显示的应用。 调整大小后,右侧显示相同的应用。 第一个按钮与表单的左边界和上边界保持距离。 第二个按钮与表单的右边框保持距离。 但是它在垂直方向上没有保持任何距离。

Mono Winforms 中的布局管理 - 图1

Mono Winforms 中的布局管理 - 图2

图:调整大小前后

Dock

Dock属性允许我们将控件粘贴到父窗体或控件的特定边缘。

以下是可能的DockStyle值。

  • Top
  • Left
  • Right
  • Bottom
  • Fill
  • None

编辑器骨架

以下代码示例演示了正在使用的Dock属性。

editor.cs

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. class MForm : Form {
  5. public MForm() {
  6. Text = "Editor";
  7. Size = new Size(210, 180);
  8. MainMenu mainMenu = new MainMenu();
  9. MenuItem file = mainMenu.MenuItems.Add("&File");
  10. file.MenuItems.Add(new MenuItem("E&xit",
  11. new EventHandler(this.OnExit), Shortcut.CtrlX));
  12. Menu = mainMenu;
  13. TextBox tb = new TextBox();
  14. tb.Parent = this;
  15. tb.Dock = DockStyle.Fill;
  16. tb.Multiline = true;
  17. StatusBar sb = new StatusBar();
  18. sb.Parent = this;
  19. sb.Text = "Ready";
  20. CenterToScreen();
  21. }
  22. void OnExit(object sender, EventArgs e) {
  23. Close();
  24. }
  25. }
  26. class MApplication {
  27. public static void Main() {
  28. Application.Run(new MForm());
  29. }
  30. }

我们显示一个菜单栏和一个状态栏。 其余区域由TextBox控件占用。

  1. TextBox tb = new TextBox();
  2. tb.Parent = this;

在这里,我们创建TextBox控件。 Form容器被设置为文本框的父级。

  1. tb.Dock = DockStyle.Fill;

此代码行使TextBox控件占用了表单容器内的剩余空间。

Mono Winforms 中的布局管理 - 图3

图:编辑器骨架

固定按钮

下一个示例显示了位于窗体右下角的两个按钮。

anchoredbuttons.cs

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. class MForm : Form {
  5. private int WIDTH = 250;
  6. private int HEIGHT = 150;
  7. private int BUTTONS_SPACE = 15;
  8. private int PANEL_SPACE = 8;
  9. private int CLOSE_SPACE = 10;
  10. public MForm() {
  11. Text = "Anchor";
  12. Size = new Size(WIDTH, HEIGHT);
  13. Button ok = new Button();
  14. int PANEL_HEIGHT = ok.Height + PANEL_SPACE;
  15. Panel panel = new Panel();
  16. panel.Height = PANEL_HEIGHT;
  17. panel.Dock = DockStyle.Bottom;
  18. panel.Parent = this;
  19. int x = ok.Width * 2 + BUTTONS_SPACE;
  20. int y = (PANEL_HEIGHT - ok.Height) / 2;
  21. ok.Text = "Ok";
  22. ok.Parent = panel;
  23. ok.Location = new Point(WIDTH-x, y);
  24. ok.Anchor = AnchorStyles.Right;
  25. Button close = new Button();
  26. x = close.Width;
  27. close.Text = "Close";
  28. close.Parent = panel;
  29. close.Location = new Point(WIDTH-x-CLOSE_SPACE, y);
  30. close.Anchor = AnchorStyles.Right;
  31. CenterToScreen();
  32. }
  33. }
  34. class MApplication {
  35. public static void Main() {
  36. Application.Run(new MForm());
  37. }
  38. }

该示例在对话框的右下角显示确定,关闭按钮,这在对话框窗口中很常见。

  1. private int WIDTH = 250;
  2. private int HEIGHT = 150;

WIDTHHEIGHT变量确定应用窗口的宽度和高度。

  1. private int BUTTONS_SPACE = 15;
  2. private int PANEL_SPACE = 8;
  3. private int CLOSE_SPACE = 10;

BUTTONS_SPACE是“确定”和“关闭”按钮之间的空间。 PANEL_SPACE是面板和表单底部之间的空间。 最后,CLOSE_SPACE变量设置“关闭”按钮和表单右边框之间的间隔。

  1. int PANEL_HEIGHT = ok.Height + PANEL_SPACE;

在这里,我们计算面板的高度。 面板的高度基于“确定”按钮的高度。 并且我们添加了一些额外的空间,以使按钮不会太靠近边框。

  1. Panel panel = new Panel();
  2. panel.Height = PANEL_HEIGHT;
  3. panel.Dock = DockStyle.Bottom;
  4. panel.Parent = this

在这里,我们创建和管理Panel控件。 在此示例中,它用作按钮的容器。 它被粘贴到表单的底部边框。 然后将按钮放置在面板内。

  1. ok.Text = "Ok";
  2. ok.Parent = panel;
  3. ok.Location = new Point(WIDTH-x, y);
  4. ok.Anchor = AnchorStyles.Right;

“确定”按钮的父级设置为面板小部件。 计算位置。 并且Anchor属性设置为右侧。 另一个按钮的创建类似。

Mono Winforms 中的布局管理 - 图4

图:固定按钮

播放器骨架

Mono Winforms 教程这一部分的最后一个示例显示了一个更复杂的示例。 它是音乐播放器的骨架。

player.cs

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. class MForm : Form {
  5. public MForm() {
  6. Text = "Player";
  7. Size = new Size(350, 280);
  8. MainMenu mainMenu = new MainMenu();
  9. MenuItem file = mainMenu.MenuItems.Add("&File");
  10. MenuItem playm = mainMenu.MenuItems.Add("&Play");
  11. MenuItem view = mainMenu.MenuItems.Add("&View");
  12. MenuItem tools = mainMenu.MenuItems.Add("&Tools");
  13. MenuItem favourites = mainMenu.MenuItems.Add("&Favourites");
  14. MenuItem help = mainMenu.MenuItems.Add("&Help");
  15. file.MenuItems.Add(new MenuItem("E&xit",
  16. new EventHandler(this.OnExit), Shortcut.CtrlX));
  17. Menu = mainMenu;
  18. Panel panel = new Panel();
  19. panel.Parent = this;
  20. panel.BackColor = Color.Black;
  21. panel.Dock = DockStyle.Fill;
  22. Panel buttonPanel = new Panel();
  23. buttonPanel.Parent = this;
  24. buttonPanel.Height = 40;
  25. buttonPanel.Dock = DockStyle.Bottom;
  26. Button pause = new Button();
  27. pause.FlatStyle = FlatStyle.Popup;
  28. pause.Parent = buttonPanel;
  29. pause.Location = new Point(5, 10);
  30. pause.Size = new Size(25, 25);
  31. pause.Image = new Bitmap("pause.png");
  32. Button play = new Button();
  33. play.FlatStyle = FlatStyle.Popup;
  34. play.Parent = buttonPanel;
  35. play.Location = new Point(35, 10);
  36. play.Size = new Size(25, 25);
  37. play.Image = new Bitmap("play.png");
  38. Button forward = new Button();
  39. forward.FlatStyle = FlatStyle.Popup;
  40. forward.Parent = buttonPanel;
  41. forward.Location = new Point(80, 10);
  42. forward.Size = new Size(25, 25);
  43. forward.Image = new Bitmap("forward.png");
  44. Button backward = new Button();
  45. backward.FlatStyle = FlatStyle.Popup;
  46. backward.Parent = buttonPanel;
  47. backward.Location = new Point(110, 10);
  48. backward.Size = new Size(25, 25);
  49. backward.Image = new Bitmap("backward.png");
  50. TrackBar tb = new TrackBar();
  51. tb.Parent = buttonPanel;
  52. tb.TickStyle = TickStyle.None;
  53. tb.Size = new Size(150, 25);
  54. tb.Location = new Point(200, 10);
  55. tb.Anchor = AnchorStyles.Right;
  56. Button audio = new Button();
  57. audio.FlatStyle = FlatStyle.Popup;
  58. audio.Parent = buttonPanel;
  59. audio.Size = new Size(25, 25);
  60. audio.Image = new Bitmap("audio.png");
  61. audio.Location = new Point(170, 10);
  62. audio.Anchor = AnchorStyles.Right;
  63. StatusBar sb = new StatusBar();
  64. sb.Parent = this;
  65. sb.Text = "Ready";
  66. CenterToScreen();
  67. }
  68. void OnExit(object sender, EventArgs e) {
  69. Close();
  70. }
  71. }
  72. class MApplication {
  73. public static void Main() {
  74. Application.Run(new MForm());
  75. }
  76. }

这是一个更复杂的示例,它同时显示了DockAnchor属性。

  1. MainMenu mainMenu = new MainMenu();
  2. MenuItem file = mainMenu.MenuItems.Add("&File");
  3. ...
  4. Menu = mainMenu;

在这里,我们创建菜单栏。

  1. Panel panel = new Panel();
  2. panel.Parent = this;
  3. panel.BackColor = Color.Black;
  4. panel.Dock = DockStyle.Fill;

这是黑色的面板,占据了菜单栏,状态栏和控制面板剩余的所有剩余空间。

  1. Panel buttonPanel = new Panel();
  2. buttonPanel.Parent = this;
  3. buttonPanel.Height = 40;
  4. buttonPanel.Dock = DockStyle.Bottom;

这是控制面板。 它的父级是表单容器。 它被粘贴到表格的底部。 高度为 40 像素。 在此控制面板内部,我们放置了所有按钮和轨迹仪。

  1. Button pause = new Button();
  2. pause.FlatStyle = FlatStyle.Popup;
  3. pause.Parent = buttonPanel;
  4. pause.Location = new Point(5, 10);
  5. pause.Size = new Size(25, 25);
  6. pause.Image = new Bitmap("pause.png");

暂停按钮是具有默认Anchor属性值的四个按钮之一。 该按钮的样式设置为平面,因为它看起来更好。 我们在按钮上放置一个位图。

  1. tb.Anchor = AnchorStyles.Right;
  2. ...
  3. audio.Anchor = AnchorStyles.Right;

最后两个控件固定在右侧。

Mono Winforms 中的布局管理 - 图5

图:播放器骨架

Mono Winforms 教程的这一部分是关于控件的布局管理的。 我们实践了 Winforms 库提供的各种可能性。