「Xamarin.Macでシステムステータスバーにアイコンを表示する」の版間の差分

提供: MonoBook
ナビゲーションに移動 検索に移動
imported>Mono Book
(ページの作成:「Mac OS Xのシステムステータスバー(Windowsでいうタスクトレイ)にアイコンを表示したい。 ファイル:システムステータ...」)
 
imported>Mono Book
23行目: 23行目:
 
             statusItem.Image = new NSImage(Path.Combine(NSBundle.MainBundle.BundlePath, "Contents", "Resources", "CDx18.png"));
 
             statusItem.Image = new NSImage(Path.Combine(NSBundle.MainBundle.BundlePath, "Contents", "Resources", "CDx18.png"));
  
             // アイコンをダブルクリックされたらこのアプリを終了する
+
             // アイコンがダブルクリックされたらこのアプリを終了する
 
             statusItem.DoubleClick += (object sender, EventArgs e) => {
 
             statusItem.DoubleClick += (object sender, EventArgs e) => {
 
                 NSApplication.SharedApplication.Terminate(this);
 
                 NSApplication.SharedApplication.Terminate(this);
76行目: 76行目:
 
{{stub}}
 
{{stub}}
  
 +
[[category:MonoMac]]
 
[[category:Xamarin.Mac]]
 
[[category:Xamarin.Mac]]

2015年5月21日 (木) 06:00時点における版

Mac OS Xのシステムステータスバー(Windowsでいうタスクトレイ)にアイコンを表示したい。

Mac OS Xのシステムステータスバー

実装例1

NSStatusBar.SystemStatusBar以下にCreateだのRemoveだのといったインスタンスを生成したり削除したりする静的メソッドがある。 システムステータスバーにアイコンを表示するにはこのメソッド群で作ったインスタンスのImageプロパティにNSImageをぶち込んでやればよい。

    using System;
    using System.IO;
    using Foundation;
    using AppKit;

    public partial class AppDelegate : NSApplicationDelegate
    {
        // 〜〜〜省略〜〜〜

        public override void DidFinishLaunching(NSNotification notification)
        {
            var statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem(NSStatusItemLength.Variable);

            // アイコンを設定する
            statusItem.Image = new NSImage(Path.Combine(NSBundle.MainBundle.BundlePath, "Contents", "Resources", "CDx18.png"));

            // アイコンがダブルクリックされたらこのアプリを終了する
            statusItem.DoubleClick += (object sender, EventArgs e) => {
                NSApplication.SharedApplication.Terminate(this);
            };
        }
    }

クリックされた際にメニューを表示するのも似たような感じで、MenuプロパティにNSMenuを突っ込めばいいだけのようだ。

    using System;
    using System.IO;
    using Foundation;
    using AppKit;

    public partial class AppDelegate : NSApplicationDelegate
    {
        // 〜〜〜省略〜〜〜

        public override void DidFinishLaunching(NSNotification notification)
        {
            // ステータスバーをクリックした時に表示するメニューを用意する
            var appMenu = new NSMenu();
            var quitItem = new NSMenuItem("Quit", "q", 
                (s, e) => NSApplication.SharedApplication.Terminate(this));
            appMenu.AddItem(quitItem);

            // ステータスバーのアイテムを生成する
            var statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem(NSStatusItemLength.Variable);
            statusItem.Menu = appMenu;
            statusItem.Image = new NSImage(Path.Combine(NSBundle.MainBundle.BundlePath, "Contents", "Resources", "CDx18.png"));
            statusItem.Title = "";
            statusItem.HighlightMode = false;
       }
    }

その他

システムステータスバーに表示するアイコンサイズは18pxにするとピッタリらしい。少し余裕をみて16pxでもいいような気がする。まるでX68000やらのスプライトみたいサイズでありドット絵師としての腕前が必要となる高度な分野である。なお縮小していない256x256サイズの画像をImageプロパティにぶち込んでみたが、そのまま表示され自動では縮小されなかった。PhotoshopFireAlpacaあたりのペイントソフトで縮小しておくとよい。

また、どのアプリも軒並み白黒のアイコンを使っているが、システムステータスバーは白黒画像しか扱えないわけではなく、カラー画像でも普通に表示される。しかしひとつだけカラーだと凄い安っぽく見えるので止めた方が無難である。

Yosemiteではdeprecationの警告が出る。代替となる方法は不明である。

関連項目

参考文献