那么,基于C# Windows Forms应用程序是什么样的呢?看一下tic-tac-toe例程吧。一个C# Windows Forms应用程序一开始通过一系列using声明先引入必要的定义(程序需要的类型定义)。
namespace CSharpTicTacToe {
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.WinForms;
// Windows Form code goes here?
};
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
第一个namespace关键字是可选的。但是对于设定功能的作用范围通常是很有用的——特别是在assembly过程中,一种编写DLL的新方式。在关键字之后,每一个using声明告诉C#编译器,程序所要用到的系统功能。因为tic-tac-toe游戏是一个Windows 窗体,源文件使用了System的WinForms namespace。而且,因为游戏使用了图形,源代码就要引入URT的绘图功能。
在你引用了namespace后,你就要通过从系统提供的Form类继承一个类来表示一个Windows 窗体。
public class CSharpTicTacToe : Form {
// Windows Form code goes here, including
// data members, a constructor, and
// some event handlers?
}
- 1.
- 2.
- 3.
- 4.
- 5.
C#提倡枚举作为定义变量类型的一种方式,而不是指定一个整数范围,这样能维护类型的安全性并能提供尽可能多的信息。Tic-tac-toe游戏指定了三种枚举类型:player类型、用于在板上做标记的类型和对板上位置命名的类型。以下就是具体的描述。你可以在游戏的多个地方看到它们的用途。
public enum Player {
XPlayer,OPlayer
}
public enum Mark
{
XMark,
OMark,
Blank
}
public enum Positions
TopLeft,
TopCenter,
TopRight,
MiddleLeft,
MiddleCenter,
MiddleRight,
BottomLeft,
BottomCenter,
BottomRight,
Unknown
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
以上介绍C# Windows Forms应用程序
【编辑推荐】