-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet_Window.psm1
371 lines (300 loc) · 11.1 KB
/
Set_Window.psm1
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
Function Set_Window ([string]$para1,[string]$para2,[string]$para3,[string]$para4){
Add-Type -AssemblyName System.Windows.Forms
$paracheck1=$PSBoundParameters.ContainsKey('para1')
if($paracheck1 -eq $false -or $para1.Length -eq 0){
$para1="paint"
}
$ProcessName = "*$para1*"
function Set-WindowState {
<#
.LINK
https://gist.github.com/Nora-Ballard/11240204
#>
[CmdletBinding(DefaultParameterSetName = 'InputObject')]
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[Object[]] $InputObject,
[Parameter(Position = 1)]
[ValidateSet('FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE',
'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED',
'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL')]
[string] $State = 'SHOW'
)
Begin {
$WindowStates = @{
'FORCEMINIMIZE' = 11
'HIDE' = 0
'MAXIMIZE' = 3
'MINIMIZE' = 6
'RESTORE' = 9
'SHOW' = 5
'SHOWDEFAULT' = 10
'SHOWMAXIMIZED' = 3
'SHOWMINIMIZED' = 2
'SHOWMINNOACTIVE' = 7
'SHOWNA' = 8
'SHOWNOACTIVATE' = 4
'SHOWNORMAL' = 1
}
$Win32ShowWindowAsync = Add-Type -MemberDefinition @'
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
'@ -Name "Win32ShowWindowAsync" -Namespace Win32Functions -PassThru
if (!$global:MainWindowHandles) {
$global:MainWindowHandles = @{ }
}
}
Process {
foreach ($process in $InputObject) {
if ($process.MainWindowHandle -eq 0) {
if ($global:MainWindowHandles.ContainsKey($process.Id)) {
$handle = $global:MainWindowHandles[$process.Id]
} else {
Write-Error "Main Window handle is '0'"
continue
}
} else {
$handle = $process.MainWindowHandle
$global:MainWindowHandles[$process.Id] = $handle
}
$Win32ShowWindowAsync::ShowWindowAsync($handle, $WindowStates[$State]) | Out-Null
Write-Verbose ("Set Window State '{1} on '{0}'" -f $MainWindowHandle, $State)
}
}
}
#---get window current size
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class SetWindowHelper {
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, out myRECT myrect);
}
public struct myRECT {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
"@
$windowHandle = (Get-Process -Name $ProcessName).MainWindowHandle
$myrect = New-Object myRECT
[SetWindowHelper]::GetWindowRect($windowHandle, [ref]$myrect)
$appwindowWidth = $myrect.Right - $myrect.Left
$appwindowHeight = $myrect.Bottom - $myrect.Top
#Write-Output "Window Width: $windowWidth"
#Write-Output "Window Height: $windowHeight"
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class PInvoke {
[DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("gdi32.dll")] public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
}
"@
$hdc = [PInvoke]::GetDC([IntPtr]::Zero)
$curwidth = [PInvoke]::GetDeviceCaps($hdc, 118) # width
$curheight = [PInvoke]::GetDeviceCaps($hdc, 117) # height
$screen = [System.Windows.Forms.Screen]::PrimaryScreen
$bounds = $screen.Bounds
$currentDPI = (Get-ItemProperty -Path "HKCU:\Control Panel\Desktop\WindowMetrics" -Name AppliedDPI).AppliedDPI
$dpisets=@(96,120,144,168)
$sclsets=@(100,125,150,175)
$index = $dpisets.IndexOf($currentDPI)
$calcu = $sclsets[$index] /100
$bounds.Width = $curwidth * $calcu
$bounds.Height = $curheight * $calcu
$windowWidth=$bounds.Width
$windowheight=$bounds.Height
#-------------------------------------------------------------------------------
#---full screen size
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class PInvoke {
[DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("gdi32.dll")] public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
}
"@
$hdc = [PInvoke]::GetDC([IntPtr]::Zero)
$curwidth = [PInvoke]::GetDeviceCaps($hdc, 118) # width
$curheight = [PInvoke]::GetDeviceCaps($hdc, 117) # height
#-------------------------------------------------------------------------------
#https://stackoverflow.com/questions/59953946/powershell-calculate-pixel-height-of-start-bar
#---taskbar size
function Get-TaskBarDimensions {
param (
[System.Windows.Forms.Screen]$Screen = [System.Windows.Forms.Screen]::PrimaryScreen
)
$device = ($Screen.DeviceName -split '\\')[-1]
if ($Screen.Primary) { $device += ' (Primary Screen)' }
if ($Screen.Bounds.Equals($Screen.WorkingArea)) {
Write-Warning "Taskbar is hidden on device $device or moved to another screen."
return
}
# calculate heights and widths for the possible positions (left, top, right and bottom)
$ScreenRect = $Screen.Bounds
$workingArea = $Screen.WorkingArea
$left = [Math]::Abs([Math]::Abs($ScreenRect.Left) - [Math]::Abs($WorkingArea.Left))
$top = [Math]::Abs([Math]::Abs($ScreenRect.Top) - [Math]::Abs($workingArea.Top))
$right = ($ScreenRect.Width - $left) - $workingArea.Width
$bottom = ($ScreenRect.Height - $top) - $workingArea.Height
if ($bottom -gt 0) {
# TaskBar is docked to the bottom
return [PsCustomObject]@{
X = $workingArea.Left
Y = $workingArea.Bottom
Width = $workingArea.Width
Height = $bottom
Position = 'Bottom'
Device = $device
}
}
if ($left -gt 0) {
# TaskBar is docked to the left
return [PsCustomObject]@{
X = $ScreenRect.Left
Y = $ScreenRect.Top
Width = $left
Height = $ScreenRect.Height
Position = 'Left'
Device = $device
}
}
if ($top -gt 0) {
# TaskBar is docked to the top
return [PsCustomObject]@{
X = $workingArea.Left
Y = $ScreenRect.Top
Width = $workingArea.Width
Height = $top
Position = 'Top'
Device = $device
}
}
if ($right -gt 0) {
# TaskBar is docked to the right
return [PsCustomObject]@{
X = $workingArea.Right
Y = $ScreenRect.Top
Width = $right
Height = $ScreenRect.Height
Position = 'Right'
Device = $device
}
}
}
$tbsize = Get-TaskBarDimensions
$tbsize
#-------------------------------------------------------------------------------
#$ProcessName = $para1
#$point = $para2
#$X = $point.Split(",")[0]
#$Y = $point.Split(",")[1]
#$Width = $para3
#$Height = $para4
#$Passthru = $para5
#$ProcessName = "*paint*"
#$Width = $curwidth
#$Height = $curheight - $tbsize.Height
#$X = 0
#$Y = 0
$paracheck2=$PSBoundParameters.ContainsKey('para2')
$paracheck3=$PSBoundParameters.ContainsKey('para3')
$paracheck4=$PSBoundParameters.ContainsKey('para4')
if($paracheck2 -eq $false -or $para2.Length -eq 0){
$para2 = $appwindowWidth.ToString() + "," + $appwindowHeight.ToString()
}
if($para2 -match "max"){
$para2 = $windowWidth.ToString() + "," + $windowHeight.ToString()
}
if($paracheck3 -eq $false -or $para3.Length -eq 0 -or $para2 -match "max"){
#$para3= $curheight - $tbsize.Height
$para3 = "0,0"
}
if($paracheck4 -eq $false -or $para4.length -eq 0){
$para4=""
}
$nonlog_flag=$para4
$Width = ($para2 -split(","))[0]
$Height = ($para2 -split(","))[1]
$X = ($para3 -split(","))[0]
$Y = ($para3 -split(","))[1]
if($PSScriptRoot.length -eq 0){
$scriptRoot="C:\testing_AI\modules"
}else{
$scriptRoot=$PSScriptRoot
}
Try {
[void][Window2]
} Catch {
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class Window2 {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT2 lpRect);
[DllImport("User32.dll")]
public extern static bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
}
public struct RECT2
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
"@
}
$Rectangle2 = New-Object RECT2
$Handles = (Get-Process -Name $ProcessName).MainWindowHandle ### 1.1//JosefZ
foreach ($Handle in $Handles) { ### 1.1//JosefZ
if ($Handle -eq [System.IntPtr]::Zero) { Continue } ### 1.1//JosefZ
$Return = [Window2]::GetWindowRect($Handle, [ref]$Rectangle2)
if ($Return) {
$Return = [Window2]::MoveWindow($Handle, $X, $Y, $Width, $Height, $True)
}
if ($Passthru) {
$Rectangle2 = New-Object RECT2
$Return = [Window2]::GetWindowRect($Handle, [ref]$Rectangle2)
if ($Return) {
$Height = $Rectangle2.Bottom - $Rectangle2.Top
$Width = $Rectangle2.Right - $Rectangle2.Left
$Size = New-Object System.Management.Automation.Host.Size -ArgumentList $Width, $Height
$TopLeft = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle2.Left, $Rectangle2.Top
$BottomRight = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle2.Right, $Rectangle2.Bottom
if ($Rectangle2.Top -lt 0 -and $Rectangle2.Left -lt 0) {
Write-Warning "Window is minimized! Coordinates will not be accurate."
}
$Object = [pscustomobject]@{
ProcessName = $ProcessName
Size = $Size
TopLeft = $TopLeft
BottomRight = $BottomRight
}
$Object.PSTypeNames.insert(0, 'System.Automation.WindowInfo')
$Object
}
}
}
### write to log ###
$action="Set_Window"
if((Get-Process -Name $ProcessName)){
$results="OK"
}else{
$results="NG"
}
$index="pointX :$X & ponitY :$Y & Width :$Width & Height :$Height"
$tcpath=(Split-Path -Parent $scriptRoot)+"\currentjob\TC.txt"
$tcnumber=((get-content $tcpath).split(","))[0]
$tcstep=((get-content $tcpath).split(","))[1]
if($nonlog_flag.Length -eq 0){
Get-Module -name "outlog"|remove-module
$mdpath=(Get-ChildItem -path "C:\testing_AI\modules\" -r -file |Where-Object{$_.name -match "outlog" -and $_.name -match "psm1"}).fullname
Import-Module $mdpath -WarningAction SilentlyContinue -Global
#write-host "Do $action!"
outlog $action $results $tcnumber $tcstep $index
}
}
Export-ModuleMember -Function Set_Window