Windows Forms和C#的強大
一旦你定義了窗體,就需要一些數(shù)據(jù)成員,一個構(gòu)造函數(shù)和一些事件句柄。我會依次向您闡釋Windows Forms和C#。首先是基本的數(shù)據(jù)成員,一個tic-tac-toe板。Tic-tac-toe游戲的數(shù)據(jù)包含了一個表示游戲板的3*3的矩陣數(shù)組。這個游戲定義了一塊板的格子。
- public struct BoardSpace {
- public BoardSpace(Mark mark,
- int left,
- int top,
- int right,
- int bottom) {
- // Initialize internal state?
- }
- public void SetMark(Player player) {
- // if the space is blank, mark it using
- // the player enumeration
- }
- public void Render(Graphics g) {
- Pen pen =
- new Pen(Color.FromARGB(170, Color.Black), 3);
- switch(m_mark) {
- case Mark.XMark:
- g.DrawLine(pen, m_left, m_top, m_right,
- m_bottom);
- g.DrawLine(pen, m_left, m_bottom, m_right,
- m_top);
- break;
- case Mark.OMark:
- int cx = m_right - m_left;
- int cy = m_bottom - m_top;
- g.DrawEllipse(pen, m_left, m_top, cx, cy);
- break;
- default:
- break;
- }
- }
- public Mark m_mark;
- public int m_top, m_left, m_right, m_bottom;
- };
每一個板的格子都表示在屏幕上的一個位置并確定玩家是否做了標記。此外,格子還使用了一個X和一個O,來決定哪個玩家做了標記。我還會細致的說明的。
Tic-tac-toe板管理了3*3的格子。
- public struct TicTacToeBoard {
- BoardSpace[,] m_BoardSpaces;
- public void Initialize() {
- m_BoardSpaces = new BoardSpace[3,3];
- // Initialize each space with a location on the screen and a
- // blank mark.
- // Here‘s the first space:
- m_BoardSpaces[0, 0] = new BoardSpace(Mark.Blank, 1,
- 1, 50, 50);
- // Do the rest like that?
- }
- public void ClearBoard() {
- // loop through the spaces clearing them
- }
- public Player EvaluateGame() {
- // Check adjacent marks and see who won.
- }
- public Positions HitTest(int x, int y, Player player) {
- // Test the incoming Coords and mark the right space
- // using the player enumeration
- }
- public void Render(Graphics g) {
- Pen pen = new Pen(Color.FromARGB(170,
- Color.Black), 5);
- g.DrawLine(pen, 1, 50, 150, 50);
- g.DrawLine(pen, 50, 1, 50, 150);
- g.DrawLine(pen, 1, 100, 150, 100);
- g.DrawLine(pen, 100, 1, 100, 150);
- for(int i = 0; i < 3; i++) {
- for(int j = 0; j < 3; j++) {
- m_BoardSpaces[i, j].Render(g);
- }
- }
- }
- };
它也管理著BoardSpace對象3*3的數(shù)組,并用線條來劃分tic-tac-toe的格子并讓每一個格子來繪制它們自己。大部分的游戲邏輯都是由板來負責的,所以制作這個游戲的最主要的部分就是建立一個窗體,把板作為數(shù)據(jù)成員,并且當鼠標按下時請求板的繪制。
- public class CSharpTicTacToe : Form {
- public Player m_Player = Player.XPlayer;
- TicTacToeBoard m_board = new TicTacToeBoard();
- public CSharpTicTacToe() {
- SetStyle(ControlStyles.Opaque, true);
- Size = new Size(500, 500);
- Text = "CSharp Tic Tac Toe";
- m_board.Initialize();
- //Finally add a button so that we can render to a bitmap
- Button buttonRestart = new Button();
- buttonRestart.Size=new Size(100,50);
- buttonRestart.Location=new Point(300,100);
- buttonRestart.Text="Restart";
- buttonRestart.AddOnClick(new EventHandler(Restart));
- this.Controls.Add(buttonRestart);
- }
- //Fired when the restart button is pressed
- private void Restart(object sender, EventArgs e) {
- m_Player = Player.XPlayer;
- m_board.ClearBoard();
- this.Invalidate();
- }
- protected override void OnMouseDown(MouseEventArgs e) {
- base.OnMouseDown(e);
- Positions position = m_board.HitTest(e.X, e.Y, m_Player);
- if(position == Positions.Unknown) {
- return;
- }
- if(m_Player == Player.XPlayer) {
- m_Player = Player.OPlayer;
- } else {
- m_Player = Player.XPlayer;
- }
- this.Invalidate();
- }
- protected override void OnPaint(PaintEventArgs e) {
- Graphics g = e.Graphics;
- e.Graphics.SmoothingMode =
- SmoothingMode.AntiAlias;
- g.FillRectangle(new
- SolidBrush(Color.FromARGB(250,
- Color.White)), ClientRectangle);
- m_board.Render(g);
- }
- public static void Main() {
- Application.Run(new CSharpTicTacToe());
- }
- }
- }
包含了Windows Forms應用程序的初始化代碼。注意這個過程就是初始化游戲板,創(chuàng)建一個Reset按鈕和其事件句柄,然后截獲MouseDown和Paint事件。
大部分的時間,響應事件就是重載(override)正確的函數(shù)。例如,游戲要響應MouseDown事件(通過把鼠標的位置交給板來處理)和Paint 事件。當它生成了事件,系統(tǒng)就會自動的調(diào)用。你還可以為非系統(tǒng)的、用戶定義的事件如按鈕被按下而手工關(guān)聯(lián)事件句柄。該游戲也可以創(chuàng)建一個Reset按鈕來處理清除游戲板的事件。
Windows Forms編程最基本的就是基于用戶界面,請求你來繪制屏幕的過程。Windows Forms定義了一個捕獲WM_PAINT消息的良好方法。Form類包含了一個名為OnPaint()的函數(shù)來讓你重載。通過重載這一方法,你可以捕獲繪圖事件并在屏幕上做你想做的??匆幌吕痰脑创a,你會注意到Paint事件的參數(shù)包括一個Graphics對象,它類似于SDK編程時的一個設(shè)備上下文。Graphics對象包括了畫線和圖形、填充區(qū)域以及任何你想在屏幕上做的。
Tic-tac-toe游戲通過讓游戲板自繪來響應Paint事件。如果你在例程中看一下TicTacToeBoard類和BoardSpace類,你就會發(fā)現(xiàn)每一個類都有一個Render()函數(shù)來使用Graphics對象的DrawLine()和DrawEllipse()方法在屏幕上繪圖。Windows Forms和C#的強大地方就在于你不必考慮管理GDI類型的資源,因為。NET Framework為你做了。
Windows Forms也提供給你很多的可行性,包括在Windows 窗體上添加菜單和圖標,顯示對話框和捕獲Paint和MouseDown事件以外的大量事件。以上介紹Windows Forms和C#的強大。
【編輯推薦】