blog.Ring.idv.tw

Adobe AIR: 打造自己的SystemTray

Adobe AIR: 打造自己的SystemTray


每次當我們將電腦一開機,進入到桌面系統之後~ 隨之而來的就是載入一連串的常駐程式~ 或是開機自動執行的相關程式~

而這些程式有些會出現System Tray Icon在右下角的工具列中~

下述程式就是要處理這樣的動作滴~ 不過我改了一點小東西~

有裝過ApacheTomcat Server嗎?它們右下角的System Tray Icon會隨著啟動或關閉伺服器而有所變化~ 底下程式可以為你做到~ 玩玩看吧~ ^^

P.S. 請準備16x16、128x128的圖示

SystemTray.as

/**
 * Date: 2008-03-11
 * Shen (http://blog.ring.idv.tw)
 **/
package 
{
	import flash.desktop.*;
	import flash.events.*;
	import flash.display.*;
	import flash.net.*;
	
	public class SystemTray
	{
		private static var _instance:SystemTray;
		public static function getInstance():SystemTray
		{
			if(SystemTray._instance == null)
			{
				SystemTray._instance = new SystemTray(new PrivateClass());
			}
			return SystemTray._instance;
		}
		public function SystemTray(pvt:PrivateClass)
		{
			trace("SystemTray initialized!");
			NativeApplication.nativeApplication.autoExit = false;
			var icon:Loader = new Loader();
			var iconMenu:NativeMenu = new NativeMenu();
			var exitCommand:NativeMenuItem = iconMenu.addItem(new NativeMenuItem("Exit"));
			exitCommand.addEventListener(Event.SELECT, Exit);

			if (NativeApplication.supportsSystemTrayIcon)
			{
				icon.contentLoaderInfo.addEventListener(Event.COMPLETE, iconLoadComplete);
				icon.load(new URLRequest("icons/StopWatchIcon16.png"));

				var systray:SystemTrayIcon = NativeApplication.nativeApplication.icon as SystemTrayIcon;
				systray.tooltip = "CSProxy";
				systray.menu = iconMenu;
			}

			if (NativeApplication.supportsDockIcon)
			{
				icon.contentLoaderInfo.addEventListener(Event.COMPLETE,iconLoadComplete);
				icon.load(new URLRequest("icons/StopWatchIcon128.png"));
				var dock:DockIcon = NativeApplication.nativeApplication.icon as DockIcon;
				dock.menu = iconMenu;
			}
		}
		private function iconLoadComplete(event:Event):void
		{
			NativeApplication.nativeApplication.icon.bitmaps = [event.target.content.bitmapData];
		}
		public function onConnected()
		{
			var icon:Loader = new Loader();
			icon.contentLoaderInfo.addEventListener(Event.COMPLETE,iconLoadComplete);
			
			if (NativeApplication.supportsSystemTrayIcon)
				icon.load(new URLRequest("icons/logo16.jpg"));
				
			if (NativeApplication.supportsDockIcon)
				icon.load(new URLRequest("icons/logo128.jpg"));	
		}
		public function onDisconnected()
		{
			var icon:Loader = new Loader();
			icon.contentLoaderInfo.addEventListener(Event.COMPLETE,iconLoadComplete);
			
			if (NativeApplication.supportsSystemTrayIcon)
				icon.load(new URLRequest("icons/StopWatchIcon16.png"));
				
			if (NativeApplication.supportsDockIcon)
				icon.load(new URLRequest("icons/StopWatchIcon128.png"));	
		}
		private function Exit(event:Event):void
		{
			NativeApplication.nativeApplication.icon.bitmaps = [];
			NativeApplication.nativeApplication.exit();
		}
	}
}
class PrivateClass
{
	public function PrivateClass( )
	{
		trace("PrivateClass called");
	}
}

Flash

var singleton:SystemTray = SystemTray.getInstance();
//改變啟動圖示
singleton.onConnected();
//改變關閉圖示
singleton.onDisconnected();

相關資源

Working with the dock and the system tray

Flash LiveDocs:Taskbar icons

2008-03-11 15:43:36

4 comments on "Adobe AIR: 打造自己的SystemTray"

  1. 1. 呈現 說:

    WOW!這個屌^^b

    2008-03-11 17:10:40

  2. 2. Shen 說:

    哈~ 趕工趕工~ ^^ CSProxy雛型愈來愈完整了~

    2008-03-11 23:07:48

  3. 3. 宗諺 說:

    超酷的耶

    BTW 我打開FLASH CS3 就能弄了嗎?
    還是要載什麼??

    2008-03-12 10:11:29

  4. 4. Shen 說:

    基本上用Flash CS3就可以開發了~
    不過建議你去裝一下 Adobe AIR Update for Flash CS3 Professional~
    不然用AIR SDK會比較繁瑣 ^^

    2008-03-12 16:15:11

Leave a Comment

Copyright (C) Ching-Shen Chen. All rights reserved.

::: 搜尋 :::

::: 分類 :::

::: Ads :::

::: 最新文章 :::

::: 最新回應 :::

::: 訂閱 :::

Atom feed
Atom Comment

::: 人氣指數 :::

今日人氣:590

累積人氣:3162041


::: 線上人數 :::

counter