-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNativeMethods.cs
More file actions
130 lines (98 loc) · 4.58 KB
/
NativeMethods.cs
File metadata and controls
130 lines (98 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#region Using statements
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
#endregion Using statements
namespace HardTop
{
internal static class NativeMethods
{
#region Private constants used in user32 API's interaction
private const int WS_EX_TOPMOST = 0x00000008;
private const int GWL_EXSTYLE = -20;
#endregion Private constants used in user32 API's interaction
#region Private variables
private static List<IntPtr> WindowHandles;
private static List<string> WindowTitles;
#endregion Private variables
#region Delegates used in user32 API's interaction
private delegate bool EnumWindowsProc(IntPtr hwnd, ArrayList lParam);
private delegate bool EnumDelegate(IntPtr hWnd, int lParam);
#endregion Delegates used in user32 API's interaction
#region Internal functions for Window handling
internal static void GetDesktopWindowHandlesAndTitles(out List<IntPtr> handles, out List<string> titles)
{
WindowHandles = new List<IntPtr>();
WindowTitles = new List<string>();
if (!EnumDesktopWindows(IntPtr.Zero, GetDesktopWindowHandlesAndTitlesFilterCallback, IntPtr.Zero))
{
handles = null;
titles = null;
}
else
{
handles = WindowHandles;
titles = WindowTitles;
}
}
internal static ArrayList AlwaysOnTopWindows()
{
var topmostWindowHandles = new ArrayList();
EnumWindows(EnumWindowsCallback, topmostWindowHandles);
return topmostWindowHandles;
}
internal static void ToggleWindowAlwaysOnTop(IntPtr hwnd, bool alwaysOnTop)
{
SetWindowPos(hwnd, (IntPtr)(alwaysOnTop ? -1 : -2), 0, 0, 0, 0, 67);
}
#endregion Internal functions for Window handling
#region Private Callback methods; window handling
private static bool EnumWindowsCallback(IntPtr hWnd, ArrayList lParam)
{
int exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
if ((exStyle & WS_EX_TOPMOST) == WS_EX_TOPMOST) lParam.Add(hWnd);
return true;
}
private static bool GetDesktopWindowHandlesAndTitlesFilterCallback(IntPtr hWnd, int lParam)
{
StringBuilder sb_title = new StringBuilder(1024);
GetWindowText(hWnd, sb_title, sb_title.Capacity);
string title = sb_title.ToString();
if (IsWindowVisible(hWnd) && string.IsNullOrEmpty(title) == false)
{
WindowHandles.Add(hWnd);
WindowTitles.Add(title);
}
return true;
}
#endregion Private Callback methods; window handling
#region user32.dll function imports; window handling
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, ArrayList lParam);
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int xy, uint flagsw);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetWindowText",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
#endregion user32.dll function imports; window handling
#region user32.dll function import; used to free GDI+ icon from memory
[DllImport("user32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DestroyIcon(IntPtr handle);
#endregion user32.dll function import; used to free GDI+ icon from memory
}
}