Question

This SHOULD be a very simple question but after lots of searching there seems to be no working example anywhere. I just want my XNA window to start off maximized. I know how to set the width and height of the window, but that's not quite the same. I also need to do this without going full screen. I just want a normal maximized window.

Était-ce utile?

La solution

@Cyral has the closest answer so far, but it's still not quite what you want. To maximize a Windows Form, you use the WindowState property:

var form = (Form)Form.FromHandle(Window.Handle);
form.WindowState = FormWindowState.Maximized;

Autres conseils

Set the IsFullScreen property of the graphics device manager to true.

http://msdn.microsoft.com/en-us/library/bb195024(v=xnagamestudio.10).aspx

    //from the above msdn sample
    graphics = new GraphicsDeviceManager( this );
    content = new ContentManager( Services );

    graphics.PreferredBackBufferWidth = 800;
    graphics.PreferredBackBufferHeight = 600;
    graphics.PreferMultiSampling = false;
    graphics.IsFullScreen = true;

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphicsdevicemanager.isfullscreen(v=xnagamestudio.10).aspx

You can add a reference to System.Windows.Forms and System.Drawing (However, You will need to type the namespaces out, Because of ambiguities)

Use the following code after base.Initialize

Form form = (Form)Form.FromHandle(Window.Handle);
form.Location = Point(0, 0);
form.Size = Screen.PrimaryScreen.WorkingArea.Size;

Others have covered the step of maximizing automatically, but to enable the actual maximize button so the user can do it when desired, do this in the Game constructor:

Window.AllowUserResizing = true; 

Depending on how you want the game to behave when resizing begins and ends, perhaps pause the game, you may need to handle some of these events.

    Form form = (Form)Form.FromHandle(Window.Handle);
    form.ResizeBegin += new EventHandler(form_ResizeBegin);
    form.ResizeEnd += new EventHandler(form_ResizeEnd);
    form.LocationChanged += new EventHandler(form_LocationChanged);
_graphics = new GraphicsDeviceManager(this);
DisplayMode displayMode = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode;
this._graphics.PreferredBackBufferFormat = displayMode.Format;
this._graphics.PreferredBackBufferWidth = (int)(displayMode.Width);
this._graphics.PreferredBackBufferHeight = (int)(displayMode.Height);

Sort of works for me but not quite, you'll understand once you try. I mean, it's not perfect and I'm sure there's a better way but for prototyping this should work - or maybe with some tweaking you could get what you need.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top