-
Notifications
You must be signed in to change notification settings - Fork 0
/
Win32Utils.cs
72 lines (65 loc) · 2.27 KB
/
Win32Utils.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.WindowsAndMessaging;
namespace Listary.FileAppPlugin.DirectoryOpus
{
internal static class Win32Utils
{
public static string GetClassName(HWND hWnd)
{
unsafe
{
char* className = stackalloc char[256];
PInvoke.GetClassName(hWnd, className, 256);
return new string(className);
}
}
public static HWND GetFocus(HWND hWnd)
{
unsafe
{
uint threadId = PInvoke.GetWindowThreadProcessId(hWnd);
GUITHREADINFO threadInfo = new GUITHREADINFO { };
threadInfo.cbSize = (uint)Marshal.SizeOf(threadInfo);
if (PInvoke.GetGUIThreadInfo(threadId, ref threadInfo))
{
return threadInfo.hwndFocus;
}
}
return default;
}
internal static string GetWindowText(HWND hWnd)
{
unsafe
{
char* buffer = stackalloc char[260];
// WM_GETTEXT won't be dropped by UIPI
PInvoke.SendMessage(hWnd, PInvoke.WM_GETTEXT, 260, (nint)buffer);
return new(buffer);
}
}
internal static HWND FindWindowExRecursively(HWND hWndParent, HWND hWndChildAfter, string lpszClass, string lpszTitle)
{
HWND result = PInvoke.FindWindowEx(hWndParent, hWndChildAfter, lpszClass, lpszTitle);
if (result != default)
return result;
HWND child = PInvoke.FindWindowEx(hWndParent, hWndChildAfter, default(PCWSTR), default);
while (child != default)
{
result = FindWindowExRecursively(child, default, lpszClass, lpszTitle);
if (result != default)
{
return result;
}
child = PInvoke.FindWindowEx(hWndParent, child, default(PCWSTR), default);
}
return default;
}
}
}