導讀
1、什么是 Windows Forms
2、需要學Windows Forms 么?
3、如何手寫一個簡單的Windows Forms 程序
4、對上面程序的說明
5、Form 類與Control類
6、Windows Forms 中的事件處理及手寫一個帶鼠標移動事件的窗體
什么是Windows Forms
通常我們的說的Windows Forms 指的是一類GUI(圖形用戶界面)程序的統稱,Windows Forms 是隨着 .NET 1.0 一起發布的,但市面上的 Windows Forms 程序主要還是 .NET 2.0 以上版本的,.NET 3.5 后主推的GUI程序變為 WPF,Windows Forms 退居2線。
需要學Windows Forms 么?
這個問題的答案因人而異,如果對於學生而言,我建議直接跳過Windows Forms 學WPF,WPF的思維模式比Windows Forms的事件處理模式要優秀不少。對於找工作(或已工作)的程序員而言,這個就要取決於公司的需求,在很多做行業軟件的公司,用Windows Form 比 WPF多,雖然WPF的界面比原生Windows Forms炫太多了,但是Windows Forms 第三方的控件庫彌補了這個問題。
如何手寫一個簡單的Windows Forms 程序
直接上代碼: FirstWinForm.cs
using System; using System.Windows.Forms; namespace DemoWinForm { class App : Form { static void Main() { Application.Run(new App()); } } }
編譯命令: csc /t:winexe FirstWinForm.cs
運行 效果
可以看到一個簡單得不能再簡單的Windows Form 程序跑起來了。
對上面程序的說明
從上面的代碼可以看出,Windows Forms 是一個類(其實應該是Windows Forms 窗體類是一個類),一個類要運行得有一個 Main 方法,上面Main方法中有一個 Application.Run(new App()); 這句話貌似是讓 Windows Forms 跑起來的語句(Run)。再看引用,System.Windows.Forms 從這命名上看這應該是 Windows Forms的程序集。好下面我們正式揭開謎底
Windows 窗體 是任何繼承 Form 的類叫窗體
System.Windows.Forms 命名空間包含了Windows Forms 程序的核心內容,它包括Windows Forms 的核心架構、可視化控件(設計時可見且運行時也可見)、組件(有設計時和運行是都可見的組件如ToolTip和僅設計時可見運行時不可見的組件如Timer)、對話框(公共對話框如OpenFileDialog)
System.Windows.Forms 命名空間中的核心類型
1、Application 該類封裝了Windows 窗體應用程序運行時操作
2、常用控件類(Button、ComboBox、CheckBox、TextBox、Label、DateTimePicker、ListBox、PictureBox、TreeView)
3、Form 窗體類
4、布局控件類(Panel、Splitter 、GroupBox、TabControl)
5、菜單控件 (Menu、MenuItem 、ContextMenu)
6、各種對話框
System.Windows.Forms.Application 類需關注的方法
Run() 運行Windows Forms 窗體
DoEvents() 提供應用程序在冗長的操作期間處理當前排在消息隊列里的信息的能力
Exit() 終止窗口應用程序並從承載的應用程序域中卸載
EnableVisualStyles() 配置應用程序以支持 Windows XP 界面外觀
現在我們分析下上面的程序:
1、上面的代碼寫的是一個可執行程序(exe),所以它有一個Main方法。
2、App類是一個窗體類,因為它繼承了 System.Windows.Forms.Form類
3、通過 Application.Run(Windows 窗體實例); 運行得到我們看到的Windows Forms 程序
上面的代碼耦合度太強了,應用程序的運行(創建AppDomian)和窗體邏輯耦合到一起(說白了窗體類應該只關注窗體邏輯,不關注誰來加載這個窗體到AppDomain中運行)所以上面的代碼最好改為如下的代碼:
using System; using System.Windows.Forms; namespace DemoWinForm { // 注意這里我用靜態類 static class App { static void Main() { Application.Run(new MainForm()); } } public class MainForm : Form{} }
Form 類與Control類
我們先看下 System.Windows.Forms.Form 的繼承關系圖
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ScrollableControl
System.Windows.Forms.ContainerControl
System.Windows.Forms.Form
標紅的是我認為需要重點關注的類,所有可視化控件(如 Button 之類)都是繼承自 System.Windows.Forms.Control,而組件則是繼承自 System.ComponentModel.Component。
System.Windows.Forms.Control 類提供了一個可視化控件的絕大多數成員(控件名、Size、字體、前景和背景色、父容器、常用事件如鼠標相關、鍵盤相關等)詳細內容參見MSDN
使用Control類的例子:
using System; using System.Windows.Forms; using System.Drawing; namespace DemoWinForm { static class App { static void Main() { Application.Run(new MainForm()); } } public class MainForm : Form { public MainForm() { Text = "一個窗口"; Height = 300; Width = 500; BackColor = Color.Green; Cursor = Cursors.Hand; } } }
編譯 csc /t:winexe UseControlDemo.cs
Windows Forms 中的事件處理及手寫一個帶鼠標移動事件的窗體
System.EventHandler
原型為
public delegate void EventHandler(object sender,EventArgs e);
它是 WinForm程序事件處理的最原始委托,sender 表示發送事件的對象,e 表示事件相關信息
Windows Forms 事件的相關委托定義都類似與System.EventHandler,如鼠標相關的委托MouseEventHandler,它的原型如下
public delegate void MouseEventHandler(object sender,MouseEventArgs e)
我們看下 MouseMove事件
所有與鼠標相關的事件(MouseMove MouseUp 等)與MouseEventHandler 委托結合工作,MouseEventArgs 擴展了EventArgs增加了一下屬性
Button 獲取哪個鼠標被單擊
Clicks 獲取鼠標被按下和釋放的次數
X 鼠標單擊處的 x 坐標
Y 鼠標單擊處的 y 坐標
看代碼
using System; using System.Windows.Forms; using System.Drawing; namespace DemoWinForm { static class App { static void Main() { Application.Run(new MainForm()); } } public class MainForm : Form { public MainForm() { this.Text = "一個窗口"; this.Height = 300; this.Width = 500; BackColor = Color.Green; Cursor = Cursors.Hand; this.MouseMove += new MouseEventHandler(MainForm_MouseMove); } void MainForm_MouseMove(object sender, MouseEventArgs e) { this.Text = string.Format("當前鼠標坐標: [{0},{1}]",e.X,e.Y); } } }
編譯 csc /t:winexe MouseMoveDemo.cs
運行效果
本文完