From 785a86e430209d31eaf15bf1f813cd6da4661449 Mon Sep 17 00:00:00 2001 From: David Hall Date: Tue, 26 Feb 2019 08:25:34 -0700 Subject: [PATCH] Addressed #42 by adding a parameter to the constructor that will attempt to respond to the WM_QUERYENDSESSION and block the shutdown. --- WIndows.Forms/Contexts/PreventShutdownContext.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/WIndows.Forms/Contexts/PreventShutdownContext.cs b/WIndows.Forms/Contexts/PreventShutdownContext.cs index d50c6d0d..a4249b19 100644 --- a/WIndows.Forms/Contexts/PreventShutdownContext.cs +++ b/WIndows.Forms/Contexts/PreventShutdownContext.cs @@ -27,14 +27,18 @@ namespace Vanara.Windows.Forms.Forms /// public class PreventShutdownContext : IDisposable { + private const int WM_QUERYENDSESSION = 0x11; private HandleRef href; - /// Initializes a new instance of the class. - /// The or that contains a valid window handle. + /// Initializes a new instance of the class. + /// The that contains a valid window handle. /// 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. - public PreventShutdownContext(Control window, string reason) + /// If set to , this class will return false to the WM_QUERYENDSESSION by handling the e.Cancel response to Form.FormClosing. + 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; }