Added an option to show the plot in a separate window or on the right of the tree-view.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Runtime.InteropServices;
40 using System.Windows.Forms;
42 namespace OpenHardwareMonitor.GUI {
43 public class ShowDesktop {
44 private static ShowDesktop instance = new ShowDesktop();
46 public delegate void ShowDesktopChangedEventHandler(bool showDesktop);
48 private event ShowDesktopChangedEventHandler ShowDesktopChangedEvent;
50 private System.Threading.Timer timer;
51 private bool showDesktop = false;
52 private NativeWindow referenceWindow;
53 private string referenceWindowCaption =
54 "OpenHardwareMonitorShowDesktopReferenceWindow";
56 private ShowDesktop() {
57 // create a reference window to detect show desktop
58 referenceWindow = new NativeWindow();
59 CreateParams cp = new CreateParams();
60 cp.ExStyle = GadgetWindow.WS_EX_TOOLWINDOW;
61 cp.Caption = referenceWindowCaption;
62 referenceWindow.CreateHandle(cp);
63 NativeMethods.SetWindowPos(referenceWindow.Handle,
64 GadgetWindow.HWND_BOTTOM, 0, 0, 0, 0, GadgetWindow.SWP_NOMOVE |
65 GadgetWindow.SWP_NOSIZE | GadgetWindow.SWP_NOACTIVATE |
66 GadgetWindow.SWP_NOSENDCHANGING);
68 // start a repeated timer to detect "Show Desktop" events
69 timer = new System.Threading.Timer(OnTimer, null,
70 System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
73 private void StartTimer() {
77 private void StopTimer() {
78 timer.Change(System.Threading.Timeout.Infinite,
79 System.Threading.Timeout.Infinite);
82 // the desktop worker window (if available) can hide the reference window
83 private IntPtr GetDesktopWorkerWindow() {
84 IntPtr shellWindow = NativeMethods.GetShellWindow();
85 if (shellWindow == IntPtr.Zero)
89 NativeMethods.GetWindowThreadProcessId(shellWindow, out shellId);
91 IntPtr workerWindow = IntPtr.Zero;
92 while ((workerWindow = NativeMethods.FindWindowEx(
93 IntPtr.Zero, workerWindow, "WorkerW", null)) != IntPtr.Zero) {
96 NativeMethods.GetWindowThreadProcessId(workerWindow, out workerId);
97 if (workerId == shellId) {
98 IntPtr window = NativeMethods.FindWindowEx(
99 workerWindow, IntPtr.Zero, "SHELLDLL_DefView", null);
100 if (window != IntPtr.Zero) {
101 IntPtr desktopWindow = NativeMethods.FindWindowEx(
102 window, IntPtr.Zero, "SysListView32", null);
103 if (desktopWindow != IntPtr.Zero)
111 private void OnTimer(Object state) {
112 bool showDesktopDetected;
114 IntPtr workerWindow = GetDesktopWorkerWindow();
115 if (workerWindow != IntPtr.Zero) {
116 // search if the reference window is behind the worker window
117 IntPtr reference = NativeMethods.FindWindowEx(
118 IntPtr.Zero, workerWindow, null, referenceWindowCaption);
119 showDesktopDetected = reference != IntPtr.Zero;
121 // if there is no worker window, then nothing can hide the reference
122 showDesktopDetected = false;
125 if (showDesktop != showDesktopDetected) {
126 showDesktop = showDesktopDetected;
127 if (ShowDesktopChangedEvent != null) {
128 ShowDesktopChangedEvent(showDesktop);
133 public static ShowDesktop Instance {
134 get { return instance; }
137 // notify when the "show desktop" mode is changed
138 public event ShowDesktopChangedEventHandler ShowDesktopChanged {
140 // start the monitor timer when someone is listening
141 if (ShowDesktopChangedEvent == null)
143 ShowDesktopChangedEvent += value;
146 ShowDesktopChangedEvent -= value;
147 // stop the monitor timer if nobody is interested
148 if (ShowDesktopChangedEvent == null)
153 private static class NativeMethods {
154 private const string USER = "user32.dll";
156 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
157 public static extern bool SetWindowPos(IntPtr hWnd,
158 IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
160 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
161 public static extern IntPtr FindWindowEx(IntPtr hwndParent,
162 IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
164 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
165 public static extern IntPtr GetShellWindow();
167 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
168 public static extern int GetWindowThreadProcessId(IntPtr hWnd,