Windowsで「active」と打つとコマンドプロンプトを常に最前面にする方法
コマンドプロンプト作業中、常にウィンドウを前面に表示しておきたいときに便利なスクリプトです。
この方法では、どこにいても active
と入力するだけで、そのコマンドプロンプトウィンドウが「常時最前面」になります。
スクリプト内容(active.cmd)
以下を active.cmd
という名前で保存してください。
@echo off
powershell -NoProfile -Command "& {
Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
public class WinAPI {
[DllImport(\"user32.dll\")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
public const UInt32 SWP_NOMOVE = 0x0002;
public const UInt32 SWP_NOSIZE = 0x0001;
public const UInt32 SWP_SHOWWINDOW = 0x0040;
}
'@ -Language CSharp
$hwnd = (Get-Process -Id $PID).MainWindowHandle
[WinAPI]::SetWindowPos($hwnd, [WinAPI]::HWND_TOPMOST, 0, 0, 0, 0, [WinAPI]::SWP_NOMOVE -bor [WinAPI]::SWP_NOSIZE -bor [WinAPI]::SWP_SHOWWINDOW)
}"
配置手順
- 任意のディレクトリに保存:
mkdir %USERPROFILE%\bin
move active.cmd %USERPROFILE%\bin
-
%USERPROFILE%\bin
を Path に追加:
- 環境変数の設定 → ユーザー環境変数 →
Path
を編集 -
新規
を押して%USERPROFILE%\bin
を追加
使い方
コマンドプロンプトでどこからでも以下を実行:
active
そのコマンドプロンプトが「常に前面に表示」されるようになります。
備考
- 効果はウィンドウを閉じるまで持続します。
- 無効化したい場合は、AutoHotkey などで解除操作を行うか、スクリプトを改造する必要があります。