日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
WindowsForms和C#的強大

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

公司主營業(yè)務:網站設計制作、成都網站建設、移動網站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出庫車免費做網站回饋大家。

 
 
 
  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的數組,并用線條來劃分tic-tac-toe的格子并讓每一個格子來繪制它們自己。大部分的游戲邏輯都是由板來負責的,所以制作這個游戲的最主要的部分就是建立一個窗體,把板作為數據成員,并且當鼠標按下時請求板的繪制。

 
 
 
  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)正確的函數。例如,游戲要響應MouseDown事件(通過把鼠標的位置交給板來處理)和Paint 事件。當它生成了事件,系統(tǒng)就會自動的調用。你還可以為非系統(tǒng)的、用戶定義的事件如按鈕被按下而手工關聯(lián)事件句柄。該游戲也可以創(chuàng)建一個Reset按鈕來處理清除游戲板的事件。

Windows Forms編程最基本的就是基于用戶界面,請求你來繪制屏幕的過程。Windows Forms定義了一個捕獲WM_PAINT消息的良好方法。Form類包含了一個名為OnPaint()的函數來讓你重載。通過重載這一方法,你可以捕獲繪圖事件并在屏幕上做你想做的。看一下例程的源代碼,你會注意到Paint事件的參數包括一個Graphics對象,它類似于SDK編程時的一個設備上下文。Graphics對象包括了畫線和圖形、填充區(qū)域以及任何你想在屏幕上做的。

Tic-tac-toe游戲通過讓游戲板自繪來響應Paint事件。如果你在例程中看一下TicTacToeBoard類和BoardSpace類,你就會發(fā)現(xiàn)每一個類都有一個Render()函數來使用Graphics對象的DrawLine()和DrawEllipse()方法在屏幕上繪圖。Windows Forms和C#的強大地方就在于你不必考慮管理GDI類型的資源,因為。NET Framework為你做了。

Windows Forms也提供給你很多的可行性,包括在Windows 窗體上添加菜單和圖標,顯示對話框和捕獲Paint和MouseDown事件以外的大量事件。以上介紹Windows Forms和C#的強大。


標題名稱:WindowsForms和C#的強大
文章起源:http://www.dlmjj.cn/article/coigesd.html