annotate ServerMonitor/Win32Helpers.cs @ 40:c4fc74593a78 default tip

Mono fix
author Brad Greco <brad@bgreco.net>
date Sat, 13 Jun 2020 13:28:20 -0400
parents 7645122aa7a9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
1 ´╗┐using System;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
2 using System.Runtime.InteropServices;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
3 using System.Windows.Forms;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
5 namespace ServerMonitorApp
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
6 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
7 /// <summary>Methods for interacting with the Win32 API.</summary>
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
8 class Win32Helpers
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
9 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10 [DllImport("user32.dll")]
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
11 [return: MarshalAs(UnmanagedType.Bool)]
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12 static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
13
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
14 [StructLayout(LayoutKind.Sequential)]
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15 public struct FLASHWINFO
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
16 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
17 public UInt32 cbSize;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
18 public IntPtr hwnd;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
19 public UInt32 dwFlags;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
20 public UInt32 uCount;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
21 public UInt32 dwTimeout;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
22 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
23
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
24 public const int HWND_BROADCAST = 0xffff;
39
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
25
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
26 #if __MonoCS__
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
27 public static readonly int WM_SHOWMONITOR = -12345;
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
28 #else
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
29 public static readonly int WM_SHOWMONITOR = RegisterWindowMessage("WM_SHOWMONITOR");
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
30 #endif
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
31
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
32 [DllImport("user32")]
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
33 public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
34 [DllImport("user32")]
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
35 public static extern int RegisterWindowMessage(string message);
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
36
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
37 public enum FlashWindowFlags : uint
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
38 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
39 /// <summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
40 /// Stop flashing. The system restores the window to its original state.
39
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
41 /// </summary>
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
42 FLASHW_STOP = 0,
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
43
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
44 /// <summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
45 /// Flash the window caption
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
46 /// </summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
47 FLASHW_CAPTION = 1,
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
48
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
49 /// <summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
50 /// Flash the taskbar button.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
51 /// </summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
52 FLASHW_TRAY = 2,
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
53
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
54 /// <summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
55 /// Flash both the window caption and taskbar button.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
56 /// This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
57 /// </summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
58 FLASHW_ALL = 3,
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
59
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
60 /// <summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
61 /// Flash continuously, until the FLASHW_STOP flag is set.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
62 /// </summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
63 FLASHW_TIMER = 4,
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
64
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
65 /// <summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
66 /// Flash continuously until the window comes to the foreground.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
67 /// </summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
68 FLASHW_TIMERNOFG = 12
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
69 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
70
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
71 /// <summary>Flashes a window icon in the taskbar.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
72 /// <param name="form">The form to flash.</param>
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
73 public static bool FlashWindowEx(Form form)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
74 {
39
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
75 if (Helpers.IsMono()) {
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
76 return false;
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
77 }
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
78
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
79 IntPtr hWnd = form.Handle;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
80 FLASHWINFO fInfo = new FLASHWINFO();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
81
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
82 fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
83 fInfo.hwnd = hWnd;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
84 fInfo.dwFlags = (uint)FlashWindowFlags.FLASHW_TRAY;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
85 fInfo.uCount = 10;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
86 fInfo.dwTimeout = 0;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
87
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
88 return FlashWindowEx(ref fInfo);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
89 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
90
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
91 /// <summary>Stops flashing a window icon in the taskbar.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
92 /// <param name="form">The form to stop flashing.</param>
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
93 public static bool StopFlashWindowEx(Form form)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
94 {
39
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
95 if (Helpers.IsMono()) {
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
96 return false;
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
97 }
7645122aa7a9 Get it working under Mono
Brad Greco <brad@bgreco.net>
parents: 17
diff changeset
98
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
99 IntPtr hWnd = form.Handle;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
100 FLASHWINFO fInfo = new FLASHWINFO();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
101
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
102 fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
103 fInfo.hwnd = hWnd;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
104 fInfo.dwFlags = (uint)FlashWindowFlags.FLASHW_STOP;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
105 fInfo.dwTimeout = 0;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
106
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
107 return FlashWindowEx(ref fInfo);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
108 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
109 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
110 }