1 / 119

基礎視窗程式設計 (Windows Programming)

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

ebony-bowen
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版 • 繪圖工具類別Graphics

  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. 程式UsingMessageBox畫面

  16. 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"); //******************************************* } } }

  17. 綱要 • 第一個視窗程式 • 訊息盒 • 工具箱與控制項 • 事件處理 • 選單 • 對話表單 • MVC:模型-呈現-控制器架構 • 加入圖形影像

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

  19. 程式UsingControls畫面

  20. 控制項屬性視窗「事件」頁

  21. UsingControls.Form1.cs (1/2) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace UsingControls { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

  22. UsingControls.Form1.cs (2/2) private void button1_Click(object sender, EventArgs e) { //************************************** MessageBox.Show("Hello!"+textBox1.Text); //************************************** } } }

  23. UsingControls.Form1.Designer.cs部份程式碼 (1/2) private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(166, 192); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size( 75, 23);

  24. UsingControls.Form1.Designer.cs部份程式碼 (2/2) this.button1.TabIndex = 0; this.button1.Text = "確定"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(136, 55); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size( 105, 22); this.textBox1.TabIndex = 1; . . . }

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

  26. 綱要 • 第一個視窗程式 • 訊息盒 • 工具箱與控制項 • 事件處理 • 選單 • 對話表單 • MVC:模型-呈現-控制器架構 • 加入圖形影像

  27. 視窗程式執行流程 建立視窗表單物件 進入函式Run 事件發生 要求視窗物件處理事件 進入等待狀態 事件處理完畢 視窗表單關閉 釋放資源 離開函式Run

  28. 綱要 • 第一個視窗程式 • 訊息盒 • 工具箱與控制項 • 事件處理 • 選單 • 對話表單 • MVC:模型-呈現-控制器架構 • 加入圖形影像

  29. 程式UsingMenuStrip選單結構

  30. 程式UsingMenuStrip選單建立

  31. UsingMenuStrip.MainForm.cs部份程式碼 public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void 輸入表格ToolStripMenuItem_Click( object sender, EventArgs e) { //*********************************** 計算ToolStripMenuItem.Enabled = true; //*********************************** } }

  32. 程式UsingMenuStrip畫面

  33. 綱要 • 第一個視窗程式 • 訊息盒 • 工具箱與控制項 • 事件處理 • 選單 • 對話表單 • MVC:模型-呈現-控制器架構 • 加入圖形影像

  34. 程式UsingDialogForm畫面

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

  36. 輸出表單設計 • 專案/加入新項目/Windows Form • 工具箱/RichTextBox

  37. 程式UsingDialogForm結構

  38. 程式UsingDialogForm「輸入表格」順序圖

  39. 程式UsingDialogForm「計算」順序圖

  40. UsingDialogForm.MainForm.cs (1/3) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace UsingDialogForm { public partial class MainForm : Form {

  41. UsingDialogForm.MainForm.cs (2/3) //********************************* int[,] t; //********************************* public MainForm() { InitializeComponent(); } private void 輸入表格ToolStripMenuItem_Click(object sender, EventArgs e) { //************************************ Dialog diag = new Dialog(); diag.ShowDialog(); t = diag.Content; 計算ToolStripMenuItem.Enabled = true; //************************************ }

  42. UsingDialogForm.MainForm.cs (3/3) private void 計算ToolStripMenuItem_Click(object sender, EventArgs e) { //********************************* Output output = new Output(); output.DoComputation(t); output.ShowDialog(); //********************************* } } }

  43. UsingDialogForm.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(); }

  44. UsingDialogForm.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"); //******************************************** }

  45. UsingDialogForm.Dialog.cs (3/3) private void button2_Click(object sender, EventArgs e) { //********************************* Dispose(); //********************************* } //********************************************** public int[,] Content { get { return table; } } //********************************************** } }

  46. UsingDialogForm.Output.cs (1/6) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace UsingDialogForm { public partial class Output : Form { public Output() { InitializeComponent(); }

  47. UsingDialogForm.Output.cs (2/6) //********************************************* public void DoComputation(int[,] t) { int[,] table = t; int[] rowSum = RowSum(t); int[] colSum = ColSum(t); int totalSum = TotalSum(t); richTextBox1.Text = "表格統計結果\n" + " \t1\t2\t3\t總和\n" + "1\t"+table[0,0].ToString()+"\t"+ table[0,1].ToString()+"\t table[0,2].ToString()+"\t"+ rowSum[0].ToString()+"\n"+

  48. UsingDialogForm.Output.cs (3/6) "2\t"+table[1,0].ToString()+"\t"+ table[1,1].ToString()+"\t"+ table[1,2].ToString()+"\t"+ rowSum[1].ToString()+"\n"+ "總和\t"+colSum[0].ToString()+"\t"+ colSum[1].ToString()+"\t"+ colSum[2].ToString()+"\t"+ totalSum.ToString()+"\n"; }

  49. UsingDialogForm.Output.cs (4/6) private int[] RowSum(int[,] data) { int nRow = data.GetUpperBound(0) + 1; int nCol = data.GetUpperBound(1) + 1; int[] rowSum = new int[nRow]; for (int i = 0; i < nRow; ++i) { rowSum[i] = 0; for (int j = 0; j < nCol; ++j) { rowSum[i] += data[i, j]; } } return rowSum; }

  50. UsingDialogForm.Output.cs (5/6) private int[] ColSum(int[,] data) { int nRow = data.GetUpperBound(0) + 1; int nCol = data.GetUpperBound(1) + 1; int[] colSum = new int[nCol]; for (int j = 0; j < nCol; ++j) { colSum[j] = 0; for (int i = 0; i < nRow; ++i) { colSum[j] += data[i, j]; } } return colSum; }

More Related