Windows Forms和C#的强大

开发 后端
这里介绍Windows Forms和C#的强大,以及介绍Windows Forms编程最基本的就是基于用户界面,请求你来绘制屏幕的过程。

一旦你定义了窗体,就需要一些数据成员,一个构造函数和一些事件句柄。我会依次向您阐释Windows Forms和C#。首先是基本的数据成员,一个tic-tac-toe板。Tic-tac-toe游戏的数据包含了一个表示游戏板的3*3的矩阵数组。这个游戏定义了一块板的格子。

  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应用程序的初始化代码。注意这个过程就是初始化游戏板,创建一个Reset按钮和其事件句柄,然后截获MouseDown和Paint事件。

大部分的时间,响应事件就是重载(override)正确的函数。例如,游戏要响应MouseDown事件(通过把鼠标的位置交给板来处理)和Paint 事件。当它生成了事件,系统就会自动的调用。你还可以为非系统的、用户定义的事件如按钮被按下而手工关联事件句柄。该游戏也可以创建一个Reset按钮来处理清除游戏板的事件。

Windows Forms编程最基本的就是基于用户界面,请求你来绘制屏幕的过程。Windows Forms定义了一个捕获WM_PAINT消息的良好方法。Form类包含了一个名为OnPaint()的函数来让你重载。通过重载这一方法,你可以捕获绘图事件并在屏幕上做你想做的。看一下例程的源代码,你会注意到Paint事件的参数包括一个Graphics对象,它类似于SDK编程时的一个设备上下文。Graphics对象包括了画线和图形、填充区域以及任何你想在屏幕上做的。

Tic-tac-toe游戏通过让游戏板自绘来响应Paint事件。如果你在例程中看一下TicTacToeBoard类和BoardSpace类,你就会发现每一个类都有一个Render()函数来使用Graphics对象的DrawLine()和DrawEllipse()方法在屏幕上绘图。Windows Forms和C#的强大地方就在于你不必考虑管理GDI类型的资源,因为。NET Framework为你做了。

Windows Forms也提供给你很多的可行性,包括在Windows 窗体上添加菜单和图标,显示对话框和捕获Paint和MouseDown事件以外的大量事件。以上介绍Windows Forms和C#的强大。

【编辑推荐】

  1. C#字符串操作步骤
  2. C#集成开发环境浅析
  3. Visual C# .NET应用程序
  4. C# TimeLabel控件详解
  5. C#复合控件开发技术
责任编辑:佚名 来源: MSDN
相关推荐

2009-08-24 13:40:58

C# Windows

2009-08-24 13:30:50

C# Windows

2010-12-20 15:13:11

Windows For

2009-08-17 09:57:00

C# Windows

2009-08-27 16:11:03

C# delegateC# event

2009-08-13 17:04:09

C#语言C#程序

2009-08-27 16:18:47

C#类C#结构体

2009-08-26 09:54:45

C#打印预览C#打印

2023-03-30 08:19:15

工具反编译安全

2009-08-26 10:34:15

C#类型C#变量

2009-08-14 16:02:50

C#启动windows

2009-08-19 16:50:32

Visual C#C#语言特性

2009-07-30 15:24:13

C#析构函数C#构造函数

2009-07-31 16:12:10

Windows APIC#

2009-08-07 16:43:44

C#调用Windows

2009-08-25 15:38:12

C# Windows服

2009-08-03 13:34:57

C#调用Windows

2009-08-10 10:04:25

C#抽象类C#接口

2009-09-01 17:51:47

C#拆箱C#装箱

2009-08-06 16:36:20

研究C#和.Net
点赞
收藏

51CTO技术栈公众号