ウィンドウを透明化したいが、それだけのためにフリーソフトは入れたくない。
Powershellで”メモ帳”を含むウィンドウタイトルを対象に透明化する。
コード
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
using System.Text;
public class User32 {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
[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);
public const int GWL_EXSTYLE = -20;
public const int WS_EX_LAYERED = 0x80000;
public const int LWA_ALPHA = 0x2;
}
"@ -Language CSharp
$windowsWithMemoPad = New-Object System.Collections.ArrayList
$enumWindowsCallback = [User32+EnumWindowsProc] {
param ([System.IntPtr]$hWnd, [System.IntPtr]$lParam)
$title = New-Object System.Text.StringBuilder 1024
[User32]::GetWindowText($hWnd, $title, $title.Capacity)
if ($title.ToString().Contains("メモ帳")) {
$null = $windowsWithMemoPad.Add($hWnd)
}
return $true
}
[User32]::EnumWindows($enumWindowsCallback, [System.IntPtr]::Zero)
foreach ($hwnd in $windowsWithMemoPad) {
$style = [User32]::GetWindowLong($hwnd, [User32]::GWL_EXSTYLE)
[User32]::SetWindowLong($hwnd, [User32]::GWL_EXSTYLE, ($style -bor [User32]::WS_EX_LAYERED))
[User32]::SetLayeredWindowAttributes($hwnd, 0, 150, [User32]::LWA_ALPHA) #透明度を指定する(0-255)
}
if ($windowsWithMemoPad.Count -eq 0) {
Write-Host "タイトルにメモ帳を含むウィンドウが見つかりませんでした。"
}
透明度やウィンドウ名を調整すればカスタム可能。