forked from Excel-DNA/IntelliSense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWin32Helper.cs
349 lines (291 loc) · 12 KB
/
Win32Helper.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using ExcelDna.Integration;
namespace ExcelDna.IntelliSense
{
static class Win32Helper
{
enum WM : uint
{
GETTEXT = 0x000D,
GETTEXTLENGTH = 0x000E,
EM_POSFROMCHAR = 214,
LVM_FIRST = 0x1000,
LVM_GETITEM = (LVM_FIRST + 5),
LVM_GETNEXTITEM = (LVM_FIRST + 12),
LVM_GETITEMRECT = (LVM_FIRST + 14),
LVM_GETITEMTEXTW = (LVM_FIRST + 115),
LVNI_SELECTED = 0x0002,
}
[DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("kernel32.dll")]
static extern uint GetCurrentProcessId();
[DllImport("user32.dll")]
static extern int GetClassNameW(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder buf, int nMaxCount);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("gdi32.dll")]
internal static extern IntPtr CreateRoundRectRgn(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // height of ellipse
int nHeightEllipse); // width of ellipse
[DllImport("gdi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeleteObject([In] IntPtr hObject);
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, WM Msg, IntPtr wParam, [Out] StringBuilder lParam);
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, WM Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, WM Msg, IntPtr wParam, ref LV_ITEM lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, WM Msg, IntPtr wParam, ref RECT lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetCursorPos(out Point lpPoint);
[DllImport("user32.dll")]
static extern bool ScreenToClient(IntPtr hWnd, ref Point lpPoint);
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);
struct GUITHREADINFO
{
public int cbSize;
public int flags;
public IntPtr hwndActive;
public IntPtr hwndFocus;
public IntPtr hwndCapture;
public IntPtr hwndMenuOwner;
public IntPtr hwndMoveSize;
public IntPtr hwndCaret;
public Rectangle rcCaret;
}
// Different to Rectangle ...?
[StructLayout(LayoutKind.Sequential)]
struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hwnd, out RECT rect);
enum GetAncestorFlags
{
// Retrieves the parent window. This does not include the owner, as it does with the GetParent function.
GetParent = 1,
// Retrieves the root window by walking the chain of parent windows.
GetRoot = 2,
// Retrieves the owned root window by walking the chain of parent and owner windows returned by GetParent.
GetRootOwner = 3
}
[DllImport("user32.dll")]
static extern IntPtr GetAncestor(IntPtr hwnd, GetAncestorFlags flags);
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
public static extern IntPtr SetCapture(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
// Returns the WindowHandle of the focused window, if that window is in our process.
public static IntPtr GetFocusedWindowHandle()
{
var info = new GUITHREADINFO();
info.cbSize = Marshal.SizeOf(info);
if (!GetGUIThreadInfo(0, ref info))
return IntPtr.Zero;
var focusedWindow = info.hwndFocus;
if (focusedWindow == IntPtr.Zero)
return focusedWindow;
uint processId;
uint threadId = GetWindowThreadProcessId(focusedWindow, out processId);
if (threadId == 0)
return IntPtr.Zero;
uint currentProcessId = GetCurrentProcessId();
if (processId == currentProcessId)
return focusedWindow;
return IntPtr.Zero;
}
// Should return null if there is no such ancestor
public static IntPtr GetRootAncestor(IntPtr hWnd)
{
return GetAncestor(hWnd, GetAncestorFlags.GetRoot);
}
public static System.Drawing.Point GetClientCursorPos(IntPtr hWnd)
{
Point pt;
bool ok = GetCursorPos(out pt);
bool ok2 = ScreenToClient(hWnd, ref pt);
return pt;
}
// We use System.Windows.Rect to be consistent with the UIAutomation we used initially.
// Returns Rect.Empty if the Win32 call fails (Window is not visible?)
// Returns the window bounds in
public static System.Windows.Rect GetWindowBounds(IntPtr hWnd)
{
RECT rect; // This struct layout is like Win32 RECT (not like System.Drawing.Rectangle)
if (GetWindowRect(hWnd, out rect))
{
return new System.Windows.Rect(rect.Left, rect.Top, rect.Right - rect.Left + 1, rect.Bottom - rect.Top + 1);
}
else
{
return System.Windows.Rect.Empty;
}
}
public static string GetWindowTextRaw(IntPtr hwnd)
{
// Allocate correct string length first
int length = (int)SendMessage(hwnd, WM.GETTEXTLENGTH, IntPtr.Zero, null);
StringBuilder sb = new StringBuilder(length + 1);
SendMessage(hwnd, WM.GETTEXT, (IntPtr)sb.Capacity, sb);
return sb.ToString();
}
const int SW_HIDE = 0;
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public static bool HideWindow(IntPtr hWnd)
{
// Happy to suppress any errors here
try
{
return ShowWindow(hWnd, SW_HIDE);
}
catch (Exception ex)
{
Debug.Print($"Win32Helper.HideWindow Error: {ex.ToString()}");
return false;
}
}
public static string GetXllName()
{
return ExcelDnaUtil.XllPath;
}
public static IntPtr GetXllModuleHandle()
{
return GetModuleHandle(GetXllName());
}
public static uint GetExcelProcessId()
{
return GetCurrentProcessId();
}
static StringBuilder _buffer = new StringBuilder(65000);
public static string GetClassName(IntPtr hWnd)
{
_buffer.Length = 0;
GetClassNameW(hWnd, _buffer, _buffer.Capacity);
return _buffer.ToString();
}
public static string GetText(IntPtr hWnd)
{
// Allocate correct string length first
int length = GetWindowTextLength(hWnd);
var sb = new StringBuilder(length + 1);
GetWindowText(hWnd, sb, sb.Capacity);
return sb.ToString();
}
public static int GetPosFromChar(IntPtr hWnd, int ch)
{
return (int)SendMessage(hWnd, WM.EM_POSFROMCHAR, new IntPtr(ch), IntPtr.Zero);
}
internal static int GetListViewSelectedItemIndex(IntPtr hwndPopupList)
{
return 1;
}
const int LVIR_BOUNDS = 0;
[StructLayoutAttribute(LayoutKind.Sequential)]
struct LV_ITEM
{
public uint mask;
public int iItem;
public int iSubItem;
public uint state;
public uint stateMask;
public IntPtr pszText;
public int cchTextMax;
public int iImage;
public IntPtr lParam;
}
// Sets text to emptyh string if failed to find an item, or another error
internal static int GetListViewSelectedItemInfo(IntPtr hwndList, out string text, out System.Windows.Rect bounds)
{
string listViewClassName = GetClassName(hwndList);
//Debug.Assert(listViewClassName != "SysListView32");
var selectedItemIndex = (int)SendMessage(hwndList, WM.LVM_GETNEXTITEM, new IntPtr(-1), new IntPtr((int)WM.LVNI_SELECTED));
if (selectedItemIndex == -1)
{
text = string.Empty;
bounds = System.Windows.Rect.Empty;
return selectedItemIndex;
}
// Debug.Print($"#### PopupList SelectedItemIndex: {selectedItemIndex}");
// First get text
LV_ITEM item = new LV_ITEM();
item.mask = /*public const int LVIF_TEXT = */ 0x00000001;
item.iSubItem = 0;
IntPtr nativeBuffer = Marshal.AllocHGlobal(512 * 2); // There might be a more elegant way to do this, sith a StringBuilder or something...
for (int i = 0; i < 512; ++i)
{
Marshal.WriteInt16(nativeBuffer, i * 2, '\0');
}
try
{
item.pszText = nativeBuffer;
item.cchTextMax = 512;
uint length = (uint)SendMessage(hwndList, WM.LVM_GETITEMTEXTW, new IntPtr(selectedItemIndex), ref item);
if (length > 0)
{
text = Marshal.PtrToStringUni(item.pszText, (int)length);
}
else
{
text = string.Empty;
}
}
finally
{
Marshal.FreeHGlobal(nativeBuffer);
}
// Now get bounds
RECT rect = new RECT();
rect.Left = LVIR_BOUNDS;
uint ok = (uint)SendMessage(hwndList, WM.LVM_GETITEMRECT, new IntPtr(selectedItemIndex), ref rect);
if (ok != 0)
bounds = new System.Windows.Rect(rect.Left, rect.Top, rect.Right - rect.Left + 1, rect.Bottom - rect.Top + 1);
else
bounds = System.Windows.Rect.Empty;
Debug.Print($"#### >>> {selectedItemIndex} / {ok} / ({rect.Left}, {rect.Top}, {rect.Right}, {rect.Bottom}) / {bounds}");
return selectedItemIndex;
}
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetWindow(IntPtr hWnd, GW uCmd);
enum GW : uint
{
GW_HWNDFIRST = 0,
GW_HWNDLAST = 1,
GW_HWNDNEXT = 2,
GW_HWNDPREV = 3,
GW_OWNER = 4,
GW_CHILD = 5,
GW_ENABLEDPOPUP = 6
}
internal static IntPtr GetFirstChildWindow(IntPtr hwndPopupList)
{
return GetWindow(hwndPopupList, GW.GW_CHILD);
}
}
}