|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Windows.Forms; |
| 8 | + |
| 9 | +namespace WinDynamicDesktop |
| 10 | +{ |
| 11 | + // https://github.com/specshell/specshell.software.winform.hiddenform/blob/main/src/Specshell.WinForm.HiddenForm/HiddenForm.cs |
| 12 | + internal class HiddenForm : Form |
| 13 | + { |
| 14 | + private const int ENDSESSION_CLOSEAPP = 0x1; |
| 15 | + private const int WM_QUERYENDSESSION = 0x11; |
| 16 | + private const int WM_ENDSESSION = 0x16; |
| 17 | + |
| 18 | + [DllImport("user32.dll")] |
| 19 | + private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hwndNewParent); |
| 20 | + |
| 21 | + public HiddenForm() |
| 22 | + { |
| 23 | + this.Load += this.HiddenForm_Load; |
| 24 | + } |
| 25 | + |
| 26 | + protected override CreateParams CreateParams |
| 27 | + { |
| 28 | + get |
| 29 | + { |
| 30 | + const int WS_POPUP = unchecked((int)0x80000000); |
| 31 | + const int WS_EX_TOOLWINDOW = 0x80; |
| 32 | + |
| 33 | + CreateParams cp = base.CreateParams; |
| 34 | + cp.ExStyle = WS_EX_TOOLWINDOW; |
| 35 | + cp.Style = WS_POPUP; |
| 36 | + cp.Height = 0; |
| 37 | + cp.Width = 0; |
| 38 | + return cp; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + protected override void WndProc(ref Message m) |
| 43 | + { |
| 44 | + // https://github.com/rocksdanister/lively/blob/9142f6a4cfc222cd494f205a5daaa1a0238282e3/src/Lively/Lively/Views/WindowMsg/WndProcMsgWindow.xaml.cs#L41 |
| 45 | + switch (m.Msg) |
| 46 | + { |
| 47 | + case WM_QUERYENDSESSION: |
| 48 | + if ((m.LParam & ENDSESSION_CLOSEAPP) != 0) |
| 49 | + { |
| 50 | + UpdateChecker.RegisterApplicationRestart(null, (int)RestartFlags.RESTART_NO_CRASH | |
| 51 | + (int)RestartFlags.RESTART_NO_HANG | (int)RestartFlags.RESTART_NO_REBOOT); |
| 52 | + } |
| 53 | + m.Result = new IntPtr(1); |
| 54 | + break; |
| 55 | + case WM_ENDSESSION: |
| 56 | + if (m.WParam != IntPtr.Zero) |
| 57 | + { |
| 58 | + Application.Exit(); |
| 59 | + } |
| 60 | + m.Result = IntPtr.Zero; |
| 61 | + break; |
| 62 | + default: |
| 63 | + base.WndProc(ref m); |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private void HiddenForm_Load(object source, EventArgs e) |
| 69 | + { |
| 70 | + const int HWND_MESSAGE = -1; |
| 71 | + SetParent(this.Handle, new IntPtr(HWND_MESSAGE)); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments