-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGUI-Basic.psm1
More file actions
157 lines (133 loc) · 4.87 KB
/
GUI-Basic.psm1
File metadata and controls
157 lines (133 loc) · 4.87 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
function Switch-TaskbarHide {
param (
[ValidateSet("Hide", "Show")]
[string]$Action = "Hide"
)
if (-not ('TaskbarNative' -as [Type])) {
Add-Type @"
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct APPBARDATA {
public int cbSize;
public IntPtr hWnd;
public uint uCallbackMessage;
public uint uEdge;
public RECT rc;
public IntPtr lParam;
}
public static class TaskbarNative {
[DllImport("user32.dll")]
public static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("shell32.dll")]
public static extern uint SHAppBarMessage(uint dwMessage, ref APPBARDATA pData);
public const int SW_HIDE = 0;
public const int SW_SHOW = 5;
public const uint ABM_SETSTATE = 0x0000000A;
public const int ABS_AUTOHIDE = 0x1;
public const int ABS_ALWAYSONTOP = 0x2;
}
"@
}
$taskbarHandle = [TaskbarNative]::FindWindow("Shell_TrayWnd", $null)
if ($taskbarHandle -eq [IntPtr]::Zero) {
throw "Unable to locate the taskbar window handle."
}
$appBarData = [APPBARDATA]::new()
$appBarData.cbSize = [Runtime.InteropServices.Marshal]::SizeOf([APPBARDATA]::new())
$appBarData.hWnd = $taskbarHandle
if ($Action -eq "Hide") {
$state = [TaskbarNative]::ABS_AUTOHIDE -bor [TaskbarNative]::ABS_ALWAYSONTOP
$appBarData.lParam = [IntPtr]$state
[TaskbarNative]::SHAppBarMessage([TaskbarNative]::ABM_SETSTATE, [ref]$appBarData) | Out-Null
Start-Sleep -Seconds 1 # HACK: mandatory...
[TaskbarNative]::ShowWindow($taskbarHandle, [TaskbarNative]::SW_HIDE) | Out-Null
}
else {
$state = [TaskbarNative]::ABS_ALWAYSONTOP
$appBarData.lParam = [IntPtr]$state
[TaskbarNative]::ShowWindow($taskbarHandle, [TaskbarNative]::SW_SHOW) | Out-Null
[TaskbarNative]::SHAppBarMessage([TaskbarNative]::ABM_SETSTATE, [ref]$appBarData) | Out-Null
}
}
function Switch-TaskbarClickThrough {
param(
[switch]$Disable
)
if (-not ('Win32ClickThrough' -as [Type])) {
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class Win32ClickThrough {
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public const int GWL_EXSTYLE = -20;
public const int WS_EX_LAYERED = 0x80000;
public const int WS_EX_TRANSPARENT = 0x20;
public const int WS_EX_TOOLWINDOW = 0x80;
}
"@
}
$hwnd = [Win32ClickThrough]::FindWindow("Shell_TrayWnd", $null)
if ($hwnd -eq [IntPtr]::Zero) {
Write-Error "Taskbar not found."
return
}
$style = [Win32ClickThrough]::GetWindowLong($hwnd, [Win32ClickThrough]::GWL_EXSTYLE)
if ($Disable) {
# Remove transparent: Enable clicking
# But KEEP ToolWindow so it stays hidden from Alt+Tab
$newStyle = ($style -band (-not [Win32ClickThrough]::WS_EX_TRANSPARENT)) -bor [Win32ClickThrough]::WS_EX_TOOLWINDOW
}
else {
# Add transparent: Disable clicking (Clickthrough)
# We ensure Layered is set, as Transparent often requires it.
# Also enforcing ToolWindow style as requested to keep it hidden from Alt+Tab
$newStyle = $style -bor [Win32ClickThrough]::WS_EX_TRANSPARENT -bor [Win32ClickThrough]::WS_EX_LAYERED -bor [Win32ClickThrough]::WS_EX_TOOLWINDOW
}
# Only update if changed
if ($style -ne $newStyle) {
[Win32ClickThrough]::SetWindowLong($hwnd, [Win32ClickThrough]::GWL_EXSTYLE, $newStyle) | Out-Null
}
}
function Find-ExplorerShell {
<#
.SYNOPSIS
Finds the handle of the Windows Taskbar (Shell_TrayWnd).
.DESCRIPTION
Returns the hex handle string (e.g. 0x501CC).
.EXAMPLE
smartcontextmenu --handle (Find-ExplorerShell) --transparency 60 --clickthrough on
#>
if (-not ('Win32ExplorerHelper' -as [Type])) {
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class Win32ExplorerHelper {
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}
"@
}
$hwnd = [Win32ExplorerHelper]::FindWindow("Shell_TrayWnd", $null)
if ($hwnd -ne [IntPtr]::Zero) {
return "0x{0:X}" -f $hwnd.ToInt64()
}
else {
Write-Warning "Shell_TrayWnd not found."
return $null
}
}