-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWinApiHelpers.cs
216 lines (181 loc) · 8.24 KB
/
WinApiHelpers.cs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace MusicBeePlugin
{
public static class WinApiHelpers
{
const int WM_GETTEXT = 0x000D;
const int WM_GETTEXTLENGTH = 0x000E;
const int WM_SETTEXT = 0x000C;
const int WM_KEYDOWN = 0x0100;
const int WM_KEYUP = 0x0101;
const int VK_RETURN = 0x0D;
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
[DllImport("user32.dll", SetLastError = true)]
static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, StringBuilder lParam);
[DllImport("user32.dll", SetLastError = true)]
static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
static extern bool EnumChildWindows(IntPtr hWndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll")]
public static extern IntPtr GetFocus();
delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
static string lastNonEmptyText = string.Empty;
static bool EnumChildProc(IntPtr hWnd, IntPtr lParam)
{
StringBuilder className = new StringBuilder(256);
GetClassName(hWnd, className, className.Capacity);
if (className.ToString().Contains("EDIT"))
{
int length = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, IntPtr.Zero);
if (length > 0)
{
StringBuilder windowText = new StringBuilder(length + 1);
SendMessage(hWnd, WM_GETTEXT, windowText.Capacity, windowText);
if (windowText.Length > 0)
{
lastNonEmptyText = windowText.ToString();
}
}
}
return true;
}
public static string GetText(IntPtr hwnd)
{
int length = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, IntPtr.Zero);
if (length > 0)
{
StringBuilder windowText = new StringBuilder(length + 1);
SendMessage(hwnd, WM_GETTEXT, windowText.Capacity, windowText);
return windowText.ToString();
}
return string.Empty;
}
public static string GetLastNonEmptyEditText(IntPtr hwnd)
{
lastNonEmptyText = string.Empty;
EnumChildWindows(hwnd, EnumChildProc, IntPtr.Zero);
return lastNonEmptyText;
}
public static string GetClassNN(IntPtr hwnd)
{
StringBuilder className = new StringBuilder(256);
GetClassName(hwnd, className, className.Capacity);
return className.ToString();
}
public static List<IntPtr> GetEditHwnds(IntPtr hwnd)
{
List<IntPtr> editHwnds = new List<IntPtr>();
bool EnumChildProc(IntPtr hWnd, IntPtr lParam)
{
StringBuilder className = new StringBuilder(256);
GetClassName(hWnd, className, className.Capacity);
if (className.ToString().Contains("EDIT"))
{
editHwnds.Add(hWnd);
}
return true;
}
EnumChildWindows(hwnd, EnumChildProc, IntPtr.Zero);
return editHwnds;
}
public static void SetEditText(IntPtr hwndEdit, string text)
{
SendMessage(hwndEdit, WM_SETTEXT, 0, new StringBuilder(text));
}
public static void SendEnterKey(IntPtr hwndEdit)
{
SendMessage(hwndEdit, WM_KEYDOWN, VK_RETURN, IntPtr.Zero);
SendMessage(hwndEdit, WM_KEYUP, VK_RETURN, IntPtr.Zero);
}
public static bool SendAccepts(Keys keys)
{
var modifiers = keys & Keys.Modifiers;
return (modifiers == (modifiers & (Keys.Control | Keys.Alt | Keys.Shift)));
}
public static void SendKey(Keys keys)
{
var modifiers = keys & Keys.Modifiers;
string keysToSend = "";
// these are the only modifiers that SendKeys.Send supports
if ((modifiers & Keys.Control) == Keys.Control)
keysToSend += "^";
if ((modifiers & Keys.Shift) == Keys.Shift)
keysToSend += "+";
if ((modifiers & Keys.Alt) == Keys.Alt)
keysToSend += "%";
var input = keys & ~Keys.Modifiers;
switch (input)
{
case Keys.Back: keysToSend += "{BACKSPACE}"; break;
case Keys.CapsLock: keysToSend += "{CAPSLOCK}"; break;
case Keys.Delete: keysToSend += "{DELETE}"; break;
case Keys.Down: keysToSend += "{DOWN}"; break;
case Keys.End: keysToSend += "{END}"; break;
case Keys.Enter: keysToSend += "{ENTER}"; break;
case Keys.Escape: keysToSend += "{ESC}"; break;
case Keys.Help: keysToSend += "{HELP}"; break;
case Keys.Home: keysToSend += "{HOME}"; break;
case Keys.Insert: keysToSend += "{INSERT}"; break;
case Keys.Left: keysToSend += "{LEFT}"; break;
case Keys.NumLock: keysToSend += "{NUMLOCK}"; break;
case Keys.PageDown: keysToSend += "{PGDN}"; break;
case Keys.PageUp: keysToSend += "{PGUP}"; break;
case Keys.PrintScreen: keysToSend += "{PRTSC}"; break;
case Keys.Right: keysToSend += "{RIGHT}"; break;
case Keys.Scroll: keysToSend += "{SCROLLLOCK}"; break;
case Keys.Tab: keysToSend += "{TAB}"; break;
case Keys.Up: keysToSend += "{UP}"; break;
case Keys.F1: keysToSend += "{F1}"; break;
case Keys.F2: keysToSend += "{F2}"; break;
case Keys.F3: keysToSend += "{F3}"; break;
case Keys.F4: keysToSend += "{F4}"; break;
case Keys.F5: keysToSend += "{F5}"; break;
case Keys.F6: keysToSend += "{F6}"; break;
case Keys.F7: keysToSend += "{F7}"; break;
case Keys.F8: keysToSend += "{F8}"; break;
case Keys.F9: keysToSend += "{F9}"; break;
case Keys.F10: keysToSend += "{F10}"; break;
case Keys.F11: keysToSend += "{F11}"; break;
case Keys.F12: keysToSend += "{F12}"; break;
case Keys.F13: keysToSend += "{F13}"; break;
case Keys.F14: keysToSend += "{F14}"; break;
case Keys.F15: keysToSend += "{F15}"; break;
case Keys.F16: keysToSend += "{F16}"; break;
case Keys.Add: keysToSend += "{ADD}"; break;
case Keys.Subtract: keysToSend += "{SUBTRACT}"; break;
case Keys.Multiply: keysToSend += "{MULTIPLY}"; break;
case Keys.Divide: keysToSend += "{DIVIDE}"; break;
default: keysToSend += input.ToString().ToLower(); break;
}
SendKeys.SendWait(keysToSend);
}
public static void CenterForm(Form form, IntPtr parentHandle)
{
if (parentHandle != IntPtr.Zero)
{
var parentWindow = Control.FromHandle(parentHandle);
if (parentWindow != null)
{
var parentBounds = parentWindow.Bounds;
form.StartPosition = FormStartPosition.Manual;
form.Location = new Point(
parentBounds.X + (parentBounds.Width - form.Width) / 2,
parentBounds.Y + (parentBounds.Height - form.Height) / 2
);
}
}
}
}
}