Fixed an issue where the gadget would not be movable on an extra monitor positioned to the left of the primary monitor.
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 (int)((uint)message.LParam & 0xFFFF),
144 (int)(((uint)message.LParam >> 16) & 0xFFFF)));
145 message.Result = IntPtr.Zero;
147 case WM_WINDOWPOSCHANGING: {
148 WindowPos wp = (WindowPos)Marshal.PtrToStructure(
149 message.LParam, typeof(WindowPos));
151 if (!lockPositionAndSize) {
152 // prevent the window from leaving the screen
153 if ((wp.flags & SWP_NOMOVE) == 0) {
154 Rectangle rect = Screen.GetWorkingArea(new Point(wp.x, wp.y));
155 const int margin = 16;
156 wp.x = Math.Max(wp.x, rect.Left - wp.cx + margin);
157 wp.x = Math.Min(wp.x, rect.Right - margin);
158 wp.y = Math.Max(wp.y, rect.Top - wp.cy + margin);
159 wp.y = Math.Min(wp.y, rect.Bottom - margin);
162 // update location and fire event
163 if ((wp.flags & SWP_NOMOVE) == 0) {
164 if (location.X != wp.x || location.Y != wp.y) {
165 location = new Point(wp.x, wp.y);
166 if (LocationChanged != null)
167 LocationChanged(this, EventArgs.Empty);
171 // update size and fire event
172 if ((wp.flags & SWP_NOSIZE) == 0) {
173 if (size.Width != wp.cx || size.Height != wp.cy) {
174 size = new Size(wp.cx, wp.cy);
175 if (SizeChanged != null)
176 SizeChanged(this, EventArgs.Empty);
180 // update the size of the layered window
181 if ((wp.flags & SWP_NOSIZE) == 0) {
182 NativeMethods.UpdateLayeredWindow(Handle, IntPtr.Zero,
183 IntPtr.Zero, ref size, IntPtr.Zero, IntPtr.Zero, 0,
187 // update the position of the layered window
188 if ((wp.flags & SWP_NOMOVE) == 0) {
189 NativeMethods.SetWindowPos(Handle, IntPtr.Zero,
190 location.X, location.Y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE |
191 SWP_NOZORDER | SWP_NOSENDCHANGING);
195 // do not forward any move or size messages
196 wp.flags |= SWP_NOSIZE | SWP_NOMOVE;
197 Marshal.StructureToPtr(wp, message.LParam, false);
198 message.Result = IntPtr.Zero;
201 base.WndProc(ref message);
206 private BlendFunction CreateBlendFunction() {
207 BlendFunction blend = new BlendFunction();
208 blend.BlendOp = AC_SRC_OVER;
209 blend.BlendFlags = 0;
210 blend.SourceConstantAlpha = opacity;
211 blend.AlphaFormat = AC_SRC_ALPHA;
215 public void Update(Bitmap bitmap) {
216 IntPtr screen = NativeMethods.GetDC(IntPtr.Zero);
217 IntPtr memory = NativeMethods.CreateCompatibleDC(screen);
218 IntPtr newHBitmap = IntPtr.Zero;
219 IntPtr oldHBitmap = IntPtr.Zero;
222 newHBitmap = bitmap.GetHbitmap(Color.Black);
223 oldHBitmap = NativeMethods.SelectObject(memory, newHBitmap);
225 Point pointSource = Point.Empty;
226 BlendFunction blend = CreateBlendFunction();
228 NativeMethods.UpdateLayeredWindow(Handle, screen, IntPtr.Zero,
229 ref size, memory, ref pointSource, 0, ref blend, ULW_ALPHA);
231 // make sure the window is at the right location
232 NativeMethods.SetWindowPos(Handle, IntPtr.Zero,
233 location.X, location.Y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE |
234 SWP_NOZORDER | SWP_NOSENDCHANGING);
237 if (newHBitmap != IntPtr.Zero) {
238 NativeMethods.SelectObject(memory, oldHBitmap);
239 NativeMethods.DeleteObject(newHBitmap);
241 NativeMethods.DeleteDC(memory);
242 NativeMethods.ReleaseDC(IntPtr.Zero, screen);
246 public byte Opacity {
251 if (opacity != value) {
253 BlendFunction blend = CreateBlendFunction();
254 NativeMethods.UpdateLayeredWindow(Handle, IntPtr.Zero, IntPtr.Zero,
255 IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 0, ref blend, ULW_ALPHA);
260 public bool Visible {
265 if (visible != value) {
267 NativeMethods.SetWindowPos(Handle, IntPtr.Zero, 0, 0, 0, 0,
268 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER |
269 (value ? SWP_SHOWWINDOW : SWP_HIDEWINDOW));
272 ShowDesktop.Instance.ShowDesktopChanged += ShowDesktopChanged;
275 ShowDesktop.Instance.ShowDesktopChanged -= ShowDesktopChanged;
281 // if locked, the window can not be moved or resized
282 public bool LockPositionAndSize {
284 return lockPositionAndSize;
287 lockPositionAndSize = value;
291 public bool AlwaysOnTop {
296 if (value != alwaysOnTop) {
300 ShowDesktop.Instance.ShowDesktopChanged -= ShowDesktopChanged;
301 MoveToTopMost(Handle);
303 MoveToBottom(Handle);
305 ShowDesktop.Instance.ShowDesktopChanged += ShowDesktopChanged;
318 NativeMethods.UpdateLayeredWindow(Handle, IntPtr.Zero, IntPtr.Zero,
319 ref size, IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero, 0);
320 if (SizeChanged != null)
321 SizeChanged(this, EventArgs.Empty);
326 public event EventHandler SizeChanged;
328 public Point Location {
333 if (location != value) {
335 NativeMethods.SetWindowPos(Handle, IntPtr.Zero,
336 location.X, location.Y, 0, 0,
337 SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSENDCHANGING);
338 if (LocationChanged != null)
339 LocationChanged(this, EventArgs.Empty);
344 public event EventHandler LocationChanged;
346 public ContextMenu ContextMenu {
351 this.contextMenu = value;
355 public event HitTestEventHandler HitTest;
357 [StructLayout(LayoutKind.Sequential, Pack = 1)]
358 private struct BlendFunction {
360 public byte BlendFlags;
361 public byte SourceConstantAlpha;
362 public byte AlphaFormat;
365 [StructLayout(LayoutKind.Sequential, Pack = 1)]
366 private struct WindowPos {
368 public IntPtr hwndInsertAfter;
376 public static readonly IntPtr HWND_BOTTOM = (IntPtr)1;
377 public static readonly IntPtr HWND_TOPMOST = (IntPtr)(-1);
379 public const int WS_EX_LAYERED = 0x00080000;
380 public const int WS_EX_TOOLWINDOW = 0x00000080;
382 public const uint SWP_NOSIZE = 0x0001;
383 public const uint SWP_NOMOVE = 0x0002;
384 public const uint SWP_NOACTIVATE = 0x0010;
385 public const uint SWP_HIDEWINDOW = 0x0080;
386 public const uint SWP_SHOWWINDOW = 0x0040;
387 public const uint SWP_NOZORDER = 0x0004;
388 public const uint SWP_NOSENDCHANGING = 0x0400;
390 public const int ULW_COLORKEY = 0x00000001;
391 public const int ULW_ALPHA = 0x00000002;
392 public const int ULW_OPAQUE = 0x00000004;
394 public const byte AC_SRC_OVER = 0x00;
395 public const byte AC_SRC_ALPHA = 0x01;
397 public const int WM_NCHITTEST = 0x0084;
398 public const int WM_NCLBUTTONDBLCLK = 0x00A3;
399 public const int WM_NCLBUTTONDOWN = 0x00A1;
400 public const int WM_NCLBUTTONUP = 0x00A2;
401 public const int WM_NCRBUTTONDOWN = 0x00A4;
402 public const int WM_NCRBUTTONUP = 0x00A5;
403 public const int WM_WINDOWPOSCHANGING = 0x0046;
404 public const int WM_COMMAND = 0x0111;
406 public const int TPM_RIGHTBUTTON = 0x0002;
407 public const int TPM_VERTICAL = 0x0040;
409 private enum WindowAttribute : int {
410 DWMWA_NCRENDERING_ENABLED = 1,
411 DWMWA_NCRENDERING_POLICY,
412 DWMWA_TRANSITIONS_FORCEDISABLED,
414 DWMWA_CAPTION_BUTTON_BOUNDS,
415 DWMWA_NONCLIENT_RTL_LAYOUT,
416 DWMWA_FORCE_ICONIC_REPRESENTATION,
418 DWMWA_EXTENDED_FRAME_BOUNDS,
419 DWMWA_HAS_ICONIC_BITMAP,
421 DWMWA_EXCLUDED_FROM_PEEK,
426 /// Some macros imported and converted from the Windows SDK
428 private static class Macros {
429 public static UInt16 LOWORD(IntPtr l) {
430 return ((UInt16) (((UInt64) (l)) & 0xffff));
433 public static UInt16 HIWORD(IntPtr l) {
434 return ((UInt16) ((((UInt64) (l)) >> 16) & 0xffff));
437 public static int GET_X_LPARAM(IntPtr lp) {
438 return ((int) (short) LOWORD(lp));
441 public static int GET_Y_LPARAM(IntPtr lp) {
442 return ((int) (short) HIWORD(lp));
447 /// Imported native methods
449 private static class NativeMethods {
450 private const string USER = "user32.dll";
451 private const string GDI = "gdi32.dll";
452 public const string DWMAPI = "dwmapi.dll";
454 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
455 [return: MarshalAs(UnmanagedType.Bool)]
456 public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst,
457 IntPtr pptDst, ref Size psize, IntPtr hdcSrc, IntPtr pprSrc,
458 int crKey, IntPtr pblend, int dwFlags);
460 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
461 [return: MarshalAs(UnmanagedType.Bool)]
462 public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst,
463 IntPtr pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc,
464 int crKey, ref BlendFunction pblend, int dwFlags);
466 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
467 [return: MarshalAs(UnmanagedType.Bool)]
468 public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst,
469 IntPtr pptDst, IntPtr psize, IntPtr hdcSrc, IntPtr pprSrc,
470 int crKey, ref BlendFunction pblend, int dwFlags);
472 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
473 public static extern IntPtr GetDC(IntPtr hWnd);
475 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
476 public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
478 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
479 public static extern bool SetWindowPos(IntPtr hWnd,
480 IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
482 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
483 public static extern bool TrackPopupMenuEx(IntPtr hMenu, uint uFlags,
484 int x, int y, IntPtr hWnd, IntPtr tpmParams);
486 [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
487 public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
489 [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
490 [return: MarshalAs(UnmanagedType.Bool)]
491 public static extern bool DeleteDC(IntPtr hdc);
493 [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
494 public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
496 [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
497 [return: MarshalAs(UnmanagedType.Bool)]
498 public static extern bool DeleteObject(IntPtr hObject);
500 [DllImport(DWMAPI, CallingConvention = CallingConvention.Winapi)]
501 public static extern int DwmSetWindowAttribute(IntPtr hwnd,
502 WindowAttribute dwAttribute, ref bool pvAttribute, int cbAttribute);
506 public enum HitResult {
522 public delegate void HitTestEventHandler(object sender, HitTestEventArgs e);
524 public class HitTestEventArgs : EventArgs {
525 public HitTestEventArgs(Point location, HitResult hitResult) {
527 HitResult = hitResult;
529 public Point Location { get; private set; }
530 public HitResult HitResult { get; set; }