How to prevent OpenTK.GameWindow from closing - c#

I have a game I've written in OpenTK but am looking for a way to add a handler to the close button of the game window without actually closing the game (e.g. invoking a "do you want to save before you quit?" sort of dialog). I can't seem to find any event handler or documentation that accomplishes this.

You can override the OnClosing method and show your message box there. If the user does not want to close, you can use e.Cancel = true, which will stop the form from closing:
protected override void OnClosing(CancelEventArgs e)
{
// ... show message box
if (/* wants to save*/)
{
// Cancel closing, the player does not want to exist
e.Cancel = true;
}
base.OnClosing(e);
}

Related

Windows Phone Back KeyPress + MessageBox crashes the app without Selection

I have a strange issue overriding BackkeyPress Function in code behind, inside the function i have a simple message box to Go back or cancel navigation ( stay in current page ), when no choice is made (ok or cancel ) and Messagebox is open for long time, Application crashes, when i try to debug, no exception is thrown and App remains in the state unless OK or cancel is pressed , but on Normal run ( without debugger ) the crash is apparent.
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
string caption = "exit?";
string message = "Do you still want to exit?";
e.Cancel = MessageBoxResult.Cancel == MessageBox.Show(message, caption,
MessageBoxButton.OKCancel);
base.OnBackKeyPress(e);
}
http://msdn.microsoft.com/en-US/library/windowsphone/develop/jj206947(v=vs.105).aspx
In Windows Phone 8, if you call Show in
OnBackKeyPress(CancelEventArgs) or a handler for the BackKeyPress
event, the app will exit.
You can work around this by calling Show on a different thread, as
described in the following steps. Override BackKeyPress or create a
handler for the BackKeyPress event. Set the Cancel to true to cancel
the back key press action. Dispatch a method that shows the
MessageBox. If the user chooses to leave the app, call Terminate(),
otherwise, do nothing.
I found one more solution to this, so I thought it would be good if I posted it here. It's just a workaround though.
private async void PhoneApplicationPage_BackKeyPress (object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
await Task.Delay(100);
if (MessageBox.Show(msg, cap, MessageBoxButton.OKCancel) == MssageBoxResult.OK)
{
//somecode
}
}
Source
When using Terminate() - be aware that a number of app.xaml.cs rootFrame navigating events associated with normal exit won't trigger, neither the ApplicationClosing or your page's OnNavigatedFrom. So check if anything going on is important. You might tack it on before terminating...

How to force close button to terminate all childforms?

I have program that opens subwindows inside of it (mdi.parent). I have made component that is in one window under it, however, i want that that window never actually disposed after its created because i want to keep only one instance of it.
This can be made with code:
// This prevents this window disposing from window close button, we want always show one and only
// one instance of this window.
FormClosing += (o, e) =>
{
Hide();
e.Cancel = true;
};
However, after this there is problem, closing program requires pressing close button press twice. First press closes subwindow and second terminates program. How this can be get around?
I am working with Winforms.
As Habib said, you can call Application.Exit, but:
The Form.Closed and Form.Closing events are not raised when the
Application.Exit method is called to exit your application
If this is important to you, you can do something like this (MDI parent code):
private Boolean terminating;
protected override void OnClosing(CancelEventArgs e)
{
if (!terminating)
{
terminating = true;
Close();
}
base.OnClosing(e);
}
Call Application.Exit() in the form close event.
Application.Exit - MSDN
Informs all message pumps that they must terminate, and then closes
all application windows after the messages have been processed.
The code inside of your FormClosing event handler method is a bit too terse. It does its job of preventing the user from closing the form, but as you've also noticed, it prevents you from closing the form programmatically as well.
This is easily solved by testing the value of the CloseReason property of the FormClosingEventArgs that are passed in each time the event is raised.
These will tell you the reason why the form is attempting to close. If the value is CloseReason.UserClosing, then you want to set e.Cancel to true and hide the form. If the value is something else, then you want to allow the form to continue closing.
// This prevents this window disposing when its close button is clicked by the
// user; we want always show one and only one instance of this window.
// But we still want to be able to close the form programmatically.
FormClosing += (o, e) =>
{
if (e.CloseReason == CloseReason.UserClosing)
{
Hide();
e.Cancel = true;
}
};
Use This
Form[] ch = this.MdiChildren;
foreach (Form chfrm in ch)
chfrm.Close();
You can use Application.Exit if there is no processing happening when the application is closed. Otherwise, you can check Application.OpenForms collection in MDI parent's closing event and close all the other forms that are open.

Taskbar Button rightclick: Customized menu in windows forms

