Presentation is loading. Please wait.

Presentation is loading. Please wait.

視窗程式設計 (Windows Programming)

Similar presentations


Presentation on theme: "視窗程式設計 (Windows Programming)"— 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; /// 清除任何使用中的資源。 /// <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"); //******************************************** }

51 UsingDialog.Dialog.cs (3/3)
private void button2_Click(object sender, EventArgs e) { //********************************* Dispose(); }

52 練習 以對話表單輸入資料,內容自定

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

54 Model-View-Controller
*D. Collins, Designing Object-Oriented User Interfaces, Benjamin/Cummings, 1995.

55 形式與功能 Function determines forms 資料處理核心與使用介面儘量分離 (Document vs. View)
使用介面較常變動 資料處理核心較為穩定

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

57 程式UsingGUI畫面

58 UsingGUI.MainForm.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 UsingGUI { public partial class MainForm : Form //********************************* Table t = new Table();

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

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

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

62 UsingGUI.Dialog.cs (2/4) public Dialog() { InitializeComponent(); }
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);

63 UsingGUI.Dialog.cs (3/4) 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"); Dispose(); //******************************************** } private void button2_Click( object sender, EventArgs e) { //******************************************** Dispose();

64 UsingGUI.Dialog.cs (4/4) //********************************************** public int[,] Content { get { return table; } }

65 UsingGUI.Output.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 UsingGUI { public partial class Output : Form public Output() InitializeComponent(); }

66 UsingGUI.Output.cs (2/3) private void Output_Load(
object sender, EventArgs e) {} private void button1_Click( object sender, EventArgs e) { //******************************************** Dispose(); } //********************************************** public void DoComputation(Table t) int[,] table = t.Content; int[] rowSum = t.RowSum(); int[] colSum = t.ColSum(); int totalSum = t.TotalSum();

67 UsingGUI.Output.cs (3/3) label5.Text = table[0, 0].ToString();
label8.Text = rowSum[0].ToString(); label9.Text = table[1, 0].ToString(); label10.Text = table[1, 1].ToString(); label11.Text = table[1, 2].ToString(); label12.Text = rowSum[1].ToString(); label13.Text = colSum[0].ToString(); label14.Text = colSum[1].ToString(); label15.Text = colSum[2].ToString(); label16.Text = totalSum.ToString(); } //************************************************ }

68 UsingGUI.Table.cs片段 public int[,] Content { get { return data; } set {
data = value; nRow = value.GetUpperBound(0) + 1; nCol = value.GetUpperBound(1) + 1; }

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

70 程式DisplayingCards畫面

71 程式DisplayingCards注意事項
檔案夾PlayingCards應放在bin資料夾內,與Debug(偵錯版)及Release(發行版)資料夾併行

72 DisplayingcCards.MainForm. Designer.cs (1/4)
//***************************** using System.Drawing; namespace DisplayingCards { partial class MainForm /// <summary> /// 設計工具所需的變數。 /// </summary> private System.ComponentModel.IContainer components = null; //******************************************* private Image image; private Graphics graphics; private bool started = false; //*******************************************

73 DisplayingcCards.MainForm. Designer.cs (2/4)
/// <summary> /// 清除任何使用中的資源。 /// </summary> /// <param name="disposing">如果應該處置 Managed 資源則為 true,否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) components.Dispose(); } base.Dispose(disposing);

74 DisplayingcCards.MainForm. Designer.cs (3/4)
//****************************************** private void DisplayImage() { int si = listBox1.SelectedIndex; string[] suit = { "s", "h", "d", "c" }; int i = listBox2.SelectedIndex + 1; string rank = i.ToString(); string fileName = "..\\PlayingCards\\" + suit[si] + rank + ".jpg"; image = Image.FromFile(fileName); graphics = CreateGraphics(); graphics.DrawImage(image, 5, 5, 85, 150); }

75 DisplayingcCards.MainForm. Designer.cs (4/4)
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { base.OnPaint(e); if (started) DisplayImage(); } //****************************************** #region Windows Form 設計工具產生的程式碼 #endregion

76 MainForm.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 DisplayingCards { public partial class MainForm : Form public MainForm() InitializeComponent(); }

77 MainForm.cs (2/2) private void MainForm_Load(object sender,
EventArgs e) { } private void button1_Click(object sender, //***************** DisplayImage();

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

79 BlackJack_0_1G 類別圖

80 互動設計:Activity Diagram

81 Form Design 開始 要牌 莊家: 18 點 玩家: 21 點 清除

82 起始處理:Collaboration Diagram

83 起始處理:Sequence Diagram

84 玩家要牌:Sequence Diagram

85 玩家停牌:Sequence Diagram

86 系統表單

87 BlackJack_0_1G.MainForm.cs (1/11)
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 BlackJack_0_1G { //*********************************** public struct PlayerInfo public string name; public Status status;

88 BlackJack_0_1G.MainForm.cs (2/11)
public int totalPoints; public Card[] cards; public int nCards; } //*********************************** public partial class MainForm : Form { //******************************* private Game game; private PlayerInfo playerInfo; private PlayerInfo dealerInfo; private Image image; private Graphics graphics; private bool inGame = false;

89 BlackJack_0_1G.MainForm.cs (3/11)
public MainForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) //************************************** game.ProcessPlayerRun(out playerInfo); ShowInfo(); CheckBlackJackOrBurst(playerInfo); CheckBlackJackOrBurst(dealerInfo);

90 BlackJack_0_1G.MainForm.cs (4/11)
//****************************************** private void ShowInfo() { int i; string fileName; graphics = CreateGraphics(); for (i = 0; i < playerInfo.nCards; ++i) fileName = "..\\PlayingCards\\" + playerInfo.cards[i].Name() + ".jpg"; image = Image.FromFile(fileName); graphics.DrawImage(image, 5+100*i, 220, 85, 150); }

91 BlackJack_0_1G.MainForm.cs (5/11)
for (i = 0; i < dealerInfo.nCards; ++i) { fileName = "..\\PlayingCards\\" + dealerInfo.cards[i].Name() + ".jpg"; image = Image.FromFile(fileName); graphics.DrawImage(image, 5+100*i, 5, 85, 150); } label1.Text = dealerInfo.totalPoints.ToString(); label2.Text = playerInfo.totalPoints.ToString(); label11.Text = dealerInfo.name; label12.Text = playerInfo.name; //******************************************

92 BlackJack_0_1G.MainForm.cs (6/11)
private void button3_Click(object sender, EventArgs e) { //***************************************** // "開始"按鈕 inGame = true; game = new Game(); game.InitPlay(out playerInfo, out dealerInfo); ShowInfo(); CheckBlackJackOrBurst(playerInfo); CheckBlackJackOrBurst(dealerInfo); button3.Enabled = false; button4.Enabled = true; }

93 BlackJack_0_1G.MainForm.cs (7/11)
private void button2_Click(object sender, EventArgs e) { //************************************** // "停"按鈕 game.ProcessDealerRun(out dealerInfo); ShowInfo(); CheckBlackJackOrBurst(playerInfo); CheckBlackJackOrBurst(dealerInfo); if (playerInfo.status == Status.PASS && dealerInfo.status == Status.PASS)

94 BlackJack_0_1G.MainForm.cs (8/11)
if (dealerInfo.totalPoints >= playerInfo.totalPoints) { MessageBox.Show(dealerInfo.name + "勝" + playerInfo.name); } else MessageBox.Show(playerInfo.name + "勝" + dealerInfo.name); //**************************************

95 BlackJack_0_1G.MainForm.cs (9/11)
private void CheckBlackJackOrBurst(PlayerInfo info) { if (info.status == Status.BLACK_JACK) MessageBox.Show(info.name+" 二十一點"); } if (info.status == Status.BURST) MessageBox.Show(info.name + " 爆!!!");

96 BlackJack_0_1G.MainForm.cs (10/11)
private void button4_Click(object sender, EventArgs e) { //************************************** // "清除"按鈕 inGame = false; Invalidate(); label1.Text = "0"; label2.Text = "0"; button4.Enabled = false; button3.Enabled = true; }

97 BlackJack_0_1G.MainForm.cs (11/11)
//****************************************** protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (inGame) ShowInfo(); }

98 BlackJack_0_1G.Game.cs (1/7)
/* * 二十一點遊戲, for GUI version * 11/21/2008 */ using System; namespace BlackJack_0_1G { class Game const int N_PLAYERS = 2; Deck deck; Player[] players = new Player[N_PLAYERS]; public Game()

99 BlackJack_0_1G.Game.cs (2/7)
players[0] = new Player("Jeng"); players[N_PLAYERS - 1] = new Dealer(); deck = new Deck(); } public void InitPlay(out PlayerInfo playerInfo, out PlayerInfo dealerInfo) { int i; // 第一輪發牌 for (i = 0; i < N_PLAYERS; ++i) players[i].SaveACard( deck.DealACard());

100 BlackJack_0_1G.Game.cs (3/7)
// 第二輪發牌 for (i = 0; i < N_PLAYERS; ++i) { players[i].SaveACard( deck.DealACard()); } playerInfo.name = players[0].Name; playerInfo.status = players[0].GetStatus(); playerInfo.totalPoints = players[0].GetTotalPoints(); playerInfo.cards = players[0].DumpCards(); playerInfo.nCards = players[0].GetNCards();

101 BlackJack_0_1G.Game.cs (4/7)
dealerInfo.name = players[N_PLAYERS - 1].Name; dealerInfo.status = players[N_PLAYERS - 1].GetStatus(); dealerInfo.totalPoints = players[N_PLAYERS - 1].GetTotalPoints(); dealerInfo.cards = players[N_PLAYERS - 1].DumpCards(); dealerInfo.nCards = players[N_PLAYERS - 1].GetNCards(); }

102 BlackJack_0_1G.Game.cs (5/7)
public void ProcessPlayerRun(out PlayerInfo playerInfo) { players[0].SaveACard(deck.DealACard()); playerInfo.name = players[0].Name; playerInfo.status = players[0].GetStatus(); playerInfo.totalPoints = players[0].GetTotalPoints(); playerInfo.cards = players[0].DumpCards(); playerInfo.nCards = players[0].GetNCards(); }

103 BlackJack_0_1G.Game.cs (6/7)
public void ProcessDealerRun(out PlayerInfo dealerInfo) { while ( players[N_PLAYERS - 1].WantOneMoreCard()) players[N_PLAYERS - 1].SaveACard( deck.DealACard()); } dealerInfo.name = players[N_PLAYERS - 1].Name; dealerInfo.status = players[N_PLAYERS - 1].GetStatus(); dealerInfo.totalPoints = players[N_PLAYERS - 1].GetTotalPoints();

104 BlackJack_0_1G.Game.cs (7/7)
dealerInfo.cards = players[N_PLAYERS - 1].DumpCards(); dealerInfo.nCards = players[N_PLAYERS - 1].GetNCards(); }

105 BlackJack_0_1G.Player.cs (1/6)
/* * 模擬玩家, for GUI version * 4/19/2009 */ using System; namespace BlackJack_0_1G { class Player // a player can have at most 11 cards // {A, A, A, A, 2, 2, 2, 2, 3, 3, 3} // for not burst or BlackJack private Card[] hand = new Card[11]; private int nCards;

106 BlackJack_0_1G.Player.cs (2/6)
private Status status; private int totalPoints; private string name; public Player() { nCards = 0; name = "無名氏"; } public Player(string name) this.name = name;

107 BlackJack_0_1G.Player.cs (3/6)
public string Name { get { return name; } } public void SaveACard(Card card) hand[nCards++] = card; StatusChecker.DetermineStatusAndTotalPoints(hand, nCards, out status, out totalPoints); public Status GetStatus() return status;

108 BlackJack_0_1G.Player.cs (4/6)
public int GetTotalPoints() { return totalPoints; } virtual public bool WantOneMoreCard() Console.Write("要再一張牌嗎? (y/n) "); string answer = Console.ReadLine(); return (answer == "Y" || answer == "y"); public void Dump() int i; Console.Write(name + " 牌: ");

109 BlackJack_0_1G.Player.cs (5/6)
for (i = 0; i < nCards; ++i) { hand[i].Dump(); Console.Write("\t"); if ((i + 1) % 5 == 0) Console.WriteLine(); } Console.WriteLine(name + " 總點數: " + totalPoints); public Card[] DumpCards() return hand;

110 BlackJack_0_1G.Player.cs (6/6)
public int GetNCards() { return nCards; }


Download ppt "視窗程式設計 (Windows Programming)"

Similar presentations


Ads by Google