耿鴨玩轉C#之WinForm界面設計

0. 簡介

WinForm 是 Windows Form 的簡稱,是基於 .NET Framework 平臺的客戶端(PC軟件)開發技術,一般使用 C# 編程。C# WinForm 編程需要創建「Windows窗體應用程序」項目。
.NET 提供了大量 Windows 風格的控件和事件,我們可以直接拿來使用,上手簡單,開發快速。

1. Hello, world!

1.1 新建項目

打開Visual Studio 2019,新建項目。然後選擇“Windows窗體應用”。
在這裏插入圖片描述

1.2 控件工具箱

選擇“視圖->工具箱”,打開控件工具箱。
在這裏插入圖片描述
如果工具箱中的控件不能滿足開發項目的需求,也可以向工具箱中添加新的控件, 或者對工具箱中的控件重置或進行分組等操作。右擊工具箱,在彈出的右鍵菜單中選擇“選擇項”,彈出以下窗口。
在這裏插入圖片描述

1.3 高分屏設置

隨意拖動幾個按鈕,然後選擇“調試->開始執行(不調試)”。可以看到,程序的實際運行效果和設計界面的不太一樣。
在這裏插入圖片描述
這是由於C#默認情況下沒有禁用高分屏自動縮放,只要禁用了即可,方法如下:
在項目上點擊右鍵,選擇“添加->類”。然後選擇“應用程序清單文件”。
在這裏插入圖片描述
找到大約47行的位置,去除註釋即可。
在這裏插入圖片描述
再一次運行程序,效果很棒!
在這裏插入圖片描述

1.4 窗體屬性

在 Windows 窗體應用程序中右擊窗體,在彈出的右鍵菜單中選擇“屬性”命令,彈出如下圖所示的屬性面板。
在這裏插入圖片描述
窗體的常用屬性如下表所示:
在這裏插入圖片描述

1.5 新建窗體

下面創建一個新的窗體。
在這裏插入圖片描述
在這裏插入圖片描述

1.6 設置啓動窗體

每一個 Windows 窗體應用程序在運行時僅能指定一個啓動窗體,設置啓動窗體的方式是在項目的 Program.cs 文件中指定。
在這裏插入圖片描述

1.7 窗體事件

在 Windows 窗體應用程序中系統已經自定義了一些事件,在窗體屬性面板中單擊閃電圖標即可查看到窗體中的事件,如下圖所示。
在這裏插入圖片描述
窗體中常用的事件如下表所示:
在這裏插入圖片描述
如果需要編寫事件的代碼,可以雙擊相應事件的左側,系統會自動生成代碼。
在這裏插入圖片描述
在窗體對應的cs文件中編寫事件,代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2 {
    public partial class Form2 : Form {
        public Form2() {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e) {
            this.BackColor = Color.Red;
        }

        private void Form2_Click(object sender, EventArgs e) {
            this.BackColor = Color.Green;
        }

        private void Form2_DoubleClick(object sender, EventArgs e) {
            this.BackColor = Color.Blue;
        }
    }
}

運行程序。可以發現,在窗口加載完畢時、鼠標單擊時、鼠標雙擊時,會呈現不同的顏色。
在這裏插入圖片描述

1.8 窗體方法

自定義的窗體都繼承自 System.Windows.Form 類,能使用 Form 類中已有的成員,包括屬性、方法、事件等。
窗體中也有一些從 System.Windows.Form 類繼承的方法,如下表所示。
在這裏插入圖片描述
以下示例演示了打開窗體、居中窗體、關閉窗體。

設置Form1爲啓動窗體。
設置Form1的鼠標單擊事件:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e) {
        	// 新建窗口對象
            Form2 form2 = new Form2();
            // 顯示窗口
            form2.Show();
        }
    }
}

設置Form2的鼠標單擊事件和鼠標雙擊事件:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2 {
    public partial class Form2 : Form {
        public Form2() {
            InitializeComponent();
        }

        private void Form2_MouseClick(object sender, MouseEventArgs e) {
            // 將窗體居中
            this.CenterToScreen();
        }

        private void Form2_MouseDoubleClick(object sender, MouseEventArgs e) {
            // 關閉窗體
            this.Close();
        }
    }
}

運行程序。此時程序打開窗體Form1,點擊鼠標左鍵,打開窗體Form2,在Form2中單擊,窗體居中,再雙擊,Form2關閉。

2. MessageBox - 消息框

通過調用 MessageBox 類的 Show() 方法,可以彈出消息框。Show() 方法的重載形式如下:
在這裏插入圖片描述
下面對參數進行介紹。

MessageBoxButtons 枚舉類型主要用於設置消息框中顯示的按鈕,具體的枚舉值如下。

  • OK:在消息框中顯示“確定”按鈕。
  • OKCancel:在消息框中顯示“確定”和“取消”按鈕。
  • AbortRetryIgnore:在消息框中顯示“中止” “重試”和“忽略”按鈕。
  • YesNoCancel:在消息框中顯示“是” “否”和“取消”按鈕。
  • YesNo:在消息框中顯示“是”和“否”按鈕。
  • RetryCancel:在消息框中顯示“重試”和“取消”按鈕。

MessageBoxIcon 枚舉類型主要用於設置消息框中顯示的圖標,具體的枚舉值如下。

  • None:在消息框中不顯示任何圖標。
  • Hand、Stop、Error:在消息框中顯示由一個紅色背景的圓圈及其中的白色X組成 的圖標。
  • Question:在消息框中顯示由圓圈和其中的一個問號組成的圖標。
  • Exclamation、Warning:在消息框中顯示由一個黃色背景的三角形及其中的一個感嘆號組成的圖標。
  • Asterisk、Information:在消息框中顯示由一個圓圈及其中的小寫字母 i 組成的圖標。

調用 MessageBox 類中的 Show 方法將返回一個 DialogResult 類型的值。
DialogResult 也是一個枚舉類型,是消息框的返回值,通過單擊消息框中不同的按鈕得到不同的消息框返回值。
DialogResult 枚舉類型的具體值如下。

  • None:消息框沒有返回值,表明有消息框繼續運行。
  • OK:消息框的返回值是 OK (通常從標籤爲“確定”的按鈕發送)。
  • Cancel:消息框的返回值是 Cancel (通常從標籤爲“取消”的按鈕發送)。
  • Abort:消息框的返回值是 Abort (通常從標籤爲“中止”的按鈕發送)。
  • Retry:消息框的返回值是 Retry (通常從標籤爲“重試”的按鈕發送)。
  • Ignore:消息框的返回值是 Ignore (通常從標籤爲“忽略“的按鈕發送)。
  • Yes:消息框的返回值是 Yes (通常從標籤爲“是“的按鈕發送)。
  • No:消息框的返回值是 No (通常從標籤爲“否“的按鈕發送)。

以下是一個示例,演示了消息框的常見用法。

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApp2 {
    public partial class MainForm : Form {
        public MainForm() {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e) {
            // 樸素消息框
            MessageBox.Show("早上好~");

            // 有標題的消息框
            MessageBox.Show("早上好~", "問好");

            // 定製按鈕的消息框
            DialogResult res = MessageBox.Show("你好馬?", "問好", MessageBoxButtons.YesNo);
            if (res == DialogResult.Yes) {
                // 只有在調試模式下可以看到輸出
                Debug.WriteLine("用戶點擊了 是 按鈕。");
            } else {
                Debug.WriteLine("用戶點擊了 否 按鈕。");
            }

            // 按鈕+圖標的消息框
            MessageBox.Show("你好馬?", "問好", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章