Addressed #42 by adding a parameter to the constructor that will attempt to respond to the WM_QUERYENDSESSION and block the shutdown.

pull/60/head
David Hall 2019-02-26 08:25:34 -07:00
parent 89d95791b2
commit 785a86e430
1 changed files with 7 additions and 3 deletions

View File

@ -27,14 +27,18 @@ namespace Vanara.Windows.Forms.Forms
/// </code></para></remarks>
public class PreventShutdownContext : IDisposable
{
private const int WM_QUERYENDSESSION = 0x11;
private HandleRef href;
/// <summary>Initializes a new instance of the <see cref="PreventShutdownContext"/> class.</summary>
/// <param name="window">The <see cref="Form"/> or <see cref="Control"/> that contains a valid window handle.</param>
/// <summary>Initializes a new instance of the <see cref="PreventShutdownContext" /> class.</summary>
/// <param name="window">The <see cref="Form" /> that contains a valid window handle.</param>
/// <param name="reason">The reason the application must block system shutdown. Because users are typically in a hurry when shutting down the system, they may spend only a few seconds looking at the shutdown reasons that are displayed by the system. Therefore, it is important that your reason strings are short and clear.</param>
public PreventShutdownContext(Control window, string reason)
/// <param name="tryToPreventShutdown">If set to <see langword="true" />, this class will return false to the WM_QUERYENDSESSION by handling the e.Cancel response to Form.FormClosing.</param>
public PreventShutdownContext(Form window, string reason, bool tryToPreventShutdown = false)
{
href = new HandleRef(window, window.Handle);
if (tryToPreventShutdown)
window.FormClosing += (s, e) => e.Cancel = true;
Reason = reason;
}