0
votes

basically what I want to do is center a 800x600 game inside a maximized window. so if the resolution is 1280x1024, there will be a large border, but if the resolution is 800x600 it will take up the whole screen

this is in the Initialize method, it makes the game window fullscreen with a fixed 3d border but the client area also resizes to full screen

Form gameWindowForm = (Form)sys.Form.FromHandle(this.Window.Handle);
gameWindowForm.FormBorderStyle = FormBorderStyle.Fixed3D;
gameWindowForm.Text = "";
gameWindowForm.ControlBox = false;
gameWindowForm.WindowState = sys.FormWindowState.Maximized;

I have tried setting preferred back buffer, clientbounds size, viewport and displaymode. No matter what I try to resize I either can't because it is read only or it doesn't change anything.

Any help?

2

2 Answers

2
votes

If you want to customise the window layout, perhaps try the official WinForms sample. There's no "official" way in XNA to mess with the client area within the window.

However if all you want to do is center the rendering region within the window, then you should be able to do that just by setting the viewport at the beginning of each frame, like so:

int width = GraphicsDevice.PresentationParameters.BackBufferWidth;
int height = GraphicsDevice.PresentationParameters.BackBufferHeight;
GraphicsDevice.Viewport = new Viewport(
        width/2 - 800/2, height/2 - 600/2, 800, 600);

(Note that this code will fail if your window gets too small and the viewport exceeds the screen size - so you'll want to check for that and respond - maybe by using a smaller viewport.)

Just keep in mind that GraphicsDevice.Clear will clear the entire backbuffer in XNA 4.0, not just the viewport (so instead just draw a flat-coloured rectangle across the viewport).

1
votes

I have used rendertargets to solve a simular issue in the past. Simply render your scene to a rendertarget, then render the target to the center of your screen.

This also allowed me to easily render a 'fancy' border around the viewport.

you can handle resizing when you calculate where to render the rendertarget, in the render event/method.

Some more info about rendertargets: http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Render_to_texture.php