如何在XNA中设置窗口/屏幕大小?

55

我该如何调整XNA窗口的大小。

默认情况下,它以800x600分辨率开始。

4个回答

70

从XNA 4.0开始,此属性现在可以在GraphicsDeviceManager上找到。也就是说,这段代码将会放在你游戏的构造函数中。

graphics = new GraphicsDeviceManager(this);
graphics.IsFullScreen = false;
graphics.PreferredBackBufferHeight = 340;
graphics.PreferredBackBufferWidth = 480;

// if changing GraphicsDeviceManager properties outside 
// your game constructor also call:
// graphics.ApplyChanges();

Sjors Miltenburg的下面的回答现在已经过时了。Fuex的这个回答适用于XNA 4.0。 - Xonatron
不过在此之后,您仍然需要执行 graphics.ApplyChanges() 来更新图形。 - Petr Hudeček

61

我发现您需要设置

GraphicDevice.PreferredBackBufferHeight = height;
GraphicDevice.PreferredBackBufferWidth = width;

如果您在游戏类的构造函数中执行此操作,则可以正常运行,但是如果您尝试在构造函数之外执行此操作,则还需要调用。

GraphicsDevice.ApplyChanges();

此外,为了实现全屏(在调试时并不完全正常),您可以使用以下方法:

if (!GraphicsDevice.IsFullScreen)
   GraphicsDevice.ToggleFullScreen();

3
这个答案有点过时了,因此我建议查看下面的Fuex的答案。它大部分相同,但是代码将在没有任何修改的情况下编译。 - Vin St. John

-1

这个解决方案适用于XNA 3.0。只需将其放在您的游戏对象的构造函数中:

// Resize the screen to 1024 x 768.
IntPtr ptr = this.Window.Handle;
System.Windows.Forms.Form form = (System.Windows.Forms.Form)System.Windows.Forms.Control.FromHandle(ptr);
form.Size = new System.Drawing.Size(1024, 768);

graphics.PreferredBackBufferWidth = 1024;
graphics.PreferredBackBufferHeight = 768;

graphics.ApplyChanges();

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接