原文地址:http://blog.csdn.net/kobeair/article/details/1623253

作者:Object.飘 又名:kobeair 说明:我也是初学,如果那里有错误,请以源代码为准! 不要做为任何商业用途~


昨天晚上设置窗口实在郁闷,试了N多办法都不行。 最后请教了PP才明白。
protected override void Initialize() { // TODO: Add your initialization logic here
    graphics.PreferredBackBufferFormat = SurfaceFormat.Bgr32; 

    graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8; 

    graphics.PreferredBackBufferWidth = 640; 

    graphics.PreferredBackBufferHeight = 480; 

}

这样是不能设置窗口大小的,必须在后面加上graphics.ApplyChanges();方法。
protected override void Initialize() { // TODO: Add your initialization logic here
graphics.PreferredBackBufferFormat = SurfaceFormat.Bgr32;

graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;

graphics.PreferredBackBufferWidth = 640;

graphics.PreferredBackBufferHeight = 480;

graphics.ApplyChanges();

}
这样就可以了。 原因是Device被构造出来,Devcie的默认宽度和高度是800和600。如果不加graphics.ApplyChanges()方法就不能设置值。其实graphics.ApplyChanges()方法很像Managed DirectX中的 Device Reset.
另一种办法写在构造函数中。在Device被构造出来之前就设置其值,改变他的默认值。

public Game1()

{

graphics = new GraphicsDeviceManager(this);

content = new ContentManager(Services);
graphics.PreferredBackBufferFormat = SurfaceFormat.Bgr32;

graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;

graphics.PreferredBackBufferWidth = 640;

graphics.PreferredBackBufferHeight = 480;

}
这样也可以设置窗体大小。
还有一种办法,如果在运行时想改变大小呢?那么就要和事件进行绑定。同样也要设置在构造函数中。
public Game1()

{ g

raphics = new GraphicsDeviceManager(this); content = new ContentManager(Services);
graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);

}
private void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)

{

graphics.PreferredBackBufferFormat = SurfaceFormat.Bgr32;

graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;

e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth= 640;

e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = 480;

}

如果这样设置窗口不能用鼠标进行拖拽,如果想用鼠标实时的进行拖拽,那么必须要设置 this.Window.AllowUserResizing
this.Window.AllowUserResizing = true;
这个位置就无关紧要了,可以放在构造函数中也可以放在Initialize()方法中。
一旦设置this.Window.AllowUserResizing属性之后不能和事件同时使用。如果同时使用,无论怎么进行鼠标的拖拽,窗口都会回到在事件中设置的窗口大小。所以如果用事件设定的话就没必要在进行this.Window.AllowUserResizing属性的设置了。

posted on 2013-04-20 17:45  竟飞工作室  阅读(287)  评论(0编辑  收藏  举报