-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowCaptureService.cs
More file actions
164 lines (141 loc) · 4.74 KB
/
Copy pathWindowCaptureService.cs
File metadata and controls
164 lines (141 loc) · 4.74 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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MonitorLauncher
{
public class WindowCaptureService
{
private static readonly HashSet<string> IgnoredProcessNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"ApplicationFrameHost",
"explorer",
"MonitorLauncher",
"ShellExperienceHost",
"StartMenuExperienceHost",
"SystemSettings",
"TextInputHost",
"WindowsTerminal"
};
public List<CapturedWindowInfo> CaptureOpenWindows()
{
var windows = new List<CapturedWindowInfo>();
Win32Api.EnumWindows((hWnd, lParam) =>
{
if (!TryCaptureWindow(hWnd, out var info) || info == null)
{
return true;
}
windows.Add(info);
return true;
}, IntPtr.Zero);
return windows
.OrderBy(window => window.ProcessName, StringComparer.CurrentCultureIgnoreCase)
.ThenBy(window => window.Title, StringComparer.CurrentCultureIgnoreCase)
.ToList();
}
private static bool TryCaptureWindow(IntPtr hWnd, out CapturedWindowInfo? info)
{
info = null;
if (!IsGeneralVisibleWindow(hWnd))
{
return false;
}
string title = GetWindowTitle(hWnd);
if (string.IsNullOrWhiteSpace(title))
{
return false;
}
Win32Api.GetWindowThreadProcessId(hWnd, out uint processId);
if (processId == 0)
{
return false;
}
Process process;
try
{
process = Process.GetProcessById((int)processId);
}
catch
{
return false;
}
if (IgnoredProcessNames.Contains(process.ProcessName))
{
return false;
}
if (!Win32Api.GetWindowRect(hWnd, out var rect) || rect.Width < 120 || rect.Height < 80)
{
return false;
}
string executablePath = GetExecutablePath(process);
var center = new Point(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2);
var monitor = Screen.AllScreens.FirstOrDefault(screen => screen.Bounds.Contains(center)) ?? Screen.PrimaryScreen;
info = new CapturedWindowInfo
{
Handle = hWnd,
Title = title,
ProcessName = process.ProcessName,
ExecutablePath = executablePath,
MonitorDeviceName = monitor?.DeviceName ?? string.Empty,
X = rect.Left,
Y = rect.Top,
Width = rect.Width,
Height = rect.Height,
IsMaximized = IsMaximized(hWnd)
};
return true;
}
public static bool IsGeneralVisibleWindow(IntPtr hWnd)
{
if (hWnd == IntPtr.Zero || !Win32Api.IsWindowVisible(hWnd))
{
return false;
}
int exStyle = Win32Api.GetWindowLong32(hWnd, Win32Api.GWL_EXSTYLE);
if ((exStyle & Win32Api.WS_EX_TOOLWINDOW) != 0)
{
return false;
}
if (!Win32Api.GetWindowRect(hWnd, out var rect))
{
return false;
}
return rect.Width > 0 && rect.Height > 0;
}
public static string GetWindowTitle(IntPtr hWnd)
{
int length = Win32Api.GetWindowTextLength(hWnd);
if (length <= 0)
{
return string.Empty;
}
var builder = new StringBuilder(length + 1);
Win32Api.GetWindowText(hWnd, builder, builder.Capacity);
return builder.ToString().Trim();
}
public static bool IsMaximized(IntPtr hWnd)
{
var placement = new Win32Api.WINDOWPLACEMENT
{
Length = System.Runtime.InteropServices.Marshal.SizeOf<Win32Api.WINDOWPLACEMENT>()
};
return Win32Api.GetWindowPlacement(hWnd, ref placement) &&
placement.ShowCmd == Win32Api.SW_SHOWMAXIMIZED;
}
private static string GetExecutablePath(Process process)
{
try
{
return process.MainModule?.FileName ?? string.Empty;
}
catch
{
return string.Empty;
}
}
}
}