XNA窗口设置问题

作者: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()
{
graphics = 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属性的设置了。

是不是发现我很像唐僧~~- -!
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值