视窗程式设计讲解课件_第1页
视窗程式设计讲解课件_第2页
视窗程式设计讲解课件_第3页
视窗程式设计讲解课件_第4页
视窗程式设计讲解课件_第5页
已阅读5页,还剩106页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、视窗程式设计讲解视窗程式设计讲解綱要第一個視窗程式工具箱與控制項訊息盒事件處理標籤與按鈕選單對話表單MVC:模型-呈現-控制器原理2綱要第一個視窗程式4綱要改寫主控台程式為視窗程式加入圖形影像二十一點模擬程式0.1G版3綱要改寫主控台程式為視窗程式5綱要第一個視窗程式工具箱與控制項訊息盒事件處理標籤與按鈕選單對話表單MVC:模型-呈現-控制器原理4綱要第一個視窗程式6第一個C#視窗程式新增專案/名稱 (WindowsForm應用程式)Form1.cs設計/屬性頁建置方案/啟動但不偵錯方案總管/Program.cs方案總管/Form1.cs/Form1.Designer.cs重新命名5第一個C#

2、視窗程式新增專案/名稱 (WindowsForm應Form6Form8Form 屬性7Form 屬性9WindowsFormsApplication1.Program.cs (1/2)using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace WindowsFormsApplication1 static class Program / / 應用程式的主要進入點。 / STAThread 8WindowsFormsApplication1.ProgWin

3、dowsFormsApplication1.Program.cs (2/2) static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(); 9WindowsFormsApplication1.ProgWindowsFormsApplication1.Form1.Designer.cs (1/3)namespace WindowsFormsApplication1 partial clas

4、s Form1 / / 設計工具所需的變數。 / private System.ComponentModel.IContainer components = null; / / 清除任何使用中的資源。 / / 如果應該處置 Managed 資源則為 true,否則為 false。 10WindowsFormsApplication1.Form1WindowsFormsApplication1.Form1.Designer.cs (2/3) protected override void Dispose(bool disposing) if (disposing & (components !=

5、 null) components.Dispose(); base.Dispose(disposing); #region Windows Form 設計工具產生的程式碼 / / 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。 / / 11WindowsFormsApplication1.Form1WindowsFormsApplication1.Form1.Designer.cs (3/3) private void InitializeComponent() ponents = new System.ComponentModel.Container(); this

6、.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Text = Form1; #endregion 12WindowsFormsApplication1.Form1練習產生一個視窗程式,表單類別名為MainForm,表單標題為Hello,嘗試改變其大小13練習產生一個視窗程式,表單類別名為MainForm,表單標題綱要第一個視窗程式工具箱與控制項訊息盒事件處理標籤與按鈕選單對話表單MVC:模型-呈現-控制器原理14綱要第一個視窗程式16工具箱檢視/工具箱通用控制項ButtonCheckBoxLabelProgressB

7、aretc.15工具箱檢視/工具箱17練習產生一個視窗程式,嘗試加入一些通用控制項16練習產生一個視窗程式,嘗試加入一些通用控制項18綱要第一個視窗程式工具箱與控制項訊息盒事件處理標籤與按鈕選單對話表單MVC:模型-呈現-控制器原理17綱要第一個視窗程式19程式UsingMessageBox畫面18程式UsingMessageBox畫面20UsingMessageBox.Program.csusing System;using System.Collections.Generic;using System.Windows.Forms;namespace UsingMessageBox stat

8、ic class Program static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm(); /* MessageBox.Show(Main form has been closed); /* 19UsingMessageBox.Program.csusin綱要第一個視窗程式工具箱與控制項訊息盒事件處理標籤與按鈕選單對話表單MVC:模型-呈現-控制器原理20綱要第一個視窗程式22

9、視窗程式執行流程程式進入程式初始化等待狀態事件處理資源釋放程式離開事件發生事件處理結束程式關閉21視窗程式執行流程程式進入程式初始化等待狀態事件處理資源釋放程事件處理22事件處理24程式HandlingEvents表單輸出23程式HandlingEvents表單輸出25HandlingEvents.MainForm.cs (1/2)using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;us

10、ing System.Windows.Forms;namespace HandlingEvents public partial class MainForm : Form public MainForm() InitializeComponent(); 24HandlingEvents.MainForm.cs (1/HandlingEvents.MainForm.cs (2/2) private void MainForm_Click(object sender, EventArgs e) /* MessageBox.Show( 滑鼠剛剛點擊 ); /* 25HandlingEvents.M

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

12、igHandlingEvents.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

13、27HandlingEvents.MainForm.Desig練習產生一個視窗程式,每次滑鼠雙擊,即顯示訊息修改程式,使能累計滑鼠雙擊次數,並顯示於訊息盒28練習產生一個視窗程式,每次滑鼠雙擊,即顯示訊息30綱要第一個視窗程式工具箱與控制項訊息盒事件處理標籤與按鈕選單對話表單MVC:模型-呈現-控制器原理29綱要第一個視窗程式31程式UsingLabels畫面30程式UsingLabels畫面32基本標籤31基本標籤33標籤點擊事件處理(1/2)using System;using System.Collections.Generic;using System.ComponentModel;u

14、sing System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace UsingLabels public partial class MainForm : Form public MainForm() InitializeComponent(); 32標籤點擊事件處理(1/2)using System;34標籤點擊事件處理(2/2) private void label1_Click(object sender, EventArgs e) /* label1.Text = 程式

15、可關閉;/* 33標籤點擊事件處理(2/2) private void l程式UsingButtons畫面34程式UsingButtons畫面36按鈕35按鈕37按鈕點擊事件處理(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 Main

16、Form : Form public MainForm() InitializeComponent(); 36按鈕點擊事件處理(1/3)using System;38按鈕點擊事件處理(2/3) private void button1_Click( object sender, EventArgs e) /* if (button1.Text = 是(&Y) label1.Text = 檔案已刪除; button1.Text = 確定; button2.Visible = false; else Dispose(true); /* 37按鈕點擊事件處理(2/3) private void b按

17、鈕點擊事件處理(3/3) private void button2_Click(object sender, EventArgs e) /* Dispose(true); /* 38按鈕點擊事件處理(3/3) private void b練習撰寫應用標籤與按鈕的視窗程式,內容自由發揮39練習撰寫應用標籤與按鈕的視窗程式,內容自由發揮41綱要第一個視窗程式工具箱與控制項訊息盒事件處理標籤與按鈕選單對話表單MVC:模型-呈現-控制器原理40綱要第一個視窗程式42程式UsingMenuStrip畫面41程式UsingMenuStrip畫面43主選單與選項42主選單與選項44練習設定主選單及選項,內容

18、自定43練習設定主選單及選項,內容自定45綱要第一個視窗程式工具箱與控制項訊息盒事件處理標籤與按鈕選單對話表單MVC:模型-呈現-控制器原理44綱要第一個視窗程式46程式UsingDialogForm畫面45程式UsingDialogForm畫面47對話表單設計專案/加入新項目/Windows FormLabel/TextBox/ButtonTextBox屬性(Text, TextAlign)及Button行為調整設定46對話表單設計專案/加入新項目/Windows Form48UsingDialog.MainForm.cs (1/2)using System;using System.Col

19、lections.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(); 47UsingDialog.MainForm.cs (1/2)uUsingDialog.MainForm.cs (2/2) priva

20、te void 輸入表格ToolStripMenuItem_Click(object sender, EventArgs e) /* Dialog diag = new Dialog(); diag.ShowDialog(); /* 48UsingDialog.MainForm.cs (2/2)UsingDialog.Dialog.cs (1/3)using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System

21、.Text;using System.Windows.Forms;namespace UsingDialogForm public partial class Dialog : Form /* int , table = new int2, 3; /* public Dialog() InitializeComponent(); 49UsingDialog.Dialog.cs (1/3)usiUsingDialog.Dialog.cs (2/3) private void button1_Click(object sender, EventArgs e) /* table0, 0 = Conv

22、ert.ToInt32(textBox1.Text); table0, 1 = Convert.ToInt32(textBox2.Text); table0, 2 = Convert.ToInt32(textBox3.Text); table1, 0 = Convert.ToInt32(textBox4.Text); table1, 1 = Convert.ToInt32(textBox5.Text); table1, 2 = Convert.ToInt32(textBox6.Text); MessageBox.Show(table0, 0.ToString()+ t + table0, 1.

23、ToString()+ t + table0, 2.ToString()+ n + table1, 0.ToString()+ t + table1, 1.ToString()+ t + table1, 2.ToString() + n);/* 50UsingDialog.Dialog.cs (2/3) UsingDialog.Dialog.cs (3/3) private void button2_Click(object sender, EventArgs e) /* Dispose(); /* 51UsingDialog.Dialog.cs (3/3) 練習以對話表單輸入資料,內容自定5

24、2練習以對話表單輸入資料,內容自定54綱要第一個視窗程式工具箱與控制項訊息盒事件處理標籤與按鈕選單對話表單MVC:模型-呈現-控制器原理53綱要第一個視窗程式55Model-View-Controller*D. Collins, Designing Object-Oriented User Interfaces, Benjamin/Cummings, 1995.54Model-View-Controller*D. Colli形式與功能Function determines forms資料處理核心與使用介面儘量分離 (Document vs. View)使用介面較常變動資料處理核心較為穩定55形

25、式與功能Function determines forms綱要改寫主控台程式為視窗程式加入圖形影像二十一點模擬程式0.1G版56綱要改寫主控台程式為視窗程式58程式UsingGUI畫面57程式UsingGUI畫面59UsingGUI.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;namespac

26、e UsingGUI public partial class MainForm : Form /* Table t = new Table(); /* 58UsingGUI.MainForm.cs (1/3)usinUsingGUI.MainForm.cs (2/3) public MainForm() InitializeComponent(); private void 輸入表格ToolStripMenuItem_Click(object sender, EventArgs e) /* Dialog diag = new Dialog(); diag.ShowDialog(); t.Co

27、ntent = diag.Content; 計算ToolStripMenuItem.Enabled = true; /* 59UsingGUI.MainForm.cs (2/3) pUsingGUI.MainForm.cs (3/3) private void 計算ToolStripMenuItem_Click(object sender, EventArgs e) /* Output output = new Output(); output.DoComputation(t); output.ShowDialog(); /* 60UsingGUI.MainForm.cs (3/3) pUsi

28、ngGUI.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 int2, 3; /*61UsingGUI.Dialog.cs (1/4)using Usi

29、ngGUI.Dialog.cs (2/4) public Dialog() InitializeComponent(); private void button1_Click(object sender, EventArgs e) /* table0, 0 = Convert.ToInt32(textBox1.Text); table0, 1 = Convert.ToInt32(textBox2.Text); table0, 2 = Convert.ToInt32(textBox3.Text); table1, 0 = Convert.ToInt32(textBox4.Text); table

30、1, 1 = Convert.ToInt32(textBox5.Text); table1, 2 = Convert.ToInt32(textBox6.Text);62UsingGUI.Dialog.cs (2/4) pubUsingGUI.Dialog.cs (3/4)MessageBox.Show(table0, 0.ToString()+ t + table0, 1.ToString()+ t + table0, 2.ToString()+ n + table1, 0.ToString()+ t + table1, 1.ToString()+ t + table1, 2.ToString

31、()+ n); Dispose(); /* private void button2_Click(object sender, EventArgs e) /* Dispose(); /* 63UsingGUI.Dialog.cs (3/4)MessUsingGUI.Dialog.cs (4/4) /* public int, Content get return table; /* 64UsingGUI.Dialog.cs (4/4) /*UsingGUI.Output.cs (1/3)using System;using System.Collections.Generic;using Sy

32、stem.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace UsingGUI public partial class Output : Form public Output() InitializeComponent(); 65UsingGUI.Output.cs (1/3)using UsingGUI.Output.cs (2/3) private void Output_Load(object sender, EventA

33、rgs 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(); 66UsingGUI.Output.cs (2/3) priUsingGUI.Output.cs (3/3) label5.Text = table0, 0.T

34、oString(); label6.Text = table0, 1.ToString(); label7.Text = table0, 2.ToString(); label8.Text = rowSum0.ToString(); label9.Text = table1, 0.ToString(); label10.Text = table1, 1.ToString(); label11.Text = table1, 2.ToString(); label12.Text = rowSum1.ToString(); label13.Text = colSum0.ToString(); lab

35、el14.Text = colSum1.ToString(); label15.Text = colSum2.ToString(); label16.Text = totalSum.ToString(); /* 67UsingGUI.Output.cs (3/3) labeUsingGUI.Table.cs片段public int, Content get return data; set data = value; nRow = value.GetUpperBound(0) + 1; nCol = value.GetUpperBound(1) + 1; 68UsingGUI.Table.cs

36、片段public int綱要改寫主控台程式為視窗程式加入圖形影像二十一點模擬程式0.1G版69綱要改寫主控台程式為視窗程式71程式DisplayingCards畫面70程式DisplayingCards畫面72程式DisplayingCards注意事項檔案夾PlayingCards應放在bin資料夾內,與Debug(偵錯版)及Release(發行版)資料夾併行71程式DisplayingCards注意事項檔案夾PlayinDisplayingcCards.MainForm.Designer.cs (1/4)/*using System.Drawing;/*namespace Displayin

37、gCards partial class MainForm / / 設計工具所需的變數。 / private System.ComponentModel.IContainer components = null; /* private Image image; private Graphics graphics; private bool started = false; /*72DisplayingcCards.MainForm.DesDisplayingcCards.MainForm.Designer.cs (2/4) / / 清除任何使用中的資源。 / / 如果應該處置 Managed

38、資源則為 true,否則為 false。 protected override void Dispose(bool disposing) if (disposing & (components != null) components.Dispose(); base.Dispose(disposing); 73DisplayingcCards.MainForm.DesDisplayingcCards.MainForm.Designer.cs (3/4) /* private void DisplayImage() int si = listBox1.SelectedIndex; string s

39、uit = s, h, d, c ; int i = listBox2.SelectedIndex + 1; string rank = i.ToString(); string fileName = .PlayingCards + suitsi + rank + .jpg; image = Image.FromFile(fileName); graphics = CreateGraphics(); graphics.DrawImage(image, 5, 5, 85, 150); 74DisplayingcCards.MainForm.DesDisplayingcCards.MainForm

40、.Designer.cs (4/4) protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) base.OnPaint(e); if (started) DisplayImage(); /* #region Windows Form 設計工具產生的程式碼 . . . . . . #endregion . . . . . . 75DisplayingcCards.MainForm.DesMainForm.cs (1/2)using System;using System.Collections.Generic;

41、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(); 76MainForm.cs (1/2)using System;MainForm.cs (2/2) private void Mai

42、nForm_Load(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e) /* DisplayImage(); /* 77MainForm.cs (2/2) private v綱要改寫主控台程式為視窗程式加入圖形影像二十一點模擬程式0.1G版78綱要改寫主控台程式為視窗程式80BlackJack_0_1G 類別圖79BlackJack_0_1G 類別圖81互動設計:Activity Diagram80互動設計:Activity Diagram82Form Design莊家: 18

43、點玩家: 21 點要牌停開始清除81Form Design莊家: 18 點玩家: 21 點要牌停起始處理:Collaboration Diagram82起始處理:Collaboration Diagram84起始處理:Sequence Diagram83起始處理:Sequence Diagram85玩家要牌:Sequence Diagram84玩家要牌:Sequence Diagram86玩家停牌:Sequence Diagram85玩家停牌:Sequence Diagram87系統表單86系統表單88BlackJack_0_1G.MainForm.cs (1/11)using System;

44、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;87BlackJack_0_1G.MainForm.cs (1/BlackJack_0

45、_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; /*88BlackJack_

46、0_1G.MainForm.cs (2/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); /* 89BlackJack_0_1G.MainFo

47、rm.cs (3/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.cardsi.Name() + .jpg; image = Image.FromFile(fileName); graphics.DrawImage(image, 5+100*i, 220, 85, 15

48、0); 90BlackJack_0_1G.MainForm.cs (4/BlackJack_0_1G.MainForm.cs (5/11) for (i = 0; i = playerInfo.totalPoints) MessageBox.Show(dealerI + 勝 + playerI); else MessageBox.Show(playerI + 勝 + dealerI); /* 94BlackJack_0_1G.MainForm.cs (8/BlackJack_0_1G.MainForm.cs (9/11) private void CheckBlackJackOrBurst(P

49、layerInfo info) if (info.status = Status.BLACK_JACK) MessageBox.Show(+ 二十一點); if (info.status = Status.BURST) MessageBox.Show( + 爆!); 95BlackJack_0_1G.MainForm.cs (9/BlackJack_0_1G.MainForm.cs (10/11) private void button4_Click(object sender, EventArgs e) /* / 清除按鈕 inGame = false; Invalidate(); labe

50、l1.Text = 0; label2.Text = 0; button4.Enabled = false; button3.Enabled = true; /* 96BlackJack_0_1G.MainForm.cs (10BlackJack_0_1G.MainForm.cs (11/11) /* protected override void OnPaint(PaintEventArgs e) base.OnPaint(e); if (inGame) ShowInfo(); /* 97BlackJack_0_1G.MainForm.cs (11BlackJack_0_1G.Game.cs

51、 (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 PlayerN_PLAYERS; public Game() 98BlackJack_0_1G.Game.cs (1/7)/*BlackJack_0_1G.Game.cs (2/7)players0 = new Player(Jeng); playersN_PLAYERS - 1 = n

52、ew Dealer(); deck = new Deck(); public void InitPlay(out PlayerInfo playerInfo, out PlayerInfo dealerInfo) int i; / 第一輪發牌 for (i = 0; i N_PLAYERS; +i) playersi.SaveACard(deck.DealACard(); 99BlackJack_0_1G.Game.cs (2/7)BlackJack_0_1G.Game.cs (3/7)/ 第二輪發牌 for (i = 0; i N_PLAYERS; +i) playersi.SaveACar

53、d(deck.DealACard(); playerI = players0.Name; playerInfo.status = players0.GetStatus(); playerInfo.totalPoints = players0.GetTotalPoints(); playerInfo.cards = players0.DumpCards(); playerInfo.nCards = players0.GetNCards();100BlackJack_0_1G.Game.cs (3/7)BlackJack_0_1G.Game.cs (4/7) dealerI = playersN_

54、PLAYERS - 1.Name; dealerInfo.status = playersN_PLAYERS - 1.GetStatus(); dealerInfo.totalPoints = playersN_PLAYERS - 1.GetTotalPoints(); dealerInfo.cards = playersN_PLAYERS - 1.DumpCards(); dealerInfo.nCards = playersN_PLAYERS - 1.GetNCards(); 101BlackJack_0_1G.Game.cs (4/7)BlackJack_0_1G.Game.cs (5/

55、7) public void ProcessPlayerRun(out PlayerInfo playerInfo) players0.SaveACard(deck.DealACard(); playerI = players0.Name; playerInfo.status = players0.GetStatus(); playerInfo.totalPoints = players0.GetTotalPoints(); playerInfo.cards = players0.DumpCards(); playerInfo.nCards = players0.GetNCards(); 102BlackJack_0_1G.Game.cs (5/7)BlackJack_0_1G.Game.cs (6/7) public void ProcessDealerRun(out PlayerInfo dealerInfo) while (playersN_PLAYERS - 1.WantOneMoreCard() playersN_PLAYERS - 1.SaveACard(deck

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论