使用xna库建立一个最简单的窗体。
一个窗体也可以说是一个程序框架,只要继承于Game类,并且改写他的方法就可以了。然后在把这个派生类建立对象Run()起来就可以了。
重写Update方法跟Draw方法。Update可以用来检测用户的输入,然后更改逻辑数据。然后在Draw里面画图像内容。
例子中,在Game的构造函数里设置窗体的一些特性。
随便建立一个工程,编译前引用 Microsoft.Xna.Framework.dll Microsoft.Xna.Framework.Game.dll 程序适用于xna3.0 ctp库
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
public class Lesson1 : Game
{
private GraphicsDeviceManager device;
public Lesson1()
{
//窗体能否缩放,墨认为false
this.Window.AllowUserResizing = true;
//鼠标是否可视,默认为false
this.IsMouseVisible = true;
this.device = new GraphicsDeviceManager(this);
//设置窗体大小
this.device.PreferredBackBufferWidth = 640;
this.device.PreferredBackBufferHeight = 480;
//屏幕是否全屏,墨认为flase
this.device.IsFullScreen = false;
}
protected override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
this.device.GraphicsDevice.Clear(Color.Green);
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
//按下esc就退出
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
}
}
class Program
{
static void Main(string[] args)
{
using (Lesson1 lesson = new Lesson1())
{
lesson.Run();
}
}
}