-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTSX_Process.cs
172 lines (147 loc) · 6.74 KB
/
TSX_Process.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
// --------------------------------------------------------------------------------
// VariScan module
//
// Description:
//
// Environment: Windows 10 executable, 32 and 64 bit
//
// Usage: TBD
//
// Author: (REM) Rick McAlister, [email protected]
//
// Edit Log: Rev 1.0 Initial Version
//
// Date Who Vers Description
// ----------- --- ----- -------------------------------------------------------
//
// ---------------------------------------------------------------------------------
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace VariScan
{
public static class TSX_Process
{
public static void DeleteSRC(string folder)
{
//Bug in TSX causes hard stop if SRC file is locked by another process
// so delete all .SRC files in folder
string[] srcFiles = Directory.GetFiles(folder, "*.SRC", SearchOption.AllDirectories);
foreach (string f in srcFiles)
try { File.Delete(f); }
catch { }
return;
}
public static void MinimizeTSX()
{
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
if (process.ProcessName.Contains("Sky"))
{
IDictionary<IntPtr, string> windows = List_Windows_By_PID(process.Id);
foreach (KeyValuePair<IntPtr, string> pair in windows)
{
ShowWindowAsync(pair.Key, SW_SHOWMINIMIZED);
}
}
}
}
public static void NormalizeTSX()
{
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
if (process.ProcessName.Contains("Sky"))
{
IDictionary<IntPtr, string> windows = List_Windows_By_PID(process.Id);
foreach (KeyValuePair<IntPtr, string> pair in windows)
{
ShowWindowAsync(pair.Key, SW_SHOWNORMAL);
}
}
}
}
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private struct WINDOWPLACEMENT
{
#pragma warning disable CS0649 // Field 'TSX_Process.WINDOWPLACEMENT.length' is never assigned to, and will always have its default value 0
public int length;
#pragma warning restore CS0649 // Field 'TSX_Process.WINDOWPLACEMENT.length' is never assigned to, and will always have its default value 0
#pragma warning disable CS0649 // Field 'TSX_Process.WINDOWPLACEMENT.flags' is never assigned to, and will always have its default value 0
public int flags;
#pragma warning restore CS0649 // Field 'TSX_Process.WINDOWPLACEMENT.flags' is never assigned to, and will always have its default value 0
#pragma warning disable CS0649 // Field 'TSX_Process.WINDOWPLACEMENT.showCmd' is never assigned to, and will always have its default value 0
public int showCmd;
#pragma warning restore CS0649 // Field 'TSX_Process.WINDOWPLACEMENT.showCmd' is never assigned to, and will always have its default value 0
#pragma warning disable CS0649 // Field 'TSX_Process.WINDOWPLACEMENT.ptMinPosition' is never assigned to, and will always have its default value
public System.Drawing.Point ptMinPosition;
#pragma warning restore CS0649 // Field 'TSX_Process.WINDOWPLACEMENT.ptMinPosition' is never assigned to, and will always have its default value
#pragma warning disable CS0649 // Field 'TSX_Process.WINDOWPLACEMENT.ptMaxPosition' is never assigned to, and will always have its default value
public System.Drawing.Point ptMaxPosition;
#pragma warning restore CS0649 // Field 'TSX_Process.WINDOWPLACEMENT.ptMaxPosition' is never assigned to, and will always have its default value
#pragma warning disable CS0649 // Field 'TSX_Process.WINDOWPLACEMENT.rcNormalPosition' is never assigned to, and will always have its default value
public System.Drawing.Rectangle rcNormalPosition;
#pragma warning restore CS0649 // Field 'TSX_Process.WINDOWPLACEMENT.rcNormalPosition' is never assigned to, and will always have its default value
}
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
private delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("USER32.DLL")]
private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
[DllImport("USER32.DLL")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("USER32.DLL")]
private static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("USER32.DLL")]
private static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("USER32.DLL")]
private static extern IntPtr GetShellWindow();
[DllImport("USER32.DLL")]
private static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
public static IDictionary<IntPtr, string> List_Windows_By_PID(int processID)
{
IntPtr hShellWindow = GetShellWindow();
Dictionary<IntPtr, string> dictWindows = new Dictionary<IntPtr, string>();
EnumWindows(delegate (IntPtr hWnd, int lParam)
{
//ignore the shell window
if (hWnd == hShellWindow)
{
return true;
}
//ignore non-visible windows
if (!IsWindowVisible(hWnd))
{
return true;
}
//ignore windows with no text
int length = GetWindowTextLength(hWnd);
if (length == 0)
{
return true;
}
uint windowPid;
GetWindowThreadProcessId(hWnd, out windowPid);
//ignore windows from a different process
if (windowPid != processID)
{
return true;
}
StringBuilder stringBuilder = new StringBuilder(length);
GetWindowText(hWnd, stringBuilder, length + 1);
dictWindows.Add(hWnd, stringBuilder.ToString());
return true;
}, 0);
return dictWindows;
}
}
}