自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Windows Forms和C#的強大

開發(fā) 后端
這里介紹Windows Forms和C#的強大,以及介紹Windows Forms編程最基本的就是基于用戶界面,請求你來繪制屏幕的過程。

一旦你定義了窗體,就需要一些數(shù)據(jù)成員,一個構(gòu)造函數(shù)和一些事件句柄。我會依次向您闡釋Windows Forms和C#。首先是基本的數(shù)據(jù)成員,一個tic-tac-toe板。Tic-tac-toe游戲的數(shù)據(jù)包含了一個表示游戲板的3*3的矩陣數(shù)組。這個游戲定義了一塊板的格子。

  1. public struct BoardSpace {  
  2. public BoardSpace(Mark mark,  
  3. int left,  
  4. int top,  
  5. int right,  
  6. int bottom) {  
  7. // Initialize internal state?  
  8. }  
  9. public void SetMark(Player player) {  
  10. // if the space is blank, mark it using  
  11. // the player enumeration  
  12. }  
  13. public void Render(Graphics g) {  
  14. Pen pen =  
  15. new Pen(Color.FromARGB(170, Color.Black), 3);  
  16. switch(m_mark) {  
  17. case Mark.XMark:  
  18. g.DrawLine(pen, m_left, m_top, m_right,  
  19. m_bottom);  
  20. g.DrawLine(pen, m_left, m_bottom, m_right,  
  21. m_top);  
  22. break;  
  23. case Mark.OMark:  
  24. int cx = m_right - m_left;  
  25. int cy = m_bottom - m_top;  
  26. g.DrawEllipse(pen, m_left, m_top, cx, cy);  
  27. break;  
  28. default:  
  29. break;  
  30. }  
  31. }  
  32. public Mark m_mark;  
  33. public int m_top, m_left, m_right, m_bottom;  
  34. };  

每一個板的格子都表示在屏幕上的一個位置并確定玩家是否做了標記。此外,格子還使用了一個X和一個O,來決定哪個玩家做了標記。我還會細致的說明的。

Tic-tac-toe板管理了3*3的格子。

  1. public struct TicTacToeBoard {  
  2. BoardSpace[,] m_BoardSpaces;  
  3. public void Initialize() {  
  4. m_BoardSpaces = new BoardSpace[3,3];  
  5. // Initialize each space with a location on the screen and a  
  6. // blank mark.  
  7. // Here‘s the first space:  
  8. m_BoardSpaces[0, 0] = new BoardSpace(Mark.Blank, 1,  
  9. 1, 50, 50);  
  10. // Do the rest like that?  
  11. }  
  12. public void ClearBoard() {  
  13. // loop through the spaces clearing them  
  14. }  
  15. public Player EvaluateGame() {  
  16. // Check adjacent marks and see who won.  
  17. }  
  18. public Positions HitTest(int x, int y, Player player) {  
  19. // Test the incoming Coords and mark the right space  
  20. // using the player enumeration  
  21. }  
  22. public void Render(Graphics g) {  
  23. Pen pen = new Pen(Color.FromARGB(170,  
  24. Color.Black), 5);  
  25. g.DrawLine(pen, 1, 50, 150, 50);  
  26. g.DrawLine(pen, 50, 1, 50, 150);  
  27. g.DrawLine(pen, 1, 100, 150, 100);  
  28. g.DrawLine(pen, 100, 1, 100, 150);  
  29. for(int i = 0; i < 3; i++) {  
  30. for(int j = 0; j < 3; j++) {  
  31. m_BoardSpaces[i, j].Render(g);  
  32. }  
  33. }  
  34. }  
  35. };  

它也管理著BoardSpace對象3*3的數(shù)組,并用線條來劃分tic-tac-toe的格子并讓每一個格子來繪制它們自己。大部分的游戲邏輯都是由板來負責的,所以制作這個游戲的最主要的部分就是建立一個窗體,把板作為數(shù)據(jù)成員,并且當鼠標按下時請求板的繪制。

  1. public class CSharpTicTacToe : Form {  
  2. public Player m_Player = Player.XPlayer;  
  3. TicTacToeBoard m_board = new TicTacToeBoard();  
  4. public CSharpTicTacToe() {  
  5. SetStyle(ControlStyles.Opaque, true);  
  6. Size = new Size(500, 500);  
  7. Text = "CSharp Tic Tac Toe";  
  8. m_board.Initialize();  
  9. //Finally add a button so that we can render to a bitmap  
  10. Button buttonRestart = new Button();  
  11. buttonRestart.Size=new Size(100,50);  
  12. buttonRestart.Location=new Point(300,100);  
  13. buttonRestart.Text="Restart";  
  14. buttonRestart.AddOnClick(new EventHandler(Restart));  
  15. this.Controls.Add(buttonRestart);  
  16. }  
  17. //Fired when the restart button is pressed  
  18. private void Restart(object sender, EventArgs e) {  
  19. m_Player = Player.XPlayer;  
  20. m_board.ClearBoard();  
  21. this.Invalidate();  
  22. }  
  23. protected override void OnMouseDown(MouseEventArgs e) {  
  24. base.OnMouseDown(e);  
  25. Positions position = m_board.HitTest(e.X, e.Y, m_Player);  
  26. if(position == Positions.Unknown) {  
  27. return;  
  28. }  
  29. if(m_Player == Player.XPlayer) {  
  30. m_Player = Player.OPlayer;  
  31. } else {  
  32. m_Player = Player.XPlayer;  
  33. }  
  34. this.Invalidate();  
  35. }  
  36. protected override void OnPaint(PaintEventArgs e) {  
  37. Graphics g = e.Graphics;  
  38. e.Graphics.SmoothingMode =  
  39. SmoothingMode.AntiAlias;  
  40. g.FillRectangle(new  
  41. SolidBrush(Color.FromARGB(250,  
  42. Color.White)), ClientRectangle);  
  43. m_board.Render(g);  
  44. }  
  45. public static void Main() {  
  46. Application.Run(new CSharpTicTacToe());  
  47. }  
  48. }  

包含了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#的強大。

【編輯推薦】

  1. C#字符串操作步驟
  2. C#集成開發(fā)環(huán)境淺析
  3. Visual C# .NET應用程序
  4. C# TimeLabel控件詳解
  5. C#復合控件開發(fā)技術(shù)
責任編輯:佚名 來源: MSDN
相關(guān)推薦

2009-08-24 13:40:58

C# Windows

2009-08-24 13:30:50

C# Windows

2024-12-27 09:25:56

開源框架C#

2024-07-09 17:09:49

C#開發(fā)Windows窗體

2009-08-17 09:57:00

C# Windows

2010-12-20 15:13:11

Windows For

2009-08-13 17:04:09

C#語言C#程序

2009-08-27 16:11:03

C# delegateC# event

2009-08-26 09:54:45

C#打印預覽C#打印

2009-08-27 16:18:47

C#類C#結(jié)構(gòu)體

2023-03-30 08:19:15

工具反編譯安全

2009-08-14 16:02:50

C#啟動windows

2009-08-26 10:34:15

C#類型C#變量

2009-08-19 16:50:32

Visual C#C#語言特性

2009-07-31 16:12:10

Windows APIC#

2009-08-07 16:43:44

C#調(diào)用Windows

2009-08-03 13:34:57

C#調(diào)用Windows

2009-08-25 15:38:12

C# Windows服

2009-07-30 15:24:13

C#析構(gòu)函數(shù)C#構(gòu)造函數(shù)

2009-08-25 17:15:50

C#隱藏C#重寫C#重載
點贊
收藏

51CTO技術(shù)棧公眾號