To keep the aspect ratio, you mean?
You would do this just like any WinForms project:
When the form loads, save the aspect radio: (float)Width/(float)Height
. In XNA, this can be in the LoadContent
your game (since the window will be created by then).
Then handle the sizechanged
form sizechanged
. You will need to keep track of whether the user is changing in height, width, or both. If it is height, then set Width = Height / AspectRatio
, if the width changes, set Height = Width * AspectRatio
.
If both changes change, then select the width or height (I mean the choice of one in the design, not each size) and do as described above.
You will probably have to do something specific to XNA as soon as you do, for example, resize the buffer, etc., but that doesnβt apply to this question, so Iβll leave it (ask another question if you need to be )
EDIT. Below is the minimum working sample:
It maintains the aspect ratio, and also resizes the graphics, drawing the original size of the window on the rendering target, and then drawing the scale corresponding to the new window. If you do not want the overridden BeginDraw
and EndDraw
methods to eliminate this.
using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace WindowsGame1 { public class Game1 : Game { GraphicsDeviceManager Graphics; float AspectRatio; Point OldWindowSize; Texture2D BlankTexture; RenderTarget2D OffScreenRenderTarget; SpriteBatch SpriteBatch; public Game1() { Graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; Graphics.IsFullScreen = false; Window.AllowUserResizing = true; Window.ClientSizeChanged += new EventHandler<EventArgs>(Window_ClientSizeChanged); } void Window_ClientSizeChanged(object sender, EventArgs e) {