I do not want the form to exit when the "close window" option is clicked in the menu that pops up when the taskbar button is right clicked. Instead, I want the application to be minimized to the system tray.
How do I change the behavior of the "Close Window"?
Add an override of OnFormClosing and look at the CloseReason of the event arguments parameter. Maybe something like this:
protected override OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Hide();
}
else
{
this.Close();
}
}
This way, the user cannot close your form (only hide it), but Windows still can for other reasons (e.g. shutdown).

C# close to tray (like msn messenger)

I have a c# .net app. So I created a notifyIcon that sits in the tray. What I want to do is when the user hits the "x" button on the form, I want it to close to the tray. They should only be able to exit program by using the context menu in the tray icon.
So what I did was, on the form close event, I check whether the form is visible. If its visible, i set it to invisible and set showInTaskbar to false (simulating minimize to tray) If the form is invisible already, then they are probably closing it from the tray, so I will exit the program in that case.
However, the problem I have is that if the window is visible, but they right click on the context menu of the tray icon and hit exit, I need to exit the program and not minimize.
How do I solve this problem?
try this:
bool _closingFromMenu;
void NOTIFYICON_EXIT_MENU_HANDLER(object sender, EventArgs e)
{
_closingFromMenu = true;
Close();
}
//form closing handler
FormClosing +=(a,b) =>{
if(_closingFromMenu){
Close();
}
else{
e.Cancel = true;
//do minimize stuff;
}
}
or if you have only one form you can call Application.Exit(); in context menu item handler
You probably want to track the state of the application based on the actions of the user as that's not necessarily reflected in the state of the window. So when the user selects Exit from the menu you need to set a flag to indicate that you're really exiting, not just hiding the window.
Just make your Context Menu close event call Application.Exit()

How to Disable Alt + F4 closing form?

What is the best way to disable Alt + F4 in a c# win form to prevent the user from closing the form?
I am using a form as a popup dialog to display a progress bar and I do not want the user to be able to close it.
This does the job:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
Edit: In response to pix0rs concern - yes you are correct that you will not be able to programatically close the app. However, you can simply remove the event handler for the form_closing event before closing the form:
this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Close();
If you look at the value of FormClosingEventArgs e.CloseReason, it will tell you why the form is being closed. You can then decide what to do, the possible values are:
Member name - Description
None - The cause of the closure was not defined or could not be determined.
WindowsShutDown - The operating system is closing all applications before shutting down.
MdiFormClosing - The parent form of this multiple document interface (MDI) form is closing.
UserClosing - The user is closing the form through the user interface (UI), for example by clicking the Close button on the form window, selecting Close from the window's control menu, or pressing ALT+F4.
TaskManagerClosing - The Microsoft Windows Task Manager is closing the application.
FormOwnerClosing - The owner form is closing.
ApplicationExitCall - The Exit method of the Application class was invoked.
I believe this is the right way to do it:
protected override void OnFormClosing(FormClosingEventArgs e)
{
switch (e.CloseReason)
{
case CloseReason.UserClosing:
e.Cancel = true;
break;
}
base.OnFormClosing(e);
}
Note that it is considered bad form for an application to completely prevent itself from closing. You should check the event arguments for the Closing event to determine how and why your application was asked to close. If it is because of a Windows shutdown, you should not prevent the close from happening.
You could handle the FormClosing event and set FormClosingEventArgs.Cancel to true.
I am using a form as a popup dialog to display a progress bar and I do not want the user to be able to close it.
If the user is determined to close your app (and knowledgeable) enough to press alt+f4, they'll most likely also be knowledgeable enough to run task manager and kill your application instead.
At least with alt+f4 your app can do a graceful shutdown, rather than just making people kill it. From experience, people killing your app means corrupt config files, broken databases, half-finished tasks that you can't resume, and many other painful things.
At least prompt them with 'are you sure' rather than flat out preventing it.
This is a hack to disable Alt + F4.
private void test_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.ModifierKeys == Keys.Alt || this.ModifierKeys == Keys.F4)
{
e.Cancel = true;
}
}
Subscribe FormClosing event
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = e.CloseReason == CloseReason.UserClosing;
}
Only one line in the method body.
This does the job:
bool myButtonWasClicked = false;
private void Exit_Click(object sender, EventArgs e)
{
myButtonWasClicked = true;
Application.Exit();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (myButtonWasClicked)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
Would FormClosing be called even when you're programatically closing the window? If so, you'd probably want to add some code to allow the form to be closed when you're finished with it (instead of always canceling the operation)
Hide close button on form by using the following in constructor of the form:
this.ControlBox = false;

Categories