GUI/GadgetWindow.cs
changeset 176 c16fd81b520a
child 180 d40f49d45614
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/GUI/GadgetWindow.cs	Mon Sep 06 19:53:13 2010 +0000
     1.3 @@ -0,0 +1,423 @@
     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.Drawing;
    1.43 +using System.Reflection;
    1.44 +using System.Runtime.InteropServices;
    1.45 +using System.Windows.Forms;
    1.46 +
    1.47 +namespace OpenHardwareMonitor.GUI {
    1.48 +  public class GadgetWindow : NativeWindow {
    1.49 +
    1.50 +    private bool visible = false;
    1.51 +    private bool lockPosition = false;
    1.52 +    private bool alwaysOnTop = false;
    1.53 +    private byte opacity = 255;
    1.54 +    private Point location = new Point(100, 100);
    1.55 +    private Size size = new Size(130, 84);
    1.56 +    private ContextMenu contextMenu = null;
    1.57 +    private MethodInfo commandDispatch;
    1.58 +
    1.59 +    public GadgetWindow() {
    1.60 +      Type commandType = 
    1.61 +        typeof(Form).Assembly.GetType("System.Windows.Forms.Command");
    1.62 +      commandDispatch = commandType.GetMethod("DispatchID", 
    1.63 +        BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, 
    1.64 +        null, new Type[]{ typeof(int) }, null);
    1.65 +
    1.66 +      this.CreateHandle(CreateParams);
    1.67 +
    1.68 +      // move window to the bottom
    1.69 +      MoveToBottom(Handle);
    1.70 +
    1.71 +      // prevent window from fading to a glass sheet when peek is invoked
    1.72 +      try {
    1.73 +        bool value = true;
    1.74 +        int r = NativeMethods.DwmSetWindowAttribute(Handle,
    1.75 +          WindowAttribute.DWMWA_EXCLUDED_FROM_PEEK, ref value,
    1.76 +          Marshal.SizeOf(value));
    1.77 +      } catch (DllNotFoundException) { } catch (EntryPointNotFoundException) { }
    1.78 +    }
    1.79 +
    1.80 +    private void ShowDesktopChanged(bool showDesktop) {
    1.81 +      if (showDesktop) {
    1.82 +        MoveToTopMost(Handle);
    1.83 +      } else {
    1.84 +        MoveToBottom(Handle);
    1.85 +      }
    1.86 +    }
    1.87 +
    1.88 +    private void MoveToBottom(IntPtr handle) {
    1.89 +      NativeMethods.SetWindowPos(handle, HWND_BOTTOM, 0, 0, 0, 0,
    1.90 +        SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING);
    1.91 +    }
    1.92 +
    1.93 +    private void MoveToTopMost(IntPtr handle) {
    1.94 +      NativeMethods.SetWindowPos(handle, HWND_TOPMOST, 0, 0, 0, 0,
    1.95 +        SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING);
    1.96 +    }
    1.97 +
    1.98 +    private void ShowContextMenu(Point position) {
    1.99 +      NativeMethods.TrackPopupMenuEx(contextMenu.Handle, 
   1.100 +        TPM_RIGHTBUTTON | TPM_VERTICAL, position.X,
   1.101 +        position.Y, Handle, IntPtr.Zero);
   1.102 +    }
   1.103 +
   1.104 +    protected virtual CreateParams CreateParams {
   1.105 +      get {
   1.106 +        CreateParams cp = new CreateParams();
   1.107 +        cp.Width = size.Width;
   1.108 +        cp.Height = size.Height;
   1.109 +        cp.X = location.X;
   1.110 +        cp.Y = location.Y;
   1.111 +        cp.ExStyle = WS_EX_LAYERED | WS_EX_TOOLWINDOW;
   1.112 +        return cp;
   1.113 +      }
   1.114 +    }
   1.115 +
   1.116 +    protected override void WndProc(ref Message message) {
   1.117 +      switch (message.Msg) {
   1.118 +        case WM_COMMAND:
   1.119 +          // need to dispatch the message for the context menu
   1.120 +          if (message.LParam == IntPtr.Zero) 
   1.121 +            commandDispatch.Invoke(null, new object[] { 
   1.122 +              message.WParam.ToInt32() & 0xFFFF });          
   1.123 +          break;
   1.124 +        case WM_NCHITTEST:           
   1.125 +          // all pixels of the form belong to the caption
   1.126 +          message.Result = HTCAPTION; 
   1.127 +          break;
   1.128 +        case WM_NCLBUTTONDBLCLK:  
   1.129 +          message.Result = IntPtr.Zero; break;
   1.130 +        case WM_NCRBUTTONDOWN:
   1.131 +          message.Result = IntPtr.Zero; break;
   1.132 +        case WM_NCRBUTTONUP:
   1.133 +          if (contextMenu != null)
   1.134 +            ShowContextMenu(new Point(
   1.135 +              (int)((uint)message.LParam & 0xFFFF), 
   1.136 +              (int)(((uint)message.LParam >>16) & 0xFFFF)));          
   1.137 +          message.Result = IntPtr.Zero; 
   1.138 +          break;
   1.139 +        case WM_WINDOWPOSCHANGING:         
   1.140 +          WindowPos wp = (WindowPos)Marshal.PtrToStructure(
   1.141 +            message.LParam, typeof(WindowPos));
   1.142 +
   1.143 +          // add the nomove flag if position is locked
   1.144 +          if (lockPosition)
   1.145 +            wp.flags |= SWP_NOMOVE;
   1.146 +
   1.147 +          // prevent the window from leaving the screen
   1.148 +          if ((wp.flags & SWP_NOMOVE) == 0) {            
   1.149 +            Rectangle rect = Screen.GetWorkingArea(new Point(wp.x, wp.y));
   1.150 +            const int margin = 20;
   1.151 +            wp.x = Math.Max(wp.x, rect.Left - wp.cx + margin);
   1.152 +            wp.x = Math.Min(wp.x, rect.Right - margin);
   1.153 +            wp.y = Math.Max(wp.y, rect.Top - wp.cy + margin);
   1.154 +            wp.y = Math.Min(wp.y, rect.Bottom - margin);
   1.155 +
   1.156 +            // raise the event if location changed
   1.157 +            if (location.X != wp.x || location.Y != wp.y) {
   1.158 +              location = new Point(wp.x, wp.y);
   1.159 +              if (LocationChanged != null)
   1.160 +                LocationChanged(this, EventArgs.Empty);
   1.161 +            }
   1.162 +          }          
   1.163 +
   1.164 +          Marshal.StructureToPtr(wp, message.LParam, false);
   1.165 +          message.Result = IntPtr.Zero;
   1.166 +          break;           
   1.167 +        default:
   1.168 +          base.WndProc(ref message); break;
   1.169 +      }      
   1.170 +    }
   1.171 +
   1.172 +    private BlendFunction CreateBlendFunction() {
   1.173 +      BlendFunction blend = new BlendFunction();
   1.174 +      blend.BlendOp = AC_SRC_OVER;
   1.175 +      blend.BlendFlags = 0;
   1.176 +      blend.SourceConstantAlpha = opacity;
   1.177 +      blend.AlphaFormat = AC_SRC_ALPHA;
   1.178 +      return blend;
   1.179 +    }
   1.180 +
   1.181 +    public void Update(Bitmap bitmap) {
   1.182 +      IntPtr screen = NativeMethods.GetDC(IntPtr.Zero);
   1.183 +      IntPtr memory = NativeMethods.CreateCompatibleDC(screen);
   1.184 +      IntPtr newHBitmap = IntPtr.Zero;
   1.185 +      IntPtr oldHBitmap = IntPtr.Zero;
   1.186 +
   1.187 +      try {
   1.188 +        newHBitmap = bitmap.GetHbitmap(Color.Black);
   1.189 +        oldHBitmap = NativeMethods.SelectObject(memory, newHBitmap);
   1.190 +
   1.191 +        Size size = bitmap.Size;
   1.192 +        Point pointSource = Point.Empty;
   1.193 +        Point topPos = Location;
   1.194 +
   1.195 +        BlendFunction blend = CreateBlendFunction();
   1.196 +        NativeMethods.UpdateLayeredWindow(Handle, screen, ref topPos,
   1.197 +          ref size, memory, ref pointSource, 0, ref blend, ULW_ALPHA);
   1.198 +      } finally {
   1.199 +        NativeMethods.ReleaseDC(IntPtr.Zero, screen);
   1.200 +        if (newHBitmap != IntPtr.Zero) {
   1.201 +          NativeMethods.SelectObject(memory, oldHBitmap);
   1.202 +          NativeMethods.DeleteObject(newHBitmap);
   1.203 +        }
   1.204 +        NativeMethods.DeleteDC(memory);
   1.205 +      }
   1.206 +    }
   1.207 +
   1.208 +    public byte Opacity {
   1.209 +      get {
   1.210 +        return opacity;
   1.211 +      }
   1.212 +      set {
   1.213 +        if (opacity != value) {
   1.214 +          opacity = value;
   1.215 +          BlendFunction blend = CreateBlendFunction();
   1.216 +          NativeMethods.UpdateLayeredWindow(Handle, IntPtr.Zero, IntPtr.Zero,
   1.217 +            IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 0, ref blend, ULW_ALPHA);
   1.218 +        }
   1.219 +      }
   1.220 +    }
   1.221 +
   1.222 +    public bool Visible {
   1.223 +      get {
   1.224 +        return visible;
   1.225 +      }
   1.226 +      set {
   1.227 +        if (visible != value) {
   1.228 +          visible = value;
   1.229 +          NativeMethods.SetWindowPos(Handle, IntPtr.Zero, 0, 0, 0, 0,
   1.230 +            SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER |
   1.231 +            (value ? SWP_SHOWWINDOW : SWP_HIDEWINDOW));
   1.232 +          if (value)
   1.233 +            ShowDesktop.Instance.ShowDesktopChanged += ShowDesktopChanged;
   1.234 +          else
   1.235 +            ShowDesktop.Instance.ShowDesktopChanged -= ShowDesktopChanged;         
   1.236 +        }
   1.237 +      }
   1.238 +    }
   1.239 +
   1.240 +    // if locked, the window can not be moved
   1.241 +    public bool LockPosition {
   1.242 +      get {
   1.243 +        return lockPosition;
   1.244 +      }
   1.245 +      set {
   1.246 +        lockPosition = value;
   1.247 +      }
   1.248 +    }
   1.249 +
   1.250 +    public bool AlwaysOnTop {
   1.251 +      get {
   1.252 +        return alwaysOnTop;
   1.253 +      }
   1.254 +      set {
   1.255 +        if (value != alwaysOnTop) {
   1.256 +          alwaysOnTop = value;
   1.257 +          if (alwaysOnTop) {
   1.258 +            ShowDesktop.Instance.ShowDesktopChanged -= ShowDesktopChanged;
   1.259 +            MoveToTopMost(Handle);            
   1.260 +          } else {
   1.261 +            MoveToBottom(Handle);
   1.262 +            ShowDesktop.Instance.ShowDesktopChanged += ShowDesktopChanged;
   1.263 +          }
   1.264 +        }
   1.265 +      }
   1.266 +    }
   1.267 +
   1.268 +    public Size Size {
   1.269 +      get {
   1.270 +        return size; 
   1.271 +      }
   1.272 +      set {
   1.273 +        if (size != value) {
   1.274 +          size = value;
   1.275 +          NativeMethods.SetWindowPos(Handle, IntPtr.Zero, 0, 0, size.Width,
   1.276 +            size.Height, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | 
   1.277 +            SWP_NOSENDCHANGING);
   1.278 +        }
   1.279 +      }
   1.280 +    }
   1.281 +
   1.282 +    public Point Location {
   1.283 +      get {
   1.284 +        return location;
   1.285 +      }
   1.286 +      set {
   1.287 +        NativeMethods.SetWindowPos(Handle, IntPtr.Zero, value.X, value.Y, 0,
   1.288 +          0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSENDCHANGING);
   1.289 +        location = value;
   1.290 +        if (LocationChanged != null)
   1.291 +          LocationChanged(this, EventArgs.Empty);
   1.292 +      }
   1.293 +    }
   1.294 +
   1.295 +    public event EventHandler LocationChanged;
   1.296 +
   1.297 +    public ContextMenu ContextMenu {
   1.298 +      get {
   1.299 +        return contextMenu;
   1.300 +      }
   1.301 +      set {
   1.302 +        this.contextMenu = value;
   1.303 +      }
   1.304 +    }
   1.305 +
   1.306 +    [StructLayout(LayoutKind.Sequential, Pack = 1)]
   1.307 +    private struct BlendFunction {
   1.308 +      public byte BlendOp;
   1.309 +      public byte BlendFlags;
   1.310 +      public byte SourceConstantAlpha;
   1.311 +      public byte AlphaFormat;
   1.312 +    }
   1.313 +
   1.314 +    [StructLayout(LayoutKind.Sequential, Pack = 1)]
   1.315 +    private struct WindowPos {
   1.316 +      public IntPtr hwnd;
   1.317 +      public IntPtr hwndInsertAfter;
   1.318 +      public int x;
   1.319 +      public int y;
   1.320 +      public int cx;
   1.321 +      public int cy;
   1.322 +      public uint flags;
   1.323 +    }
   1.324 +
   1.325 +    public static readonly IntPtr HWND_BOTTOM = (IntPtr)1;
   1.326 +    public static readonly IntPtr HWND_TOPMOST = (IntPtr)(-1);
   1.327 +
   1.328 +    public const int WS_EX_LAYERED = 0x00080000;
   1.329 +    public const int WS_EX_TOOLWINDOW = 0x00000080;
   1.330 +
   1.331 +    public const uint SWP_NOSIZE = 0x0001;
   1.332 +    public const uint SWP_NOMOVE = 0x0002;
   1.333 +    public const uint SWP_NOACTIVATE = 0x0010;
   1.334 +    public const uint SWP_HIDEWINDOW = 0x0080;
   1.335 +    public const uint SWP_SHOWWINDOW = 0x0040;
   1.336 +    public const uint SWP_NOZORDER = 0x0004;
   1.337 +    public const uint SWP_NOSENDCHANGING = 0x0400;
   1.338 +
   1.339 +    public const int ULW_COLORKEY = 0x00000001;
   1.340 +    public const int ULW_ALPHA = 0x00000002;
   1.341 +    public const int ULW_OPAQUE = 0x00000004;
   1.342 +
   1.343 +    public const byte AC_SRC_OVER = 0x00;
   1.344 +    public const byte AC_SRC_ALPHA = 0x01;
   1.345 +
   1.346 +    public const int WM_NCHITTEST = 0x0084;
   1.347 +    public const int WM_NCLBUTTONDBLCLK = 0x00A3;
   1.348 +    public const int WM_NCLBUTTONDOWN = 0x00A1;
   1.349 +    public const int WM_NCLBUTTONUP = 0x00A2;
   1.350 +    public const int WM_NCRBUTTONDOWN = 0x00A4;
   1.351 +    public const int WM_NCRBUTTONUP = 0x00A5;
   1.352 +    public const int WM_WINDOWPOSCHANGING = 0x0046;
   1.353 +    public const int WM_COMMAND = 0x0111;
   1.354 +
   1.355 +    public const int TPM_RIGHTBUTTON = 0x0002;
   1.356 +    public const int TPM_VERTICAL = 0x0040;
   1.357 +
   1.358 +    public readonly IntPtr HTCAPTION = (IntPtr)2;
   1.359 +
   1.360 +    private enum WindowAttribute : int {
   1.361 +      DWMWA_NCRENDERING_ENABLED = 1,
   1.362 +      DWMWA_NCRENDERING_POLICY,
   1.363 +      DWMWA_TRANSITIONS_FORCEDISABLED,
   1.364 +      DWMWA_ALLOW_NCPAINT,
   1.365 +      DWMWA_CAPTION_BUTTON_BOUNDS,
   1.366 +      DWMWA_NONCLIENT_RTL_LAYOUT,
   1.367 +      DWMWA_FORCE_ICONIC_REPRESENTATION,
   1.368 +      DWMWA_FLIP3D_POLICY,
   1.369 +      DWMWA_EXTENDED_FRAME_BOUNDS,
   1.370 +      DWMWA_HAS_ICONIC_BITMAP,
   1.371 +      DWMWA_DISALLOW_PEEK,
   1.372 +      DWMWA_EXCLUDED_FROM_PEEK,
   1.373 +      DWMWA_LAST
   1.374 +    }
   1.375 +
   1.376 +    private static class NativeMethods {
   1.377 +      private const string USER = "user32.dll";
   1.378 +      private const string GDI = "gdi32.dll";
   1.379 +      public const string DWMAPI = "dwmapi.dll";
   1.380 +
   1.381 +      [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
   1.382 +      [return: MarshalAs(UnmanagedType.Bool)]
   1.383 +      public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, 
   1.384 +        ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, 
   1.385 +        int crKey, ref BlendFunction pblend, int dwFlags);
   1.386 +
   1.387 +      [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
   1.388 +      [return: MarshalAs(UnmanagedType.Bool)]
   1.389 +      public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst,
   1.390 +        IntPtr pptDst, IntPtr psize, IntPtr hdcSrc, IntPtr pprSrc,
   1.391 +        int crKey, ref BlendFunction pblend, int dwFlags);  
   1.392 +
   1.393 +      [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
   1.394 +      public static extern IntPtr GetDC(IntPtr hWnd);
   1.395 +
   1.396 +      [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
   1.397 +      public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
   1.398 +
   1.399 +      [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
   1.400 +      public static extern bool SetWindowPos(IntPtr hWnd,
   1.401 +        IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
   1.402 +
   1.403 +      [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
   1.404 +      public static extern bool TrackPopupMenuEx(IntPtr hMenu, uint uFlags, 
   1.405 +        int x, int y, IntPtr hWnd, IntPtr tpmParams);
   1.406 +
   1.407 +      [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
   1.408 +      public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
   1.409 +
   1.410 +      [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
   1.411 +      [return: MarshalAs(UnmanagedType.Bool)]
   1.412 +      public static extern bool DeleteDC(IntPtr hdc);
   1.413 +      
   1.414 +      [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
   1.415 +      public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
   1.416 +
   1.417 +      [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
   1.418 +      [return: MarshalAs(UnmanagedType.Bool)]
   1.419 +      public static extern bool DeleteObject(IntPtr hObject);
   1.420 +
   1.421 +      [DllImport(DWMAPI, CallingConvention = CallingConvention.Winapi)]
   1.422 +      public static extern int DwmSetWindowAttribute(IntPtr hwnd,
   1.423 +        WindowAttribute dwAttribute, ref bool pvAttribute, int cbAttribute);
   1.424 +    }    
   1.425 +  }
   1.426 +}