Added a configurable font size and window width to 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 int r = 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 (int)((uint)message.LParam & 0xFFFF) - location.X,
127 (int)(((uint)message.LParam >> 16) & 0xFFFF) - location.Y);
128 HitTestEventArgs e = new HitTestEventArgs(p, HitResult.Caption);
130 message.Result = (IntPtr)e.HitResult;
133 case WM_NCLBUTTONDBLCLK: {
134 message.Result = IntPtr.Zero;
136 case WM_NCRBUTTONDOWN: {
137 message.Result = IntPtr.Zero;
139 case WM_NCRBUTTONUP: {
140 if (contextMenu != null)
141 ShowContextMenu(new Point(
142 (int)((uint)message.LParam & 0xFFFF),
143 (int)(((uint)message.LParam >> 16) & 0xFFFF)));
144 message.Result = IntPtr.Zero;
146 case WM_WINDOWPOSCHANGING: {
147 WindowPos wp = (WindowPos)Marshal.PtrToStructure(
148 message.LParam, typeof(WindowPos));
150 if (!lockPositionAndSize) {
151 // prevent the window from leaving the screen
152 if ((wp.flags & SWP_NOMOVE) == 0) {
153 Rectangle rect = Screen.GetWorkingArea(new Point(wp.x, wp.y));
154 const int margin = 16;
155 wp.x = Math.Max(wp.x, rect.Left - wp.cx + margin);
156 wp.x = Math.Min(wp.x, rect.Right - margin);
157 wp.y = Math.Max(wp.y, rect.Top - wp.cy + margin);
158 wp.y = Math.Min(wp.y, rect.Bottom - margin);
161 // update location and fire event
162 if ((wp.flags & SWP_NOMOVE) == 0) {
163 if (location.X != wp.x || location.Y != wp.y) {
164 location = new Point(wp.x, wp.y);
165 if (LocationChanged != null)
166 LocationChanged(this, EventArgs.Empty);
170 // update size and fire event
171 if ((wp.flags & SWP_NOSIZE) == 0) {
172 if (size.Width != wp.cx || size.Height != wp.cy) {
173 size = new Size(wp.cx, wp.cy);
174 if (SizeChanged != null)
175 SizeChanged(this, EventArgs.Empty);
179 // update the size of the layered window
180 if ((wp.flags & SWP_NOSIZE) == 0) {
181 NativeMethods.UpdateLayeredWindow(Handle, IntPtr.Zero,
182 IntPtr.Zero, ref size, IntPtr.Zero, IntPtr.Zero, 0,
186 // update the position of the layered window
187 if ((wp.flags & SWP_NOMOVE) == 0) {
188 NativeMethods.SetWindowPos(Handle, IntPtr.Zero,
189 location.X, location.Y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE |
190 SWP_NOZORDER | SWP_NOSENDCHANGING);
194 // do not forward any move or size messages
195 wp.flags |= SWP_NOSIZE | SWP_NOMOVE;
196 Marshal.StructureToPtr(wp, message.LParam, false);
197 message.Result = IntPtr.Zero;
200 base.WndProc(ref message);
205 private BlendFunction CreateBlendFunction() {
206 BlendFunction blend = new BlendFunction();
207 blend.BlendOp = AC_SRC_OVER;
208 blend.BlendFlags = 0;
209 blend.SourceConstantAlpha = opacity;
210 blend.AlphaFormat = AC_SRC_ALPHA;
214 public void Update(Bitmap bitmap) {
215 IntPtr screen = NativeMethods.GetDC(IntPtr.Zero);
216 IntPtr memory = NativeMethods.CreateCompatibleDC(screen);
217 IntPtr newHBitmap = IntPtr.Zero;
218 IntPtr oldHBitmap = IntPtr.Zero;
221 newHBitmap = bitmap.GetHbitmap(Color.Black);
222 oldHBitmap = NativeMethods.SelectObject(memory, newHBitmap);
224 Point pointSource = Point.Empty;
225 BlendFunction blend = CreateBlendFunction();
227 NativeMethods.UpdateLayeredWindow(Handle, screen, IntPtr.Zero,
228 ref size, memory, ref pointSource, 0, ref blend, ULW_ALPHA);
230 // make sure the window is at the right location
231 NativeMethods.SetWindowPos(Handle, IntPtr.Zero,
232 location.X, location.Y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE |
233 SWP_NOZORDER | SWP_NOSENDCHANGING);
236 if (newHBitmap != IntPtr.Zero) {
237 NativeMethods.SelectObject(memory, oldHBitmap);
238 NativeMethods.DeleteObject(newHBitmap);
240 NativeMethods.DeleteDC(memory);
241 NativeMethods.ReleaseDC(IntPtr.Zero, screen);
245 public byte Opacity {
250 if (opacity != value) {
252 BlendFunction blend = CreateBlendFunction();
253 NativeMethods.UpdateLayeredWindow(Handle, IntPtr.Zero, IntPtr.Zero,
254 IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 0, ref blend, ULW_ALPHA);
259 public bool Visible {
264 if (visible != value) {
266 NativeMethods.SetWindowPos(Handle, IntPtr.Zero, 0, 0, 0, 0,
267 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER |
268 (value ? SWP_SHOWWINDOW : SWP_HIDEWINDOW));
271 ShowDesktop.Instance.ShowDesktopChanged += ShowDesktopChanged;
274 ShowDesktop.Instance.ShowDesktopChanged -= ShowDesktopChanged;
280 // if locked, the window can not be moved or resized
281 public bool LockPositionAndSize {
283 return lockPositionAndSize;
286 lockPositionAndSize = value;
290 public bool AlwaysOnTop {
295 if (value != alwaysOnTop) {
299 ShowDesktop.Instance.ShowDesktopChanged -= ShowDesktopChanged;
300 MoveToTopMost(Handle);
302 MoveToBottom(Handle);
304 ShowDesktop.Instance.ShowDesktopChanged += ShowDesktopChanged;
317 NativeMethods.UpdateLayeredWindow(Handle, IntPtr.Zero, IntPtr.Zero,
318 ref size, IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero, 0);
319 if (SizeChanged != null)
320 SizeChanged(this, EventArgs.Empty);
325 public event EventHandler SizeChanged;
327 public Point Location {
332 if (location != value) {
334 NativeMethods.SetWindowPos(Handle, IntPtr.Zero,
335 location.X, location.Y, 0, 0,
336 SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSENDCHANGING);
337 if (LocationChanged != null)
338 LocationChanged(this, EventArgs.Empty);
343 public event EventHandler LocationChanged;
345 public ContextMenu ContextMenu {
350 this.contextMenu = value;
354 public event HitTestEventHandler HitTest;
356 [StructLayout(LayoutKind.Sequential, Pack = 1)]
357 private struct BlendFunction {
359 public byte BlendFlags;
360 public byte SourceConstantAlpha;
361 public byte AlphaFormat;
364 [StructLayout(LayoutKind.Sequential, Pack = 1)]
365 private struct WindowPos {
367 public IntPtr hwndInsertAfter;
375 public static readonly IntPtr HWND_BOTTOM = (IntPtr)1;
376 public static readonly IntPtr HWND_TOPMOST = (IntPtr)(-1);
378 public const int WS_EX_LAYERED = 0x00080000;
379 public const int WS_EX_TOOLWINDOW = 0x00000080;
381 public const uint SWP_NOSIZE = 0x0001;
382 public const uint SWP_NOMOVE = 0x0002;
383 public const uint SWP_NOACTIVATE = 0x0010;
384 public const uint SWP_HIDEWINDOW = 0x0080;
385 public const uint SWP_SHOWWINDOW = 0x0040;
386 public const uint SWP_NOZORDER = 0x0004;
387 public const uint SWP_NOSENDCHANGING = 0x0400;
389 public const int ULW_COLORKEY = 0x00000001;
390 public const int ULW_ALPHA = 0x00000002;
391 public const int ULW_OPAQUE = 0x00000004;
393 public const byte AC_SRC_OVER = 0x00;
394 public const byte AC_SRC_ALPHA = 0x01;
396 public const int WM_NCHITTEST = 0x0084;
397 public const int WM_NCLBUTTONDBLCLK = 0x00A3;
398 public const int WM_NCLBUTTONDOWN = 0x00A1;
399 public const int WM_NCLBUTTONUP = 0x00A2;
400 public const int WM_NCRBUTTONDOWN = 0x00A4;
401 public const int WM_NCRBUTTONUP = 0x00A5;
402 public const int WM_WINDOWPOSCHANGING = 0x0046;
403 public const int WM_COMMAND = 0x0111;
405 public const int TPM_RIGHTBUTTON = 0x0002;
406 public const int TPM_VERTICAL = 0x0040;
408 private enum WindowAttribute : int {
409 DWMWA_NCRENDERING_ENABLED = 1,
410 DWMWA_NCRENDERING_POLICY,
411 DWMWA_TRANSITIONS_FORCEDISABLED,
413 DWMWA_CAPTION_BUTTON_BOUNDS,
414 DWMWA_NONCLIENT_RTL_LAYOUT,
415 DWMWA_FORCE_ICONIC_REPRESENTATION,
417 DWMWA_EXTENDED_FRAME_BOUNDS,
418 DWMWA_HAS_ICONIC_BITMAP,
420 DWMWA_EXCLUDED_FROM_PEEK,
424 private static class NativeMethods {
425 private const string USER = "user32.dll";
426 private const string GDI = "gdi32.dll";
427 public const string DWMAPI = "dwmapi.dll";
429 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
430 [return: MarshalAs(UnmanagedType.Bool)]
431 public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst,
432 IntPtr pptDst, ref Size psize, IntPtr hdcSrc, IntPtr pprSrc,
433 int crKey, IntPtr pblend, int dwFlags);
435 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
436 [return: MarshalAs(UnmanagedType.Bool)]
437 public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst,
438 IntPtr pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc,
439 int crKey, ref BlendFunction pblend, int dwFlags);
441 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
442 [return: MarshalAs(UnmanagedType.Bool)]
443 public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst,
444 IntPtr pptDst, IntPtr psize, IntPtr hdcSrc, IntPtr pprSrc,
445 int crKey, ref BlendFunction pblend, int dwFlags);
447 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
448 public static extern IntPtr GetDC(IntPtr hWnd);
450 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
451 public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
453 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
454 public static extern bool SetWindowPos(IntPtr hWnd,
455 IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
457 [DllImport(USER, CallingConvention = CallingConvention.Winapi)]
458 public static extern bool TrackPopupMenuEx(IntPtr hMenu, uint uFlags,
459 int x, int y, IntPtr hWnd, IntPtr tpmParams);
461 [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
462 public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
464 [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
465 [return: MarshalAs(UnmanagedType.Bool)]
466 public static extern bool DeleteDC(IntPtr hdc);
468 [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
469 public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
471 [DllImport(GDI, CallingConvention = CallingConvention.Winapi)]
472 [return: MarshalAs(UnmanagedType.Bool)]
473 public static extern bool DeleteObject(IntPtr hObject);
475 [DllImport(DWMAPI, CallingConvention = CallingConvention.Winapi)]
476 public static extern int DwmSetWindowAttribute(IntPtr hwnd,
477 WindowAttribute dwAttribute, ref bool pvAttribute, int cbAttribute);
481 public enum HitResult {
497 public delegate void HitTestEventHandler(object sender, HitTestEventArgs e);
499 public class HitTestEventArgs : EventArgs {
500 public HitTestEventArgs(Point location, HitResult hitResult) {
502 HitResult = hitResult;
504 public Point Location { get; private set; }
505 public HitResult HitResult { get; set; }