How to maximize window in XNA

我怕爱的太早我们不能终老 提交于 2019-12-01 11:15:59

@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;

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!