返回介绍

First steps in Mono Winforms

发布于 2025-02-22 22:19:37 字数 7319 浏览 0 评论 0 收藏 0

In this part of the Mono Winforms tutorial, we introduce some basic programs in Winforms programing library.

Simple

This is a simple Winforms application.

simple.cs

using System.Windows.Forms;
using System.Drawing;

public class Simple : Form
{
  public Simple()
  {
     Text = "Simple";
     Size = new Size(250, 200);
     CenterToScreen();
  }

  static public void Main()
  {
     Application.Run(new Simple());
  }
}

This code example shows a small window on the screen.

using System.Windows.Forms;
using System.Drawing;

Here we use the using directive, which allows us to use a type from an appropriate namespace without a fully qualified name. For example, we can now write Form instead of System.Windows.Forms.Form.

public class Simple : Form
{
...
}

In Winforms, any window or a dialog is a Form . This control is a basic container, whose purpose is to display other child controls. Our class, Simple, inherits from a form. This way it becomes a form itself.

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

Text and Size are properties of a form. Changing these properties, we modify our form control. The first line displays text "Simple" in the titlebar of the form control. The second line sets the size of the form to 250x200 px.

CenterToScreen();

This method centers our application on the screen.

static public void Main()
{
   Application.Run(new Simple());
}

When compiled and run, the Main method is executed first. The code instantiates the Simple class and runs it.

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

Here is how we compile the source code. If we didn't make any mistakes, we should have simple.exe file in our current working directory.

Simple
Figure: Simple

Icon

Mono means monkey in Spanish. If we do not provide an icon for our application, we have a head of a monkey by default. The next example shows, how to change this.

icon.cs

using System.Windows.Forms;
using System.Drawing;
using System;

public class MForm : Form
{
  public MForm()
  {
     Text = "Icon";
     Size = new Size(250, 200);

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

     CenterToScreen();
  }

  static public void Main()
  {
     Application.Run(new MForm());
  }
}

The code example shows an icon in the upper left corner of the form. A form's icon is the picture that represents the form in the taskbar as well as the icon that is displayed for the control box of the form.

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

It is a good practice to put all input output work between the try/catch keywords. The web.ico file must be available in the current working directory. This is the directory from where we execute (./icon.exe) our application.

Icon
Figure: Icon

Tooltips

A tooltip is a small rectangular pop-up window that displays a brief description of a control's purpose when the user rests the pointer on the control.

tooltips.cs

using System;
using System.Drawing;
using System.Windows.Forms;

class MForm : Form {

  public MForm() {
    Text = "Tooltips";
    Size = new Size(250, 200);

    ToolTip btnTlp = new ToolTip();

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

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

    CenterToScreen();
  }
}

class MApplication {
  static void Main() {
    Application.Run(new MForm());
  }
}

Our code example creates a tooltip for two controls. The Button control and the Form control.

ToolTip btnTlp = new ToolTip();

Here we create the ToolTip control. This instance is used to provide tooltips for both controls.

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

Here we set a tooltip for a form.

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

And here for our button.

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

Notice the creation of the Button control. The Text property is a label for the button. The Location property places the button on the form at x=30, y = 70 px coordinates. Finally, the Parent property determines the container, where the button will reside.

Tooltips
Figure: Tooltips

Button

Our last code example shows a button control in action.

button.cs

using System;
using System.Drawing;
using System.Windows.Forms;

class MForm : Form {
  public MForm() {
    Text = "Button";
    Size = new Size(250, 200);

    Button button = new Button();

    button.Location = new Point(30, 20);
    button.Text = "Quit";
    button.Click += new EventHandler(OnClick);
    button.MouseEnter += new EventHandler(OnEnter);

    Controls.Add(button);
    CenterToScreen();
  }

  void OnClick(object sender, EventArgs e) {
     Close();
  }

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

}

class MApplication {
  public static void Main() {
    Application.Run(new MForm());
  }
}

All GUI programming is event driven programming. In our example, we show a button control on a form container. The button will listen to two events. The Click and the MouseEnter events.

button.Click += new EventHandler(OnClick);   

This code line plugs an event handler to the Click event. When we click on the button, the OnClick() method is called.

button.MouseEnter += new EventHandler(OnEnter);

When we enter the button area with the mouse pointer, the MouseEnter event is triggerd. In this case, our code calls the OnEnter() method.

void OnClick(object sender, EventArgs e) {
  Close();
}

The method closes the application.

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

When we enter the button control area with the mouse pointer, "Button Entered" text is displayed in the terminal.

This part of the Mono Winforms tutorial showed some introductory code examples to get you started with the Winforms programming library.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文