D3181
D3181

Reputation: 2092

MonoGame Maximize window event

I have been having trouble trying to implement an event using monogame and c#. Currently when my window is resized i call an event:

        this.Window.ClientSizeChanged += new EventHandler<EventArgs>(Window_ClientSizeChanged);

        void Window_ClientSizeChanged(object sender, EventArgs e)
        {
            int width = Window.ClientBounds.Width;
            int height = Window.ClientBounds.Height;
            if (width > 0 && height > 0)
            {
                graphics.PreferredBackBufferWidth = width;
                graphics.PreferredBackBufferHeight = height;
                graphics.ApplyChanges();
             }
        }

This is all fine and resizes my window correctly and updates the width/height. However it is not triggered in the event that my window is resized via a user clicking the maximize/minimize button on the window frame.

After spending some time looking into some solutions the only suggestion to implement a maximize event was from here:

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

However this is a solution im not particularly fond of as it relies on the inclusion of System.Windows.Forms and since i may one day want to build for other platforms id rather not include any specific windows libraries.

Is there any monogame functionality that handles the maximize/minimize event calls for each platform or is it up to the end user to implement such functionality?

Upvotes: 2

Views: 1145

Answers (1)

Acidic
Acidic

Reputation: 6280

I've also had this issue and haven't found any 'built in' solution for that in the MonoGame API.

The solution I came up with was performing a check at the start of each frame on whether the width and height of Window.ClientBounds were changed.
It detects all window resizes similarly to the Window.ClientSizeChanged event, but also detects resizes made by maximizing/restoring the window.

Upvotes: 3

Related Questions