GUI/ShowDesktop.cs
author paulwerelds
Sat, 02 Oct 2010 14:39:25 +0000
changeset 204 59278dadc5c0
parent 176 c16fd81b520a
child 344 3145aadca3d2
permissions -rw-r--r--
Added a S.M.A.R.T dump for all drives, regardless of temperature presence so that we can start debugging SSD's and such.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     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
     8  
     9   http://www.mozilla.org/MPL/
    10 
    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.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    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.
    21 
    22   Contributor(s):
    23 
    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.
    35  
    36 */
    37 
    38 using System;
    39 using System.Runtime.InteropServices;
    40 using System.Windows.Forms;
    41 
    42 namespace OpenHardwareMonitor.GUI {
    43   public class ShowDesktop {
    44     private static ShowDesktop instance = new ShowDesktop();
    45 
    46     public delegate void ShowDesktopChangedEventHandler(bool showDesktop);
    47     
    48     private event ShowDesktopChangedEventHandler ShowDesktopChangedEvent;
    49 
    50     private System.Threading.Timer timer;
    51     private bool showDesktop = false;   
    52     private NativeWindow referenceWindow;
    53     private string referenceWindowCaption =
    54       "OpenHardwareMonitorShowDesktopReferenceWindow";
    55 
    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);
    67 
    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);
    71     }
    72 
    73     private void StartTimer() {
    74       timer.Change(0, 200);
    75     }
    76 
    77     private void StopTimer() {
    78       timer.Change(System.Threading.Timeout.Infinite,
    79         System.Threading.Timeout.Infinite);
    80     }
    81 
    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)
    86         return IntPtr.Zero;
    87 
    88       int shellId;
    89       NativeMethods.GetWindowThreadProcessId(shellWindow, out shellId);
    90 
    91       IntPtr workerWindow = IntPtr.Zero;
    92       while ((workerWindow = NativeMethods.FindWindowEx(
    93           IntPtr.Zero, workerWindow, "WorkerW", null)) != IntPtr.Zero) {
    94 
    95         int workerId;
    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)
   104               return workerWindow;
   105           }
   106         }
   107       }
   108       return IntPtr.Zero;
   109     }
   110 
   111     private void OnTimer(Object state) {
   112       bool showDesktopDetected;
   113 
   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;
   120       } else {
   121         // if there is no worker window, then nothing can hide the reference
   122         showDesktopDetected = false;
   123       }
   124 
   125       if (showDesktop != showDesktopDetected) {
   126         showDesktop = showDesktopDetected;
   127         if (ShowDesktopChangedEvent != null) {
   128           ShowDesktopChangedEvent(showDesktop);
   129         }
   130       }
   131     }
   132 
   133     public static ShowDesktop Instance {
   134       get { return instance; }
   135     }
   136 
   137     // notify when the "show desktop" mode is changed
   138     public event ShowDesktopChangedEventHandler ShowDesktopChanged {
   139       add {
   140         // start the monitor timer when someone is listening
   141         if (ShowDesktopChangedEvent == null)           
   142           StartTimer();
   143         ShowDesktopChangedEvent += value;
   144       }
   145       remove {
   146         ShowDesktopChangedEvent -= value;
   147         // stop the monitor timer if nobody is interested
   148         if (ShowDesktopChangedEvent == null)
   149           StopTimer();
   150       }
   151     }
   152 
   153     private static class NativeMethods {
   154       private const string USER = "user32.dll";
   155 
   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);
   159 
   160       [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
   161       public static extern IntPtr FindWindowEx(IntPtr hwndParent,
   162         IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
   163 
   164       [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
   165       public static extern IntPtr GetShellWindow();
   166 
   167       [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
   168       public static extern int GetWindowThreadProcessId(IntPtr hWnd,
   169         out int processId);
   170     }  
   171   }
   172 }