如何获取显示分辨率(屏幕尺寸)?

6
如何在XNA/Monogame中获取显示分辨率? 我在我的显示器上尝试了以下内容(1600x900): 下面的代码给我返回800,600。
//1.
GraphicsDevice.DisplayMode.Width
GraphicsDevice.DisplayMode.Height

//2.
GraphicsDeviceManager graphics = new GraphicsDeviceManager(this);
graphics.GraphicsDevice.DisplayMode.Width
graphics.GraphicsDevice.DisplayMode.Height

//3.
GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width
GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height

//4.
foreach (DisplayMode dm in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes)
{
    Console.WriteLine(dm.Width);
    Console.WriteLine(dm.Height);
}

我已经编辑了你的标题。请参考“问题的标题应该包含“标签”吗?”,在那里达成共识是“不应该”。 - John Saunders
@JohnSaunders 你是对的,我错了 - dimitris93
2个回答

19
_ScreenWidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
_ScreenHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;

额外参考资料:System.Drawing,System.Windows.Forms


0
如果应用程序以全屏模式启动,则可能会起作用:
Vector2 resolution = new Vector2(GraphicsDevice.Viewport.Width,GraphicsDevice.Viewport.Height);

如果你一开始无法计算屏幕大小,就无法在全屏模式下启动它。顺便说一句,我已经回答了自己的问题。 - dimitris93
不错,你也可以尝试一下这个:http://msdn.microsoft.com/en-us/library/bb195024.aspx - ViNi
this.graphics.IsFullScreen = true; 这实际上不起作用,有些人说这只适用于Xbox。 - dimitris93
它可以工作。你需要做的是: graphics = new GraphicsDeviceManager(this); graphics.IsFullScreen = true; graphics.PreferredBackBufferWidth = 1280; graphics.PreferredBackBufferHeight = 960; graphics.ApplyChanges(); - John Lord

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