1.19k likes | 1.32k Views
基礎視窗程式設計 (Windows Programming). 鄭士康 國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所. 綱要. 第一個視窗程式 訊息盒 工具箱與控制項 事件處理 選單 對話表單 MVC: 模型 - 呈現 - 控制器架構 加入圖形影像. 綱要. 物件導向二十一點模擬程式 0.1G 版 繪圖工具類別 Graphics. 綱要. 第一個視窗程式 訊息盒 工具箱與控制項 事件處理 選單 對話表單 MVC: 模型 - 呈現 - 控制器架構 加入圖形影像. 第一個 C# 視窗程式.
E N D
基礎視窗程式設計(Windows Programming) 鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所
綱要 • 第一個視窗程式 • 訊息盒 • 工具箱與控制項 • 事件處理 • 選單 • 對話表單 • MVC:模型-呈現-控制器架構 • 加入圖形影像
綱要 • 物件導向二十一點模擬程式0.1G版 • 繪圖工具類別Graphics
綱要 • 第一個視窗程式 • 訊息盒 • 工具箱與控制項 • 事件處理 • 選單 • 對話表單 • MVC:模型-呈現-控制器架構 • 加入圖形影像
第一個C#視窗程式 • 新增專案/名稱 (WindowsForm應用程式) • Form1.cs[設計]/屬性頁 • 建置方案/啟動但不偵錯 • 方案總管/Program.cs • 方案總管/Form1.cs/Form1.Designer.cs • 重新命名
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]
WindowsFormsApplication1.Program.cs (2/2) static void Main() { Application.EnableVisualStyles(); Application. SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
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>
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>
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 } }
練習 • 產生一個視窗程式,表單類別名為MainForm,表單標題為Hello,嘗試改變其大小
綱要 • 第一個視窗程式 • 訊息盒 • 工具箱與控制項 • 事件處理 • 選單 • 對話表單 • MVC:模型-呈現-控制器架構 • 加入圖形影像
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"); //******************************************* } } }
綱要 • 第一個視窗程式 • 訊息盒 • 工具箱與控制項 • 事件處理 • 選單 • 對話表單 • MVC:模型-呈現-控制器架構 • 加入圖形影像
工具箱 • 檢視/工具箱 • 通用控制項 • Button • CheckBox • Label • ProgressBar • etc.
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(); }
UsingControls.Form1.cs (2/2) private void button1_Click(object sender, EventArgs e) { //************************************** MessageBox.Show("Hello!"+textBox1.Text); //************************************** } } }
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);
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; . . . }
練習 • 產生一個視窗程式,嘗試加入一些通用控制項
綱要 • 第一個視窗程式 • 訊息盒 • 工具箱與控制項 • 事件處理 • 選單 • 對話表單 • MVC:模型-呈現-控制器架構 • 加入圖形影像
視窗程式執行流程 建立視窗表單物件 進入函式Run 事件發生 要求視窗物件處理事件 進入等待狀態 事件處理完畢 視窗表單關閉 釋放資源 離開函式Run
綱要 • 第一個視窗程式 • 訊息盒 • 工具箱與控制項 • 事件處理 • 選單 • 對話表單 • MVC:模型-呈現-控制器架構 • 加入圖形影像
UsingMenuStrip.MainForm.cs部份程式碼 public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void 輸入表格ToolStripMenuItem_Click( object sender, EventArgs e) { //*********************************** 計算ToolStripMenuItem.Enabled = true; //*********************************** } }
綱要 • 第一個視窗程式 • 訊息盒 • 工具箱與控制項 • 事件處理 • 選單 • 對話表單 • MVC:模型-呈現-控制器架構 • 加入圖形影像
對話表單設計 • 專案/加入新項目/Windows Form • Label/TextBox/Button • TextBox屬性(Text, TextAlign)及Button行為調整設定
輸出表單設計 • 專案/加入新項目/Windows Form • 工具箱/RichTextBox
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 {
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; //************************************ }
UsingDialogForm.MainForm.cs (3/3) private void 計算ToolStripMenuItem_Click(object sender, EventArgs e) { //********************************* Output output = new Output(); output.DoComputation(t); output.ShowDialog(); //********************************* } } }
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(); }
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"); //******************************************** }
UsingDialogForm.Dialog.cs (3/3) private void button2_Click(object sender, EventArgs e) { //********************************* Dispose(); //********************************* } //********************************************** public int[,] Content { get { return table; } } //********************************************** } }
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(); }
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"+
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"; }
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; }
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; }