using Tile_Engine;
System.Windows.Forms.Control gameForm;
gameForm = System.Windows.Forms.Control.FromHandle(this.Window.Handle); gameForm.VisibleChanged += new EventHandler(gameForm_VisibleChanged); gameForm.SizeChanged += new EventHandler(pictureBox_SizeChanged);
private void gameForm_VisibleChanged(object sender, EventArgs e) { if (gameForm.Visible == true) gameForm.Visible = false; } void pictureBox_SizeChanged(object sender, EventArgs e) { if (parentForm.WindowState != System.Windows.Forms.FormWindowState.Minimized) { graphics.PreferredBackBufferWidth = pictureBox.Width; graphics.PreferredBackBufferHeight = pictureBox.Height; Camera.ViewPortWidth = pictureBox.Width; Camera.ViewPortHeight = pictureBox.Height; graphics.ApplyChanges(); } }
At first, the gameForm_VisibleChanged()
method may look odd. It may seem that it would prevent the game from ever being displayed, as it sets the gameForm.Visible
property to false
if it ever ends up as true
.
Remember though, that the form (or window) that is automatically generated by XNA will always be empty. Our game's display is now being redirected to the PictureBox
on our MapEditor form. This means that we really do want to make sure that the game's form is never visible. Whenever its visibility changes, we ensure that Visible
is set to false
to keep it from appearing.
When the size of the PictureBox
changes since it is anchored to the sides of the MapEditor form, resizing the form will resize the PictureBox
we want to update the GraphicsDeviceManager with the new size of our display area, and pass those updates along to the Camera class. We need to be careful to check the WindowState
of the parent form when processing the resize event. Because the back-buffer width and height must be greater than zero, if we attempt to set them when the form has been minimized, the application will crash.
Clicking on the window close button or pressing Alt + F4 to end the application will no longer work, because the hidden window that Game1 would normally run in will not close automatically.
3.137.218.215