1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/GUI/ShowDesktop.cs Mon Sep 06 19:53:13 2010 +0000
1.3 @@ -0,0 +1,172 @@
1.4 +/*
1.5 +
1.6 + Version: MPL 1.1/GPL 2.0/LGPL 2.1
1.7 +
1.8 + The contents of this file are subject to the Mozilla Public License Version
1.9 + 1.1 (the "License"); you may not use this file except in compliance with
1.10 + the License. You may obtain a copy of the License at
1.11 +
1.12 + http://www.mozilla.org/MPL/
1.13 +
1.14 + Software distributed under the License is distributed on an "AS IS" basis,
1.15 + WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1.16 + for the specific language governing rights and limitations under the License.
1.17 +
1.18 + The Original Code is the Open Hardware Monitor code.
1.19 +
1.20 + The Initial Developer of the Original Code is
1.21 + Michael Möller <m.moeller@gmx.ch>.
1.22 + Portions created by the Initial Developer are Copyright (C) 2010
1.23 + the Initial Developer. All Rights Reserved.
1.24 +
1.25 + Contributor(s):
1.26 +
1.27 + Alternatively, the contents of this file may be used under the terms of
1.28 + either the GNU General Public License Version 2 or later (the "GPL"), or
1.29 + the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
1.30 + in which case the provisions of the GPL or the LGPL are applicable instead
1.31 + of those above. If you wish to allow use of your version of this file only
1.32 + under the terms of either the GPL or the LGPL, and not to allow others to
1.33 + use your version of this file under the terms of the MPL, indicate your
1.34 + decision by deleting the provisions above and replace them with the notice
1.35 + and other provisions required by the GPL or the LGPL. If you do not delete
1.36 + the provisions above, a recipient may use your version of this file under
1.37 + the terms of any one of the MPL, the GPL or the LGPL.
1.38 +
1.39 +*/
1.40 +
1.41 +using System;
1.42 +using System.Runtime.InteropServices;
1.43 +using System.Windows.Forms;
1.44 +
1.45 +namespace OpenHardwareMonitor.GUI {
1.46 + public class ShowDesktop {
1.47 + private static ShowDesktop instance = new ShowDesktop();
1.48 +
1.49 + public delegate void ShowDesktopChangedEventHandler(bool showDesktop);
1.50 +
1.51 + private event ShowDesktopChangedEventHandler ShowDesktopChangedEvent;
1.52 +
1.53 + private System.Threading.Timer timer;
1.54 + private bool showDesktop = false;
1.55 + private NativeWindow referenceWindow;
1.56 + private string referenceWindowCaption =
1.57 + "OpenHardwareMonitorShowDesktopReferenceWindow";
1.58 +
1.59 + private ShowDesktop() {
1.60 + // create a reference window to detect show desktop
1.61 + referenceWindow = new NativeWindow();
1.62 + CreateParams cp = new CreateParams();
1.63 + cp.ExStyle = GadgetWindow.WS_EX_TOOLWINDOW;
1.64 + cp.Caption = referenceWindowCaption;
1.65 + referenceWindow.CreateHandle(cp);
1.66 + NativeMethods.SetWindowPos(referenceWindow.Handle,
1.67 + GadgetWindow.HWND_BOTTOM, 0, 0, 0, 0, GadgetWindow.SWP_NOMOVE |
1.68 + GadgetWindow.SWP_NOSIZE | GadgetWindow.SWP_NOACTIVATE |
1.69 + GadgetWindow.SWP_NOSENDCHANGING);
1.70 +
1.71 + // start a repeated timer to detect "Show Desktop" events
1.72 + timer = new System.Threading.Timer(OnTimer, null,
1.73 + System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
1.74 + }
1.75 +
1.76 + private void StartTimer() {
1.77 + timer.Change(0, 200);
1.78 + }
1.79 +
1.80 + private void StopTimer() {
1.81 + timer.Change(System.Threading.Timeout.Infinite,
1.82 + System.Threading.Timeout.Infinite);
1.83 + }
1.84 +
1.85 + // the desktop worker window (if available) can hide the reference window
1.86 + private IntPtr GetDesktopWorkerWindow() {
1.87 + IntPtr shellWindow = NativeMethods.GetShellWindow();
1.88 + if (shellWindow == IntPtr.Zero)
1.89 + return IntPtr.Zero;
1.90 +
1.91 + int shellId;
1.92 + NativeMethods.GetWindowThreadProcessId(shellWindow, out shellId);
1.93 +
1.94 + IntPtr workerWindow = IntPtr.Zero;
1.95 + while ((workerWindow = NativeMethods.FindWindowEx(
1.96 + IntPtr.Zero, workerWindow, "WorkerW", null)) != IntPtr.Zero) {
1.97 +
1.98 + int workerId;
1.99 + NativeMethods.GetWindowThreadProcessId(workerWindow, out workerId);
1.100 + if (workerId == shellId) {
1.101 + IntPtr window = NativeMethods.FindWindowEx(
1.102 + workerWindow, IntPtr.Zero, "SHELLDLL_DefView", null);
1.103 + if (window != IntPtr.Zero) {
1.104 + IntPtr desktopWindow = NativeMethods.FindWindowEx(
1.105 + window, IntPtr.Zero, "SysListView32", null);
1.106 + if (desktopWindow != IntPtr.Zero)
1.107 + return workerWindow;
1.108 + }
1.109 + }
1.110 + }
1.111 + return IntPtr.Zero;
1.112 + }
1.113 +
1.114 + private void OnTimer(Object state) {
1.115 + bool showDesktopDetected;
1.116 +
1.117 + IntPtr workerWindow = GetDesktopWorkerWindow();
1.118 + if (workerWindow != IntPtr.Zero) {
1.119 + // search if the reference window is behind the worker window
1.120 + IntPtr reference = NativeMethods.FindWindowEx(
1.121 + IntPtr.Zero, workerWindow, null, referenceWindowCaption);
1.122 + showDesktopDetected = reference == referenceWindow.Handle;
1.123 + } else {
1.124 + // if there is no worker window, then nothing can hide the reference
1.125 + showDesktopDetected = false;
1.126 + }
1.127 +
1.128 + if (showDesktop != showDesktopDetected) {
1.129 + showDesktop = showDesktopDetected;
1.130 + if (ShowDesktopChangedEvent != null) {
1.131 + ShowDesktopChangedEvent(showDesktop);
1.132 + }
1.133 + }
1.134 + }
1.135 +
1.136 + public static ShowDesktop Instance {
1.137 + get { return instance; }
1.138 + }
1.139 +
1.140 + // notify when the "show desktop" mode is changed
1.141 + public event ShowDesktopChangedEventHandler ShowDesktopChanged {
1.142 + add {
1.143 + // start the monitor timer when someone is listening
1.144 + if (ShowDesktopChangedEvent == null)
1.145 + StartTimer();
1.146 + ShowDesktopChangedEvent += value;
1.147 + }
1.148 + remove {
1.149 + ShowDesktopChangedEvent -= value;
1.150 + // stop the monitor timer if nobody is interested
1.151 + if (ShowDesktopChangedEvent == null)
1.152 + StopTimer();
1.153 + }
1.154 + }
1.155 +
1.156 + private static class NativeMethods {
1.157 + private const string USER = "user32.dll";
1.158 +
1.159 + [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
1.160 + public static extern bool SetWindowPos(IntPtr hWnd,
1.161 + IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
1.162 +
1.163 + [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
1.164 + public static extern IntPtr FindWindowEx(IntPtr hwndParent,
1.165 + IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
1.166 +
1.167 + [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
1.168 + public static extern IntPtr GetShellWindow();
1.169 +
1.170 + [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
1.171 + public static extern int GetWindowThreadProcessId(IntPtr hWnd,
1.172 + out int processId);
1.173 + }
1.174 + }
1.175 +}