Upload
ebony-bowen
View
47
Download
2
Embed Size (px)
DESCRIPTION
基礎視窗程式設計 (Windows Programming). 鄭士康 國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所. 綱要. 第一個視窗程式 訊息盒 工具箱與控制項 事件處理 選單 對話表單 MVC: 模型 - 呈現 - 控制器架構 加入圖形影像. 綱要. 物件導向二十一點模擬程式 0.1G 版 繪圖工具類別 Graphics. 綱要. 第一個視窗程式 訊息盒 工具箱與控制項 事件處理 選單 對話表單 MVC: 模型 - 呈現 - 控制器架構 加入圖形影像. 第一個 C# 視窗程式. - PowerPoint PPT Presentation
Citation preview
(Windows Programming)//
MVC:--
0.1GGraphics
MVC:--
C#/ (WindowsForm)Form1.cs[]///Program.cs/Form1.cs/Form1.Designer.cs
Form
Form
WindowsFormsApplication1.Program.cs (1/2)using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;
namespace WindowsFormsApplication1{ static class Program { /// /// /// [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 { /// /// /// private System.ComponentModel.IContainer components = null; /// /// /// /// Managed true false
WindowsFormsApplication1.Form1.Designer.cs (2/3) protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }
#region Windows Form
/// /// - /// ///
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 }}
MainFormHello
MVC:--
UsingMessageBox
UsingMessageBox.Program.csusing 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:--
/ButtonCheckBoxLabelProgressBaretc.
UsingControls
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
UsingMenuStrip
UsingMenuStrip.MainForm.cspublic partial class MainForm : Form{ public MainForm() { InitializeComponent(); }
private void ToolStripMenuItem_Click( object sender, EventArgs e) { //*********************************** ToolStripMenuItem.Enabled = true; //*********************************** }}
UsingMenuStrip
MVC:--
UsingDialogForm
//Windows FormLabel/TextBox/ButtonTextBox(Text, TextAlign)Button
//Windows Form/RichTextBox
UsingDialogForm
UsingDialogForm
UsingDialogForm
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; }
UsingDialogForm.Output.cs (6/6) private int TotalSum(int[,] data) { int nRow = data.GetUpperBound(0) + 1; int nCol = data.GetUpperBound(1) + 1; int totalSum = 0; for (int i = 0; i < nRow; ++i) { for (int j = 0; j < nCol; ++j) { totalSum += data[i, j]; } } return totalSum; } //****************************************** }}
MVC:--
Model-View-Controller*D. Collins, Designing Object-Oriented User Interfaces, Benjamin/Cummings, 1995.
Function determines forms (Document vs. View)
MVC
MVCExample.Table.cs (1/4)using System;namespace MVCExample{ public class Table { private int[,] data; public Table() { data = new int[1, 1]; data[0, 0] = 0; } public Table(int[,] data) { this.data = data; }
MVCExample.Table.cs (2/4) public int Element(int i, int j) { return data[i, j]; } public int[] RowSum() { 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; }
MVCExample.Table.cs (3/4) public int[] ColSum() { 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; }
MVCExample.Table.cs (4/4) public int TotalSum() { int nRow = data.GetUpperBound(0) + 1; int nCol = data.GetUpperBound(1) + 1; int totalSum = 0; for (int i = 0; i < nRow; ++i) { for (int j = 0; j < nCol; ++j) { totalSum += data[i, j]; } } return totalSum; } }}
UsingDialogForm.Dialog, UsingDialogForm.MainForm, UsingDialogForm.Output/. . .UsingDialogForm.Dialog.csDialog UsingDialogForm.MainForm.csUsingDialogForm.Output.csUsingDialogForm
MVCExample.Program.cs (1/2)/* * MVC Example * MVC * 5/12/2011 * skj */using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;//**************************************using UsingDialogForm;//**************************************namespace MVCExample{
MVCExample.Program.cs (2/2) static class Program { /// /// /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } }}
MVC:--
DisplayingCards
DisplayingCardsPlayingCardsbinDebug()Release()System.DrawingImageGraphicsListBox
DisplayingcCards.MainForm.Designer.cs (1/4)//*****************************using System.Drawing;//*****************************namespace DisplayingCards{ partial class MainForm { /// /// /// private System.ComponentModel.IContainer components = null; //******************************************* private Image image; private Graphics graphics; private bool started = false; //*******************************************
DisplayingcCards.MainForm.Designer.cs (2/4) /// /// /// /// Managed true false protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }
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); }
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 . . . . . . }}
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(); }
MainForm.cs (2/2) private void MainForm_Load(object sender, EventArgs e) {
} private void button1_Click(object sender, EventArgs e) { //***************** DisplayImage(); //***************** } }}
0.1GGraphics
GUIMVCViewControllerModel
BlackJack_0_1G
GUI/
Activity Diagram
Form Design: 18 : 21
Collaboration Diagram
Sequence Diagram
Sequence Diagram
Sequence Diagram
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;
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; //*******************************
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); //************************************** }
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); }
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; } //******************************************
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; //***************************************** }
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) {
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); } } //************************************** }
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 + " !!!"); } }
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; //************************************** }
BlackJack_0_1G.MainForm.cs (11/11) //****************************************** protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (inGame) { ShowInfo(); } } //****************************************** }}
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() {
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()); }
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();
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(); }
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(); }
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();
BlackJack_0_1G.Game.cs (7/7)dealerInfo.cards = players[N_PLAYERS - 1].DumpCards(); dealerInfo.nCards = players[N_PLAYERS - 1].GetNCards(); } }}
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;
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) { nCards = 0; this.name = name; }
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; }
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 + " : ");
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(); Console.WriteLine(name + " : " + totalPoints); } public Card[] DumpCards() { return hand; }
BlackJack_0_1G.Player.cs (6/6) public int GetNCards() { return nCards; } }}
0.1GGraphics
TestingGraphics.MainForm.cs(1/2)private void button1_Click(object sender, EventArgs e) { //*********************************************** DrawContents(); //***********************************************}//***********************************************private void DrawContents(){ Graphics g = this.CreateGraphics(); g.DrawLine(new Pen(Color.Black), 0, 0, 300, 300); g.Dispose();}
TestingGraphics.MainForm.cs(2/2)protected override void OnPaint(PaintEventArgs e){ base.OnPaint(e); DrawContents();}//***********************************************
DrawingAllShapesG
MainForm.cs (1/4)public partial class MainForm : Form{ //************************************************ private bool started = false; Shape[] list = new Shape[2]; //************************************************
public MainForm() { InitializeComponent();
MainForm.cs (2/4) //********************************************** Point topLeft; topLeft.x = 30; topLeft.y = 40; Point center; center.x = 75; center.y = 70; list[0] = new Rectangle(100, 50, topLeft); list[1] = new Circle(50, center); //********************************************** }
MainForm.cs (3/4) private void button1_Click(object sender, EventArgs e) { //********************************************** started = true; DrawContents(); //********************************************** } //************************************************ private void DrawContents() { Graphics g = this.CreateGraphics(); DrawAllShapes(list, g); g.Dispose(); }
MainForm.cs (4/4) private void DrawAllShapes(Shape[] list, Graphics g) { for (int i = 0; i < 2; ++i) { list[i].Draw(g); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (started) DrawContents(); } //************************************************}
Shape.cs struct Point{ public int x; public int y;}
class Shape{ public Shape() { } virtual public void Draw(Graphics g) { }}
Rectangle.csclass Rectangle : Shape{ private int width; private int height; private Point topLeft; public Rectangle(int width, int height, Point topLeft){ this.topLeft = topLeft; this.width = width; this.height = height; } public override void Draw(Graphics g){ g.DrawRectangle(new Pen(Color.Red), topLeft.x, topLeft.y, width, height); }}
Circle.csclass Circle : Shape{ private int radius; private Point center; public Circle(int radius, Point center) { this.radius = radius; this.center = center; } public override void Draw(Graphics g) { g.DrawArc(new Pen(Color.Blue), center.x - radius, center.y + radius, 2 * radius, 2 * radius, 0, 360); }}