# 必要なuser32.dllのメソッドを定義
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class User32 {
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
}
"@ -ErrorAction Stop
# 定数の設定
$GWL_EXSTYLE = -20
$WS_EX_LAYERED = 0x80000
$LWA_ALPHA = 0x2
# アクティブなウィンドウのハンドルを取得
$activeWindowHandle = [User32]::GetForegroundWindow()
# 現在のウィンドウの拡張スタイルを取得
$currentStyle = [User32]::GetWindowLong($activeWindowHandle, $GWL_EXSTYLE)
# レイヤードスタイルを設定
[User32]::SetWindowLong($activeWindowHandle, $GWL_EXSTYLE, $currentStyle -bor $WS_EX_LAYERED)
# 透明度を設定 (0: 完全に透明, 255: 不透明)
# 例えば、128を設定すると半透明になります。
$opacity = 128
[User32]::SetLayeredWindowAttributes($activeWindowHandle, 0, [byte]$opacity, $LWA_ALPHA)
Write-Output "The transparency of the active window has been set to $opacity."
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme