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

在 Mono Winforms 教程的这一部分中,我们介绍 Winforms 编程库中的一些基本程序。

简单

这是一个简单的 Winforms 应用。

simple.cs

  1. using System.Windows.Forms;
  2. using System.Drawing;
  3. public class Simple : Form
  4. {
  5. public Simple()
  6. {
  7. Text = "Simple";
  8. Size = new Size(250, 200);
  9. CenterToScreen();
  10. }
  11. static public void Main()
  12. {
  13. Application.Run(new Simple());
  14. }
  15. }

此代码示例在屏幕上显示一个小窗口。

  1. using System.Windows.Forms;
  2. using System.Drawing;

在这里,我们使用using指令,该指令允许我们使用适当名称空间中的类型,而无需使用完全限定的名称。 例如,我们现在可以编写Form而不是System.Windows.Forms.Form

  1. public class Simple : Form
  2. {
  3. ...
  4. }

在 Winforms 中,任何窗口或对话框都是Form。 该控件是一个基本容器,其目的是显示其他子控件。 我们的类Simple继承自表单。 这样,它本身就成为一种形式。

  1. Text = "Simple";
  2. Size = new Size(250, 200);

TextSize是表单的属性。 更改这些属性,我们将修改表单控件。 第一行在表单控件的标题栏中显示文本"Simple"。 第二行将表单的大小设置为250x200px

  1. CenterToScreen();

这种方法将我们的应用集中在屏幕上。

  1. static public void Main()
  2. {
  3. Application.Run(new Simple());
  4. }

编译并运行后,将首先执行Main方法。 该代码实例化Simple类并运行它。

  1. $ gmcs -r:System.Windows.Forms.dll -r:System.Drawing.dll simple.cs

这是我们编译源代码的方式。 如果没有犯任何错误,则应在当前工作目录中包含simple.exe文件。

Mono Winforms 中的第一步 - 图1

图:简单

图标

Mono 在西班牙语中意为猴子。 如果我们不为应用提供图标,则默认情况下,我们的头是猴子。 下一个示例显示如何更改此设置。

icon.cs

  1. using System.Windows.Forms;
  2. using System.Drawing;
  3. using System;
  4. public class MForm : Form
  5. {
  6. public MForm()
  7. {
  8. Text = "Icon";
  9. Size = new Size(250, 200);
  10. try {
  11. Icon = new Icon("web.ico");
  12. } catch (Exception e) {
  13. Console.WriteLine(e.Message);
  14. Environment.Exit(1);
  15. }
  16. CenterToScreen();
  17. }
  18. static public void Main()
  19. {
  20. Application.Run(new MForm());
  21. }
  22. }

该代码示例在窗体的左上角显示一个图标。 表单的图标是代表任务栏中表单的图片以及为表单的控制框显示的图标。

  1. try {
  2. Icon = new Icon("web.ico");
  3. } catch (Exception e) {
  4. Console.WriteLine(e.Message);
  5. Environment.Exit(1);
  6. }

最好将所有输入输出工作放在try/catch关键字之间。 web.ico文件必须在当前工作目录中可用。 这是我们执行(./icon.exe)应用的目录。

Mono Winforms 中的第一步 - 图2

图:图标

工具提示

工具提示是一个小的矩形弹出窗口,当用户将指针放在控件上时,它会显示控件目的的简短说明。

tooltips.cs

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. class MForm : Form {
  5. public MForm() {
  6. Text = "Tooltips";
  7. Size = new Size(250, 200);
  8. ToolTip btnTlp = new ToolTip();
  9. btnTlp.SetToolTip(this, "This is a Form");
  10. Button button = new Button();
  11. btnTlp.SetToolTip(button, "This is a Button Control");
  12. button.Text = "Button";
  13. button.Location = new Point(30, 70);
  14. button.Parent = this;
  15. CenterToScreen();
  16. }
  17. }
  18. class MApplication {
  19. static void Main() {
  20. Application.Run(new MForm());
  21. }
  22. }

我们的代码示例为两个控件创建一个工具提示。 Button控件和Form控件。

  1. ToolTip btnTlp = new ToolTip();

在这里,我们创建ToolTip控件。 此实例用于为两个控件提供工具提示。

  1. btnTlp.SetToolTip(this, "This is a Form");

在这里,我们为表单设置工具提示。

  1. btnTlp.SetToolTip(button, "This is a Button Control");

这里是我们的按钮。

  1. Button button = new Button();
  2. btnTlp.SetToolTip(button, "This is a Button Control");
  3. button.Text = "Button";
  4. button.Location = new Point(30, 70);
  5. button.Parent = this;

注意Button控件的创建。 Text属性是按钮的标签。 Location属性将按钮放在x = 30y = 70px坐标的表单上。 最后,Parent属性确定按钮所在的容器。

Mono Winforms 中的第一步 - 图3

图:工具提示 s

按钮

我们的最后一个代码示例显示了一个有效的按钮控件。

button.cs

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. class MForm : Form {
  5. public MForm() {
  6. Text = "Button";
  7. Size = new Size(250, 200);
  8. Button button = new Button();
  9. button.Location = new Point(30, 20);
  10. button.Text = "Quit";
  11. button.Click += new EventHandler(OnClick);
  12. button.MouseEnter += new EventHandler(OnEnter);
  13. Controls.Add(button);
  14. CenterToScreen();
  15. }
  16. void OnClick(object sender, EventArgs e) {
  17. Close();
  18. }
  19. void OnEnter(object sender, EventArgs e) {
  20. Console.WriteLine("Button Entered");
  21. }
  22. }
  23. class MApplication {
  24. public static void Main() {
  25. Application.Run(new MForm());
  26. }
  27. }

所有 GUI 编程都是事件驱动的编程。 在我们的示例中,我们在表单容器上显示了一个按钮控件。 该按钮将收听两个事件。 ClickMouseEnter事件。

  1. button.Click += new EventHandler(OnClick);

此代码行将事件处理器插入Click事件。 当我们单击按钮时,将调用OnClick()方法。

  1. button.MouseEnter += new EventHandler(OnEnter);

当我们使用鼠标指针进入按钮区域时,将触发MouseEnter事件。 在这种情况下,我们的代码将调用OnEnter()方法。

  1. void OnClick(object sender, EventArgs e) {
  2. Close();
  3. }

该方法关闭应用。

  1. void OnEnter(object sender, EventArgs e) {
  2. Console.WriteLine("Button Entered");
  3. }

当我们使用鼠标指针进入按钮控制区域时,终端中将显示"Button Entered"文本。

Mono Winforms 教程的这一部分显示了一些入门代码示例,以帮助您开始使用 Winforms 编程库。