Improved the moving and resizing of the gadget on multi-monitor systems. Fixed one problem where the underlying gadget window size would be reduced unrequested. Fine tuned the progress bar size in the gadget.
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.
40 using System.Reflection;
41 using System.Runtime.InteropServices;
42 using System.Windows.Forms;
44 namespace OpenHardwareMonitor.GUI {
46 public class GadgetWindow : NativeWindow {
48 private bool visible = false;
49 private bool lockPositionAndSize = false;
50 private bool alwaysOnTop = false;
51 private byte opacity = 255;
52 private Point location = new Point(100, 100);
53 private Size size = new Size(130, 84);
54 private ContextMenu contextMenu = null;
55 private MethodInfo commandDispatch;
57 public GadgetWindow() {
59 typeof(Form).Assembly.GetType("System.Windows.Forms.Command");
60 commandDispatch = commandType.GetMethod("DispatchID",
61 BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public,
62 null, new Type[]{ typeof(int) }, null);
64 this.CreateHandle(CreateParams);
66 // move window to the bottom
69 // prevent window from fading to a glass sheet when peek is invoked
72 NativeMethods.DwmSetWindowAttribute(Handle,
73 WindowAttribute.DWMWA_EXCLUDED_FROM_PEEK, ref value,
74 Marshal.SizeOf(value));
75 } catch (DllNotFoundException) { } catch (EntryPointNotFoundException) { }
78 private void ShowDesktopChanged(bool showDesktop) {
80 MoveToTopMost(Handle);
86 private void MoveToBottom(IntPtr handle) {
87 NativeMethods.SetWindowPos(handle, HWND_BOTTOM, 0, 0, 0, 0,
88 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING);
91 private void MoveToTopMost(IntPtr handle) {
92 NativeMethods.SetWindowPos(handle, HWND_TOPMOST, 0, 0, 0, 0,
93 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING);
96 private void ShowContextMenu(Point position) {
97 NativeMethods.TrackPopupMenuEx(contextMenu.Handle,
98 TPM_RIGHTBUTTON | TPM_VERTICAL, position.X,
99 position.Y, Handle, IntPtr.Zero);
102 protected virtual CreateParams CreateParams {
104 CreateParams cp = new CreateParams();
109 cp.ExStyle = WS_EX_LAYERED | WS_EX_TOOLWINDOW;
114 protected override void WndProc(ref Message message) {
115 switch (message.Msg) {
117 // need to dispatch the message for the context menu
118 if (message.LParam == IntPtr.Zero)
119 commandDispatch.Invoke(null, new object[] {
120 message.WParam.ToInt32() & 0xFFFF });
123 message.Result = (IntPtr)HitResult.Caption;
124 if (HitTest != null) {
126 Macros.GET_X_LPARAM(message.LParam) - location.X,
127 Macros.GET_Y_LPARAM(message.LParam) - location.Y
129 HitTestEventArgs e = new HitTestEventArgs(p, HitResult.Caption);
131 message.Result = (IntPtr)e.HitResult;
134 case WM_NCLBUTTONDBLCLK: {
135 message.Result = IntPtr.Zero;
137 case WM_NCRBUTTONDOWN: {
138 message.Result = IntPtr.Zero;
140 case WM_NCRBUTTONUP: {
141 if (contextMenu != null)
142 ShowContextMenu(new Point(
143 Macros.GET_X_LPARAM(message.LParam),
144 Macros.GET_Y_LPARAM(message.LParam)
146 message.Result = IntPtr.Zero;
148 case WM_WINDOWPOSCHANGING: {
149 WindowPos wp = (WindowPos)Marshal.PtrToStructure(
150 message.LParam, typeof(WindowPos));
152 if (!lockPositionAndSize) {
153 // prevent the window from leaving the screen
154 if ((wp.flags & SWP_NOMOVE) == 0) {
155 Rectangle rect = Screen.GetWorkingArea(
156 new Rectangle(wp.x, wp.y, wp.cx, wp.cy));
157 const int margin = 16;
158 wp.x = Math.Max(wp.x, rect.Left - wp.cx + margin);
159 wp.x = Math.Min(wp.x, rect.Right - margin);
160 wp.y = Math.Max(wp.y, rect.Top - wp.cy + margin);
161 wp.y = Math.Min(wp.y, rect.Bottom - margin);
164 // update location and fire event
165 if ((wp.flags & SWP_NOMOVE) == 0) {
166 if (location.X != wp.x || location.Y != wp.y) {
167 location = new Point(wp.x, wp.y);
168 if (LocationChanged != null)
169 LocationChanged(this, EventArgs.Empty);
173 // update size and fire event
174 if ((wp.flags & SWP_NOSIZE) == 0) {
175 if (size.Width != wp.cx || size.Height != wp.cy) {
176 size = new Size(wp.cx, wp.cy);
177 if (SizeChanged != null)
178 SizeChanged(this, EventArgs.Empty);
182 // update the size of the layered window
183 if ((wp.flags & SWP_NOSIZE) == 0) {
184 NativeMethods.UpdateLayeredWindow(Handle, IntPtr.Zero,
185 IntPtr.Zero, ref size, IntPtr.Zero, IntPtr.Zero, 0,
189 // update the position of the layered window
190 if ((wp.flags & SWP_NOMOVE) == 0) {
191 NativeMethods.SetWindowPos(Handle, IntPtr.Zero,
192 location.X, location.Y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE |
193 SWP_NOZORDER | SWP_NOSENDCHANGING);
197 // do not forward any move or size messages
198 wp.flags |= SWP_NOSIZE | SWP_NOMOVE;
200 // suppress any frame changed events
201 wp.flags &= ~SWP_FRAMECHANGED;
203 Marshal.StructureToPtr(wp, message.LParam, false);
204 message.Result = IntPtr.Zero;
207 base.WndProc(ref message);
212 private BlendFunction CreateBlendFunction() {
213 BlendFunction blend = new BlendFunction();
214 blend.BlendOp = AC_SRC_OVER;
215 blend.BlendFlags = 0;
216 blend.SourceConstantAlpha = opacity;
217 blend.AlphaFormat = AC_SRC_ALPHA;
221 public void Update(Bitmap bitmap) {
222 IntPtr screen = NativeMethods.GetDC(IntPtr.Zero);
223 IntPtr memory = NativeMethods.CreateCompatibleDC(screen);
224 IntPtr newHBitmap = IntPtr.Zero;
225 IntPtr oldHBitmap = IntPtr.Zero;
228 newHBitmap = bitmap.GetHbitmap(Color.Black);
229 oldHBitmap = NativeMethods.SelectObject(memory, newHBitmap);
231 Point pointSource = Point.Empty;
232 BlendFunction blend = CreateBlendFunction();
234 NativeMethods.UpdateLayeredWindow(Handle, screen, IntPtr.Zero,
235 ref size, memory, ref pointSource, 0, ref blend, ULW_ALPHA);
237 // make sure the window is at the right location
238 NativeMethods.SetWindowPos(Handle, IntPtr.Zero,
239 location.X, location.Y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE |
240 SWP_NOZORDER | SWP_NOSENDCHANGING);
243 if (newHBitmap != IntPtr.Zero) {
244 NativeMethods.SelectObject(memory, oldHBitmap);
245 NativeMethods.DeleteObject(newHBitmap);
247 NativeMethods.DeleteDC(memory);
248 NativeMethods.ReleaseDC(IntPtr.Zero, screen);
252 public byte Opacity {
257 if (opacity != value) {
259 BlendFunction blend = CreateBlendFunction();
260 NativeMethods.UpdateLayeredWindow(Handle, IntPtr.Zero, IntPtr.Zero,
261 IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 0, ref blend, ULW_ALPHA);
266 public bool Visible {
271 if (visible != value) {
273 NativeMethods.SetWindowPos(Handle, IntPtr.Zero, 0, 0, 0, 0,
274 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER |
275 (value ? SWP_SHOWWINDOW : SWP_HIDEWINDOW));
278 ShowDesktop.Instance.ShowDesktopChanged += ShowDesktopChanged;
281 ShowDesktop.Instance.ShowDesktopChanged -= ShowDesktopChanged;
287 // if locked, the window can not be moved or resized
288 public bool LockPositionAndSize {
290 return lockPositionAndSize;
293 lockPositionAndSize = value;
297 public bool AlwaysOnTop {
302 if (value != alwaysOnTop) {
306 ShowDesktop.Instance.ShowDesktopChanged -= ShowDesktopChanged;
307 MoveToTopMost(Handle);
309 MoveToBottom(Handle);
311 ShowDesktop.Instance.ShowDesktopChanged += ShowDesktopChanged;
324 NativeMethods.UpdateLayeredWindow(Handle, IntPtr.Zero, IntPtr.Zero,
325 ref size, IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero, 0);
326 if (SizeChanged != null)
327 SizeChanged(this, EventArgs.Empty);
332 public event EventHandler SizeChanged;
334 public Point Location {
339 if (location != value) {
341 NativeMethods.SetWindowPos(Handle, IntPtr.Zero,
342 location.X, location.Y, 0, 0,
343 SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSENDCHANGING);
344 if (LocationChanged != null)
345 LocationChanged(this, EventArgs.Empty);
350 public event EventHandler LocationChanged;
352 public ContextMenu ContextMenu {
357 this.contextMenu = value;
361 public event HitTestEventHandler HitTest;
363 [StructLayout(LayoutKind.Sequential, Pack = 1)]
364 private struct BlendFunction {
366 public byte BlendFlags;
367 public byte SourceConstantAlpha;
368 public byte AlphaFormat;
371 [StructLayout(LayoutKind.Sequential, Pack = 1)]
372 private struct WindowPos {
374 public IntPtr hwndInsertAfter;
382 public static readonly IntPtr HWND_BOTTOM = (IntPtr)1;
383 public static readonly IntPtr HWND_TOPMOST = (IntPtr)(-1);
385 public const int WS_EX_LAYERED = 0x00080000;
386 public const int WS_EX_TOOLWINDOW = 0x00000080;
388 public const uint SWP_NOSIZE = 0x0001;
389 public const uint SWP_NOMOVE = 0x0002;
390 public const uint SWP_NOACTIVATE = 0x0010;
391 public const uint SWP_FRAMECHANGED = 0x0020;
392 public const uint SWP_HIDEWINDOW = 0x0080;
393 public const uint SWP_SHOWWINDOW = 0x0040;
394 public const uint SWP_NOZORDER = 0x0004;
395 public const uint SWP_NOSENDCHANGING = 0x0400;
397 public const int ULW_COLORKEY = 0x00000001;
398 public const int ULW_ALPHA = 0x00000002;
399 public const int ULW_OPAQUE = 0x00000004;
401 public const byte AC_SRC_OVER = 0x00;
402 public const byte AC_SRC_ALPHA = 0x01;
404 public const int WM_NCHITTEST = 0x0084;
405 public const int WM_NCLBUTTONDBLCLK = 0x00A3;
406 public const int WM_NCLBUTTONDOWN = 0x00A1;
407 public const int WM_NCLBUTTONUP = 0x00A2;
408 public const int WM_NCRBUTTONDOWN = 0x00A4;
409 public const int WM_NCRBUTTONUP = 0x00A5;
410 public const int WM_WINDOWPOSCHANGING = 0x0046;
411 public const int WM_COMMAND = 0x0111;
413 public const int TPM_RIGHTBUTTON = 0x0002;
414 public const int TPM_VERTICAL = 0x0040;
416 private enum WindowAttribute : int {
417 DWMWA_NCRENDERING_ENABLED = 1,
418 DWMWA_NCRENDERING_POLICY,
419 DWMWA_TRANSITIONS_FORCEDISABLED,
421 DWMWA_CAPTION_BUTTON_BOUNDS,
422 DWMWA_NONCLIENT_RTL_LAYOUT,
423 DWMWA_FORCE_ICONIC_REPRESENTATION,
425 DWMWA_EXTENDED_FRAME_BOUNDS,
426 DWMWA_HAS_ICONIC_BITMAP,
428 DWMWA_EXCLUDED_FROM_PEEK,
433 /// Some macros imported and converted from the Windows SDK
435 private static class Macros {
436 public static ushort LOWORD(IntPtr l) {
437 return (ushort) ((ulong)l & 0xFFFF);
440 public static UInt16 HIWORD(IntPtr l) {
441 return (ushort) (((ulong)l >> 16) & 0xFFFF);
444 public static int GET_X_LPARAM(IntPtr lp) {
445 return (short) LOWORD(lp);
448 public static int GET_Y_LPARAM(IntPtr lp) {
449 return (short) HIWORD(lp);
454 /// Imported native methods
456 private static class NativeMethods {
457 private const string USER = "user32.dll";
458 private const string GDI = "gdi32.dll";
459 public const string DWMAPI = "dwmapi.dll";
461 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
462 [return: MarshalAs(UnmanagedType.Bool)]
463 public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst,
464 IntPtr pptDst, ref Size psize, IntPtr hdcSrc, IntPtr pprSrc,
465 int crKey, IntPtr pblend, int dwFlags);
467 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
468 [return: MarshalAs(UnmanagedType.Bool)]
469 public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst,
470 IntPtr pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc,
471 int crKey, ref BlendFunction pblend, int dwFlags);
473 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
474 [return: MarshalAs(UnmanagedType.Bool)]
475 public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst,
476 IntPtr pptDst, IntPtr psize, IntPtr hdcSrc, IntPtr pprSrc,
477 int crKey, ref BlendFunction pblend, int dwFlags);
479 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
480 public static extern IntPtr GetDC(IntPtr hWnd);
482 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
483 public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
485 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
486 public static extern bool SetWindowPos(IntPtr hWnd,
487 IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
489 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
490 public static extern bool TrackPopupMenuEx(IntPtr hMenu, uint uFlags,
491 int x, int y, IntPtr hWnd, IntPtr tpmParams);
493 [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
494 public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
496 [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
497 [return: MarshalAs(UnmanagedType.Bool)]
498 public static extern bool DeleteDC(IntPtr hdc);
500 [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
501 public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
503 [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
504 [return: MarshalAs(UnmanagedType.Bool)]
505 public static extern bool DeleteObject(IntPtr hObject);
507 [DllImport(DWMAPI, CallingConvention = CallingConvention.Winapi)]
508 public static extern int DwmSetWindowAttribute(IntPtr hwnd,
509 WindowAttribute dwAttribute, ref bool pvAttribute, int cbAttribute);
513 public enum HitResult {
529 public delegate void HitTestEventHandler(object sender, HitTestEventArgs e);
531 public class HitTestEventArgs : EventArgs {
532 public HitTestEventArgs(Point location, HitResult hitResult) {
534 HitResult = hitResult;
536 public Point Location { get; private set; }
537 public HitResult HitResult { get; set; }