1 / 110

視窗程式設計 (Windows Programming)

視窗程式設計 (Windows Programming). 鄭士康 國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所. 綱要. 第一個視窗程式 工具箱與控制項 訊息盒 事件處理 標籤與按鈕 選單 對話表單 MVC: 模型 - 呈現 - 控制器原理. 綱要. 改寫主控台程式為視窗程式 加入圖形影像 二十一點模擬程式 0.1G 版. 綱要. 第一個視窗程式 工具箱與控制項 訊息盒 事件處理 標籤與按鈕 選單 對話表單 MVC: 模型 - 呈現 - 控制器原理. 第一個 C# 視窗程式.

sotomichael
Download Presentation

視窗程式設計 (Windows Programming)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 視窗程式設計(Windows Programming) 鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所

  2. 綱要 • 第一個視窗程式 • 工具箱與控制項 • 訊息盒 • 事件處理 • 標籤與按鈕 • 選單 • 對話表單 • MVC:模型-呈現-控制器原理

  3. 綱要 • 改寫主控台程式為視窗程式 • 加入圖形影像 • 二十一點模擬程式0.1G版

  4. 綱要 • 第一個視窗程式 • 工具箱與控制項 • 訊息盒 • 事件處理 • 標籤與按鈕 • 選單 • 對話表單 • MVC:模型-呈現-控制器原理

  5. 第一個C#視窗程式 • 新增專案/名稱 (WindowsForm應用程式) • Form1.cs[設計]/屬性頁 • 建置方案/啟動但不偵錯 • 方案總管/Program.cs • 方案總管/Form1.cs/Form1.Designer.cs • 重新命名

  6. Form

  7. Form 屬性

  8. WindowsFormsApplication1.Program.cs (1/2) using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace WindowsFormsApplication1 { static class Program { /// <summary> /// 應用程式的主要進入點。 /// </summary> [STAThread]

  9. WindowsFormsApplication1.Program.cs (2/2) static void Main() { Application.EnableVisualStyles(); Application. SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }

  10. WindowsFormsApplication1.Form1.Designer.cs (1/3) namespace WindowsFormsApplication1 { partial class Form1 { /// <summary> /// 設計工具所需的變數。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清除任何使用中的資源。 /// </summary> /// <param name="disposing">如果應該處置 Managed 資源則為 true,否則為 false。</param>

  11. WindowsFormsApplication1.Form1.Designer.cs (2/3) protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form 設計工具產生的程式碼 /// <summary> /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。 /// /// </summary>

  12. WindowsFormsApplication1.Form1.Designer.cs (3/3) private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Text = "Form1"; } #endregion } }

  13. 練習 • 產生一個視窗程式,表單類別名為MainForm,表單標題為Hello,嘗試改變其大小

  14. 綱要 • 第一個視窗程式 • 工具箱與控制項 • 訊息盒 • 事件處理 • 標籤與按鈕 • 選單 • 對話表單 • MVC:模型-呈現-控制器原理

  15. 工具箱 • 檢視/工具箱 • 通用控制項 • Button • CheckBox • Label • ProgressBar • etc.

  16. 練習 • 產生一個視窗程式,嘗試加入一些通用控制項

  17. 綱要 • 第一個視窗程式 • 工具箱與控制項 • 訊息盒 • 事件處理 • 標籤與按鈕 • 選單 • 對話表單 • MVC:模型-呈現-控制器原理

  18. 程式UsingMessageBox畫面

  19. UsingMessageBox.Program.cs using System; using System.Collections.Generic; using System.Windows.Forms; namespace UsingMessageBox{ static class Program{ static void Main(){ Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); //******************************************* MessageBox.Show("Main form has been closed"); //******************************************* } } }

  20. 綱要 • 第一個視窗程式 • 工具箱與控制項 • 訊息盒 • 事件處理 • 標籤與按鈕 • 選單 • 對話表單 • MVC:模型-呈現-控制器原理

  21. 視窗程式執行流程 程式進入 程式初始化 事件發生 等待狀態 事件處理 事件處理結束 程式關閉 資源釋放 程式離開

  22. 事件處理

  23. 程式HandlingEvents表單輸出

  24. HandlingEvents.MainForm.cs (1/2) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace HandlingEvents { public partial class MainForm : Form { public MainForm() { InitializeComponent(); }

  25. HandlingEvents.MainForm.cs (2/2) private void MainForm_Click(object sender, EventArgs e) { //******************************** MessageBox.Show( "滑鼠剛剛點擊" ); //******************************** } } }

  26. HandlingEvents.MainForm.Designer.cs片段 (1/2) #region Windows Form 設計工具產生的程式碼 /// <summary> /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改 /// 這個方法的內容。 /// /// </summary> private void InitializeComponent() { this.SuspendLayout(); // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

  27. HandlingEvents.MainForm.Designer.cs片段 (2/2) this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size( 292, 266); this.Name = "MainForm"; this.Text = "MainForm"; this.Click += new System.EventHandler(this.MainForm_Click); this.ResumeLayout(false); } #endregion

  28. 練習 • 產生一個視窗程式,每次滑鼠雙擊,即顯示訊息 • 修改程式,使能累計滑鼠雙擊次數,並顯示於訊息盒

  29. 綱要 • 第一個視窗程式 • 工具箱與控制項 • 訊息盒 • 事件處理 • 標籤與按鈕 • 選單 • 對話表單 • MVC:模型-呈現-控制器原理

  30. 程式UsingLabels畫面

  31. 基本標籤

  32. 標籤點擊事件處理(1/2) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace UsingLabels { public partial class MainForm : Form { public MainForm() { InitializeComponent(); }

  33. 標籤點擊事件處理(2/2) private void label1_Click(object sender, EventArgs e) { //************************ label1.Text = "程式可關閉"; //************************ } } }

  34. 程式UsingButtons畫面

  35. 按鈕

  36. 按鈕點擊事件處理(1/3) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace UsingButtons { public partial class MainForm : Form { public MainForm() { InitializeComponent(); }

  37. 按鈕點擊事件處理(2/3) private void button1_Click( object sender, EventArgs e) { //********************************* if (button1.Text == "是(&Y)") { label1.Text = "檔案已刪除"; button1.Text = "確定"; button2.Visible = false; } else Dispose(true); //********************************* }

  38. 按鈕點擊事件處理(3/3) private void button2_Click( object sender, EventArgs e) { //********************************* Dispose(true); //********************************* } } }

  39. 練習 • 撰寫應用標籤與按鈕的視窗程式,內容自由發揮

  40. 綱要 • 第一個視窗程式 • 工具箱與控制項 • 訊息盒 • 事件處理 • 標籤與按鈕 • 選單 • 對話表單 • MVC:模型-呈現-控制器原理

  41. 程式UsingMenuStrip畫面

  42. 主選單與選項

  43. 練習 • 設定主選單及選項,內容自定

  44. 綱要 • 第一個視窗程式 • 工具箱與控制項 • 訊息盒 • 事件處理 • 標籤與按鈕 • 選單 • 對話表單 • MVC:模型-呈現-控制器原理

  45. 程式UsingDialogForm畫面

  46. 對話表單設計 • 專案/加入新項目/Windows Form • Label/TextBox/Button • TextBox屬性(Text, TextAlign)及Button行為調整設定

  47. UsingDialog.MainForm.cs (1/2) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace UsingDialogForm { public partial class MainForm : Form { public MainForm() { InitializeComponent(); }

  48. UsingDialog.MainForm.cs (2/2) private void 輸入表格ToolStripMenuItem_Click(object sender, EventArgs e) { //********************************* Dialog diag = new Dialog(); diag.ShowDialog(); //********************************* } } }

  49. UsingDialog.Dialog.cs (1/3) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace UsingDialogForm { public partial class Dialog : Form { //*********************************** int[ , ] table = new int[2, 3]; //*********************************** public Dialog() { InitializeComponent(); }

  50. UsingDialog.Dialog.cs (2/3) private void button1_Click(object sender, EventArgs e){ //******************************************* table[0, 0] = Convert.ToInt32(textBox1.Text); table[0, 1] = Convert.ToInt32(textBox2.Text); table[0, 2] = Convert.ToInt32(textBox3.Text); table[1, 0] = Convert.ToInt32(textBox4.Text); table[1, 1] = Convert.ToInt32(textBox5.Text); table[1, 2] = Convert.ToInt32(textBox6.Text); MessageBox.Show(table[0, 0].ToString()+ "\t" + table[0, 1].ToString()+ "\t" + table[0, 2].ToString()+ "\n" + table[1, 0].ToString()+ "\t" + table[1, 1].ToString()+ "\t" + table[1, 2].ToString() + "\n"); //******************************************** }

More Related