1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Activeで指定したウィンドウを半透明に

Last updated at Posted at 2024-10-24
# 必要な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."
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?