LoginSignup
17
9

More than 3 years have passed since last update.

PoweShell で Windows のタスクトレイにネコ走らせてみた🐈

Last updated at Posted at 2020-08-13

Windowsのタスクバーでもネコ走らせてみた🐈という記事を読みました。筆者の方は「しょうもないアプリ」と謙遜されていますが、可愛いネコチャンがタスクトレイに常駐してくれれば、開発で殺伐とした心は劇的に癒されていくことでしょう。CPU 負荷に応じて走る速度が変わるというのも、単なるジョークアプリに終わらせない素晴らしいアイデアです。

しかし私は思いました。

「CPU 負荷に合わせて走る速度の変わるネコをタスクトレイ上に表示するくらいなら、 PowerShell でも書けちゃうのでは?」

PowerShell は Windows にビルトインされた強力なスクリプト言語です。Windows にできることはだいたい記述できますし、新型コロナウイルスの世界の感染状況を把握だってできる PowerShell なら、当然ネコを走らせることもできるはずです。

結果

できました。

メディア1.gif

とりあえず動かしたいときは、こちらからダウンロードした Zip ファイルを解凍し init.vbs を実行してください。

2020/08/21追記

インターネットから取得した PowerShell ファイルはセキュリティ上の都合で実行できないようになっているので、初回実行時に unblock.bat を実行してブロックを解除してください。@okahashi117 さん、ご指摘ありがとうございました!

ソースコード

短いので全文を貼ってしまいます。

runcat.ps1
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# ウィンドウを非表示化
$cscode = @"
    // Win32Api を読み込むための C# コード
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    // ウィンドウの状態を制御するため ShowWindowAsync() を extern する
    public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
"@
$Win32Functions = Add-Type -MemberDefinition $cscode -name Win32ShowWindowAsync -namespace Win32Functions -PassThru
$Win32Functions::ShowWindowAsync((Get-Process -PID $pid).MainWindowHandle, 0) > $null # bool 値を返すので null に捨てる

# タスクトレイにアイコンを作成する
$notifyIcon = New-Object System.Windows.Forms.NotifyIcon
$notifyIcon.Visible = $true

# ダークモード判定
$theme = "light"
if ((Get-ItemProperty -Path "Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize").AppsUseLightTheme -eq 0) {
    $theme = "dark"
}

# リソースの読みこみ
$path = Split-Path -Parent $MyInvocation.MyCommand.Path
$cats = @(
    New-Object System.Drawing.Icon -ArgumentList "$path\\resources\\${theme}_cat0.ico";
    New-Object System.Drawing.Icon -ArgumentList "$path\\resources\\${theme}_cat1.ico";
    New-Object System.Drawing.Icon -ArgumentList "$path\\resources\\${theme}_cat2.ico";
    New-Object System.Drawing.Icon -ArgumentList "$path\\resources\\${theme}_cat3.ico";
    New-Object System.Drawing.Icon -ArgumentList "$path\\resources\\${theme}_cat4.ico"
)

# CPU 負荷の取得が GUI プロセスをブロックしないよう、バックグラウンドジョブとして実行する
$job = Start-Job -ScriptBlock {
    Get-Counter -Counter "\Processor(_Total)\% Processor Time" -Continuous | ForEach-Object {
        $_.CounterSamples.CookedValue
    }
}

# CPU使用率を定期的に取得するため、タイマーオブジェクトを作成する
$cpuTimer = New-Object Windows.Forms.Timer

# タイマーのイベントハンドラからも書き込みたい変数を script スコープで宣言
$script:cpuUsage = 1

$cpuTimer.Add_Tick( {
        $cpuTimer.Stop()

        # バックグラウンドジョブから結果を取得する
        $script:cpuUsage = [double](Receive-Job $job)[0]

        $cpuTimer.Start()
    })

$cpuTimer.Interval = 3 * 1000
$cpuTimer.Start()

# タスクトレイアイコンを任意のタイミングで差し替えるため、タイマーオブジェクトを作成する
$animateTimer = New-Object Windows.Forms.Timer

# タイマーのイベントハンドラからも書き込みたい変数を script スコープで宣言
$script:idx = 0

$animateTimer.Add_Tick( {
        $animateTimer.Stop()

        # 次のコマを表示
        $notifyIcon.Icon = $cats[$script:idx++]
        if ($script:idx -eq 5) { $script:idx = 0 }

        # CPU 使用率をバックグラウンド処理結果から取得
        $notifyIcon.Text = $script:cpuUsage
        # ネコチャンの速さを変更
        $animateTimer.Interval = (200.0 / [System.Math]::Max(1.0, [System.Math]::Min(20.0, $script:cpuUsage / 5)))

        $animateTimer.Start()
    })

$animateTimer.Interval = 200
$animateTimer.Start()

# メッセージループで利用する ApplicationContext を作成する
$applicationContext = New-Object System.Windows.Forms.ApplicationContext

# アイコンクリック時のイベントハンドラ
$notifyIcon.add_Click( {
        # メッセージループを終了
        $applicationContext.ExitThread()
    })

# アイコンを押すまで終わらないよう、メッセージループを回す
[System.Windows.Forms.Application]::Run( $applicationContext )

# 終了処理
$cpuTimer.Stop()
$animateTimer.Stop()
$notifyIcon.Visible = $false

Win32API の読み込みには C# のスニペットを使っていますが、おおよそ PowerShell っぽく書けたのではないでしょうか。PowerShell からタストレイアイコンを触る方法や DOS 窓を表示しない起動についてはこちらの記事の記載が非常に参考になりました。ありがとうございます。

小ネタとして、パフォーマンス情報を取得する Get-Counter のレスポンスが遅かったため、描画をブロックしないようにバックグラウンドジョブで処理させています。Get-Counter には継続して情報を取得し続ける -Continuous オプションがあるため、ジョブは1つだけ立ち上げています。開発中このオプションに気づかず一定間隔でジョブを増やし続けてメモリ消費がとんでもないことになってしまいました。PowerShell のジョブは別プロセスとして立ち上がるので、そのコストが馬鹿にできないようです。

なおコード中にエラーが表示されていますが PowerShell ISE や VS Code の公式拡張機能からは怒られていないので Qiita / Rouge 側の問題と思われます。

ネコチャンとともに楽しい PowerShell ライフを。

参考リンク

17
9
2

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
17
9