Jérémie Faucher-Goulet

How do I get screen resolution of the computer

Because my program will be shared between many computers, I want the program to find the resolution of the computer on wich it is running.

By example, on my computer at home, the program will return the value 1024 and the value 768 in order to make 1024 X 768. On my laptop, I will get 1920 and 1200.

I want to get the width and the height separately, in two variables.

With those values, I will be able to set automatically on each computer the window size as I want it.

To resume, I am looking for something like this:

width = computer.ScreenResolution.width;
height = computer.ScreenResolution.height;

Also...

If I ever decide to start the game in FullScreen (Make the window Fullscreen), how do I do it
I want the start bar and the title bar still visible. Not a real fullscreen like in most games, but just maximise the window.

 P.S. I'm using XNA Game Studio on Visual C# Expresse Edition...

 




Re: XNA Framework get screen resolution & FullScreen

Kyle_W

It appears that you can get the current screen resolution with:

GraphicsDeviceManager.GraphicsDevice.DisplayMode.Width
GraphicsDeviceManager.GraphicsDevice.DisplayMode.Height

To enter true full screen mode (no title bar), you can use:

GraphicsDeviceManager.ToggleFullScreen()

In trying to find you an answer for how to start a game in maximized mode, I was stunned when I came to the realization that there does not seem to be any obvious way to do it. Likewise, there does not appear to be an obvious way to set the initial size and position of a non-maximized game window.

This is a glaring feature omission that we can only hope will be corrected in the next release of XNA.

In the meantime, you can do the following:

using System.Windows.Forms;

...

Form myForm = (Form)Form.FromHandle(this.Window.Handle);
myForm.WindowState = FormWindowState.Maximized;

You can set the position and size of the window in this way also if needed.





Re: XNA Framework get screen resolution & FullScreen

Kyle_W

One additional note:

I included the using statement in the above example for ease of reading, but you may in fact want to leave the using statement out and instead use the fully qualified names for the types from the Windows Forms namespace because there is at least one type (ButtonState) that exists both in the System.Windows.Forms namespace as well as the Microsoft.Xna.Framework.Input namespace. If you have a using statement for both namespaces and you reference a type in your game which exists in both namespaces, the compiler will force you to fully qualify the name of that type throughout your project.

Also, don't forget to add a reference to the project for System.Windows.Forms obviously.





Re: XNA Framework get screen resolution & FullScreen

dczraptor

To maximize your window when your game first runs, add this line to your main game constructor:

graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);

Then add this method to your class:

void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
{
DisplayMode displayMode = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode;
e.GraphicsDeviceInformation.PresentationParameters.BackBufferFormat = displayMode.Format;
e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth = displayMode.Width;
e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = displayMode.Height;

}






Re: XNA Framework get screen resolution & FullScreen

Kyle_W

Very cool information dczraptor! My way is shorter though.



Re: XNA Framework get screen resolution & FullScreen

Jeremie Faucher-Goulet

I will try that later today and let you know how it turns out.




Re: XNA Framework get screen resolution & FullScreen

Bill Reiss

I've added a Microsoft Connect feature suggestion for this, I think it's common enough for people to want to do this that it should be part of the framework. The link is here if you want to vote for it:

https://connect.microsoft.com/feedback/ViewFeedback.aspx FeedbackID=260505&SiteID=226






Re: XNA Framework get screen resolution & FullScreen

Kyle_W

Bill I appreciate that you took the initiative to create a Connect feature suggestion. However, it does not appear that you have accurately described the problem in your Connect posting. XNA does in fact keep the same resolution when going to full screen. The problem is that when you start a game in non-fullscreen mode, there are no readily available properties or methods to set the game window size or position, and you cannot specify that you want the game window to start in a maximized state. If you could edit the Connect posting to reflect this, I'll certainly vote for it.





Re: XNA Framework get screen resolution & FullScreen

Bill Reiss

Kyle_W wrote:

XNA does in fact keep the same resolution when going to full screen.

Yes, maybe I didn't phrase it properly, but this is the problem, isn't it Instead of keeping the same resolution, we want a way to automatically set the game window's resolution to the screen resolution. Or maybe I'm confused






Re: XNA Framework get screen resolution & FullScreen

Kyle_W

No. When you toggle a game to fullscreen mode, XNA will properly set the game to take up the whole screen using the current desktop screen resolution. The problem as I said in the previous post is that there are no properties or methods to easily alter the position or size or maximized state of a NON-fullscreen game window.



Re: XNA Framework get screen resolution & FullScreen

Ultrahead

... or methods to easily alter the position or size or maximized state of a NON-fullscreen game window ...

... nor to set the (initial) position of the "non-maximized" window ...

Also, when facing the PreparingDeviceSettings event it would handy to reset the BackBuffer's Width & Height to the preferred ones, so as to avoid weird behaviors in case you minimized the window (depending on main render method). Take for instance the original SpaceWar game: at least in my case, if you minimize the window and then restore it again at any time, you will notice that the game screen is resized with black bars as if you were going to fullscreen, because the backbuffer's size may be different than the one you configured as preferred).






Re: XNA Framework get screen resolution & FullScreen

Bill Reiss

Kyle_W wrote:
No. When you toggle a game to fullscreen mode, XNA will properly set the game to take up the whole screen using the current desktop screen resolution.

Not on my system, it changes the screen resolution based on the backbuffer width and height, but maybe I'm doing something differently

One strange thing, if I drag the corner of my window to make it take up the whole screen, the game is fine, but if I maximize the window, it gets jerky. Has anyone else seen anything like this






Re: XNA Framework get screen resolution & FullScreen

Ultrahead

What do you mean by jerky Everything is messed up on the screen, the sides of the screen shows "garbage" pixels, the screen goes the clear color. Be more specific.




Re: XNA Framework get screen resolution & FullScreen

Bill Reiss

It looks like the frame rate drops significantly I make the window the same size as it would be when it's maximized (without actually maximizing the window) and it's fine, but if I maximize the window, the frame rate drops. At least that looks like what is happening, I haven't actually put a framerate counter up on it, I guess I probably should so that I can be more specific in the symptoms.




Re: XNA Framework get screen resolution & FullScreen

Kyle_W

Well that's a different issue entirely so you should probably make a new thread if you want to discuss it. It might be possible that the back buffer is not scaling up properly when going to fullscreen mode... I'm not sure about that, but when going to fullscreen mode the screen resolution does not change, and the main question in this thread was how to start a game window maximized (not fullscreen). There are no readily available XNA properties or methods for setting the game window position or size or maximized state.