Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

How can I override the close button near minimize/maximize in such a way that the application doesn't automatically close. I want it to show an exit screen or something like this, so I would like to be able to delay the close and display something on the screen when I press the red close button. At first I thought that I should override the OnExiting() method, but I couldn't get it working. This is my first attempt at creating a fully functional game, so I have no previous experience in game development nor with XNA.

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

As described on the XNA Community Forum, you can get the host Form and listen to the FormClosing event, which allows you to cancel the event. The example code from the community form reads:

protected override void LoadContent() 
{ 
    Form f = Form.FromHandle(Window.Handle) as Form; 
    if (f != null) 
    { 
        f.FormClosing += f_FormClosing; 
    } 
    base.LoadContent(); 
} 

void f_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    if (MessageBox.Show("Are you sure?", "Are you sure?", MessageBoxButtons.YesNo) == DialogResult.No) 
        e.Cancel = true; 
}  

Using that as a template, you should be able to come up with a method for bringing up an in-game dialog. You'll likely need to introduce a variable that indicates whether the user really wants to quit or not. For example:

bool userReallyWantsToQuit = false;

void f_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    if (!userReallyWantsToQuit)
    {
        displayExitMessage();    // Show the user something useful
        e.Cancel = true; 
    }
}

void Update()
{
    if(keyboard.keyDown[Esc])    // Complete pseudocode. This will be your true exit condition
    {
         userReallyWantsToQuit = true;
         Exit();
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.