How to prevent windows from closing[Resolved]

How to prevent windows from closing?

I want to pop up a dialog box when the window is closed, prompting the user to click OK and then exit.

platform: desktopgl

I think there’s system event for that, haven’t tried it but here’s the link:

Occurs when the user is trying to log off or shut down the system.

But how do I intercept the WndProc event in monogame? Do I need to try to modify the source code to achieve my goal?

Try this on your main Program.cs

    /// <summary>
    /// The main class.
    /// </summary>
    public static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            
            using (var game = new Game1())
            {
                Microsoft.Win32.SystemEvents.SessionEnding += new Microsoft.Win32.SessionEndingEventHandler(SystemEvents_ClosingWindows);
                game.Run();
            }
        }

        static void SystemEvents_ClosingWindows(object sender, Microsoft.Win32.SessionEndingEventArgs e)
        {
            System.Windows.Forms.MessageBox.Show(" HEY DONT SHUTDOWN TILL YOU SAVE ME!!!");            
        }

    }

Thank you very much for your answer. You may understand what I mean by mistake. I don’t want to stop it when the system is closed, just stop it when you close the monogame game window.

Just as I suspected : - D try this instead ^_^y

 protected override void Initialize()
        {
        
            base.Initialize();

           //* Prevent ALT+F4
            this.Window.AllowAltF4 = false;

            //* Prevent from closing windows form
            Form _GameForm = (Form)Form.FromHandle(Window.Handle);
            _GameForm.Closing += ClosingForm;

        }

        public void ClosingForm(object sender, System.ComponentModel.CancelEventArgs e)
        {

            System.Windows.Forms.MessageBox.Show("DEVELOPER DON'T WANT  U TO CLOSE THE APPS : - D ");

            e.Cancel = true; // This will cancel the closing event
           
        }
1 Like