简体   繁体   中英

C# WPF-Ask user before closing application if right-clicking the application icon on the system task bar

Let say I click the close button in my application and it will popup a dialog box with yes and no option. How can I implement this (popup dialog box ask user whether want to close the application) if I close the application by right-clicking the application icon on the system task bar?

Subscribe to Closing event in your MainWindow:

public MainWindow()
{
    InitializeComponent();
    Closing += OnClosing;
}

private void OnClosing(object sender, CancelEventArgs cancelEventArgs)
{
    if (MessageBox.Show(this, "Your message", "Confirm", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
    {
        cancelEventArgs.Cancel = true;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM