-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormSelectProcess.cs
More file actions
197 lines (174 loc) · 8 KB
/
FormSelectProcess.cs
File metadata and controls
197 lines (174 loc) · 8 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
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
using System.Diagnostics;
using WindowPlacementManager.Services;
namespace WindowPlacementManager;
public partial class FormSelectProcess : Form
{
public Process SelectedProcess { get; private set; }
public IntPtr SelectedWindowHandle { get; private set; }
public string SelectedWindowTitle { get; private set; }
bool isSelfElevated;
public class WindowSelectionInfo
{
public IntPtr HWnd { get; set; }
public string Title { get; set; }
public string ProcessName { get; set; }
public int ProcessId { get; set; }
public bool IsElevated { get; set; }
public bool AccessDeniedCheckingElevation { get; set; }
public override string ToString() => $"[{ProcessName} ({ProcessId})] {Title}";
}
public FormSelectProcess()
{
InitializeComponent();
isSelfElevated = ProcessPrivilegeChecker.IsCurrentProcessElevated();
SetupAdminHint();
}
void SetupAdminHint()
{
if(this.pictureBoxAdminHint == null || this.toolTipAdminHint == null)
{
Debug.WriteLine("Admin hint UI elements not initialized in FormSelectProcess designer.");
return;
}
this.pictureBoxAdminHint.Visible = true;
if(isSelfElevated)
{
this.pictureBoxAdminHint.Image = SystemIcons.Information.ToBitmap();
this.toolTipAdminHint.ToolTipIcon = ToolTipIcon.Info;
this.toolTipAdminHint.ToolTipTitle = "Administrator Privileges";
string hintText = "Window Placement Manager is running with Administrator privileges.\nThis allows for better interaction with other applications.";
this.toolTipAdminHint.SetToolTip(this.pictureBoxAdminHint, hintText);
}
else
{
this.pictureBoxAdminHint.Image = SystemIcons.Warning.ToBitmap();
this.toolTipAdminHint.ToolTipIcon = ToolTipIcon.Warning;
this.toolTipAdminHint.ToolTipTitle = "Administrator Privileges";
string hintText = "Window Placement Manager is NOT running as Administrator.\n" +
"This may limit its ability to:\n" +
" • Get executable paths for elevated/admin processes.\n" +
" • Launch or manage windows of elevated/admin processes.\n" +
" • Reliably determine admin status for all processes (may show N/A).\n\n" +
"For full functionality, consider running as Administrator.";
this.toolTipAdminHint.SetToolTip(this.pictureBoxAdminHint, hintText);
}
}
void FormSelectProcess_Load(object sender, EventArgs e) => LoadWindowsToList();
void buttonRefresh_Click(object sender, EventArgs e) => LoadWindowsToList();
void listViewProcesses_SelectedIndexChanged(object sender, EventArgs e) => buttonSelect.Enabled = listViewProcesses.SelectedItems.Count > 0;
void LoadWindowsToList()
{
listViewProcesses.Items.Clear();
List<WindowSelectionInfo> windows = new List<WindowSelectionInfo>();
Native.EnumWindows((hWnd, lParam) =>
{
if(!Native.IsWindowVisible(hWnd)) return true;
string windowTitle = Native.GetWindowTitle(hWnd);
if(string.IsNullOrWhiteSpace(windowTitle) || windowTitle.Length < 3) return true;
Native.GetWindowThreadProcessId(hWnd, out uint pid);
if(pid == 0) return true;
string procName = "N/A";
bool isElevated = false;
bool accessDeniedChecking = false;
try
{
using Process process = Process.GetProcessById((int)pid);
procName = process.ProcessName;
if(!process.HasExited)
{
isElevated = ProcessPrivilegeChecker.IsProcessElevated(process.Id, out accessDeniedChecking);
}
}
catch(ArgumentException) { return true; }
catch(Exception ex)
{
Debug.WriteLine($"Error processing PID {(int)pid} (current procName: '{procName}'): {ex.Message}");
}
windows.Add(new WindowSelectionInfo
{
HWnd = hWnd,
Title = windowTitle,
ProcessName = procName,
ProcessId = (int)pid,
IsElevated = isElevated,
AccessDeniedCheckingElevation = accessDeniedChecking
});
return true;
}, IntPtr.Zero);
foreach(var winInfo in windows.OrderBy(w => w.ProcessName).ThenBy(w => w.Title))
{
var item = new ListViewItem(winInfo.ProcessName);
item.SubItems.Add(winInfo.ProcessId.ToString());
item.SubItems.Add(winInfo.Title);
string adminStatusText = winInfo.IsElevated ? "Yes" : "No";
item.ToolTipText = "";
if(winInfo.AccessDeniedCheckingElevation && !winInfo.IsElevated)
{
adminStatusText = "N/A";
item.ToolTipText = "Admin status could not be fully determined (Access Denied or other issue).";
}
item.SubItems.Add(adminStatusText);
if(winInfo.IsElevated)
{
item.ForeColor = Color.DarkGoldenrod;
if(!isSelfElevated)
{
item.ToolTipText += (string.IsNullOrEmpty(item.ToolTipText) ? "" : "\n") +
"This process is elevated. WPM may have limited interaction if not run as admin.";
}
}
else if(adminStatusText == "N/A" && !isSelfElevated)
{
item.ForeColor = Color.OrangeRed;
item.ToolTipText += (string.IsNullOrEmpty(item.ToolTipText) ? "" : "\n") +
"Admin status uncertain due to access restrictions. WPM is not admin.";
}
else
{
item.ForeColor = listViewProcesses.ForeColor;
}
item.Tag = winInfo;
listViewProcesses.Items.Add(item);
}
if(listViewProcesses.Items.Count > 0)
{
listViewProcesses.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
listViewProcesses.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
if(listViewProcesses.Columns.Count > 2 && listViewProcesses.Columns[2].Width < 350)
{
listViewProcesses.Columns[2].Width = 350;
}
if(listViewProcesses.Columns.Count > 3)
{
listViewProcesses.Columns[3].Width = Math.Max(65, listViewProcesses.Columns[3].Width);
listViewProcesses.Columns[3].TextAlign = HorizontalAlignment.Center;
}
}
}
void ConfirmSelection()
{
if(listViewProcesses.SelectedItems.Count > 0)
{
var selectedWinInfo = listViewProcesses.SelectedItems[0].Tag as WindowSelectionInfo;
if(selectedWinInfo != null)
{
try
{
SelectedProcess = Process.GetProcessById(selectedWinInfo.ProcessId);
if(SelectedProcess.HasExited) throw new ArgumentException("Process has exited.");
}
catch(ArgumentException ex)
{
MessageBox.Show($"The process '{selectedWinInfo.ProcessName}' (PID: {selectedWinInfo.ProcessId}) is no longer running or accessible.\n{ex.Message}", "Process Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
LoadWindowsToList(); return;
}
SelectedWindowHandle = selectedWinInfo.HWnd;
SelectedWindowTitle = selectedWinInfo.Title;
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}
void buttonSelect_Click(object sender, EventArgs e) => ConfirmSelection();
void listViewProcesses_DoubleClick(object sender, EventArgs e) => ConfirmSelection();
}