From f33768240e3f86777aa154b03aed5db208299eba Mon Sep 17 00:00:00 2001
From: Phyxion <>
Date: Fri, 17 Jan 2020 13:01:37 +0100
Subject: [PATCH] Fix #190
---
LibreHardwareMonitor/UI/ShowDesktop.cs | 87 ++++++++++++++++----------
1 file changed, 54 insertions(+), 33 deletions(-)
diff --git a/LibreHardwareMonitor/UI/ShowDesktop.cs b/LibreHardwareMonitor/UI/ShowDesktop.cs
index 867e998..a52705d 100644
--- a/LibreHardwareMonitor/UI/ShowDesktop.cs
+++ b/LibreHardwareMonitor/UI/ShowDesktop.cs
@@ -9,30 +9,71 @@ using System.Windows.Forms;
namespace LibreHardwareMonitor.UI
{
- public class ShowDesktop
+ public class ShowDesktop : IDisposable
{
- public delegate void ShowDesktopChangedEventHandler(bool showDesktop);
- private event ShowDesktopChangedEventHandler ShowDesktopChangedEvent;
+ private readonly NativeWindow _referenceWindow;
+ private readonly string _referenceWindowCaption = "LibreHardwareMonitorShowDesktopReferenceWindow";
private readonly System.Threading.Timer _timer;
private bool _showDesktop;
- private readonly string _referenceWindowCaption = "LibreHardwareMonitorShowDesktopReferenceWindow";
+ ///
+ /// Prevents a default instance of the class from being created.
+ ///
private ShowDesktop()
{
- // create a reference window to detect show desktop
- NativeWindow referenceWindow = new NativeWindow();
+ // Create a reference window to detect show desktop
+ _referenceWindow = new NativeWindow();
+
CreateParams cp = new CreateParams { ExStyle = GadgetWindow.WS_EX_TOOLWINDOW, Caption = _referenceWindowCaption };
- referenceWindow.CreateHandle(cp);
- NativeMethods.SetWindowPos(
- referenceWindow.Handle,
- GadgetWindow.HWND_BOTTOM, 0, 0, 0, 0, GadgetWindow.SWP_NOMOVE |
- GadgetWindow.SWP_NOSIZE | GadgetWindow.SWP_NOACTIVATE |
- GadgetWindow.SWP_NOSENDCHANGING);
+ _referenceWindow.CreateHandle(cp);
+ NativeMethods.SetWindowPos(_referenceWindow.Handle,
+ GadgetWindow.HWND_BOTTOM,
+ 0,
+ 0,
+ 0,
+ 0,
+ GadgetWindow.SWP_NOMOVE |
+ GadgetWindow.SWP_NOSIZE |
+ GadgetWindow.SWP_NOACTIVATE |
+ GadgetWindow.SWP_NOSENDCHANGING);
// start a repeated timer to detect "Show Desktop" events
_timer = new System.Threading.Timer(OnTimer, null, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
}
+ public delegate void ShowDesktopChangedEventHandler(bool showDesktop);
+
+ private event ShowDesktopChangedEventHandler ShowDesktopChangedEvent;
+
+ // notify when the "show desktop" mode is changed
+ public event ShowDesktopChangedEventHandler ShowDesktopChanged
+ {
+ add
+ {
+ // start the monitor timer when someone is listening
+ if (ShowDesktopChangedEvent == null)
+ StartTimer();
+
+ ShowDesktopChangedEvent += value;
+ }
+ remove
+ {
+ ShowDesktopChangedEvent -= value;
+ // stop the monitor timer if nobody is interested
+ if (ShowDesktopChangedEvent == null)
+ StopTimer();
+ }
+ }
+
+ public static ShowDesktop Instance { get; } = new ShowDesktop();
+
+ ///
+ public void Dispose()
+ {
+ _timer?.Dispose();
+ _referenceWindow.ReleaseHandle();
+ }
+
private void StartTimer()
{
_timer.Change(0, 200);
@@ -68,6 +109,7 @@ namespace LibreHardwareMonitor.UI
}
}
}
+
return IntPtr.Zero;
}
@@ -95,27 +137,6 @@ namespace LibreHardwareMonitor.UI
}
}
- public static ShowDesktop Instance { get; } = new ShowDesktop();
-
- // notify when the "show desktop" mode is changed
- public event ShowDesktopChangedEventHandler ShowDesktopChanged
- {
- add
- {
- // start the monitor timer when someone is listening
- if (ShowDesktopChangedEvent == null)
- StartTimer();
- ShowDesktopChangedEvent += value;
- }
- remove
- {
- ShowDesktopChangedEvent -= value;
- // stop the monitor timer if nobody is interested
- if (ShowDesktopChangedEvent == null)
- StopTimer();
- }
- }
-
private static class NativeMethods
{
private const string USER = "user32.dll";