AIR程序生成系統托盤 及 在程序最小化的時候在電腦右下角有個子定義的圖標,如果對其加入事件,還可以有打開和退出的效果
在你的應用主程序的init方法中調用createSystemTray就可以了
public function createSystemTray():void
{
var loader:Loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, prepareForSystray); //這裏就是完成第一步的任務須,這個prepareForSystray就是對托盤的生在和菜單的控制
loader.load(new URLRequest("http://gamespics.com/wp-content/uploads/2011/03/cd.jpg")); //這裏先要加載托盤圖標的小圖片 http://gamespics.com/wp-content/uploads/2011/03/cd.jpg
this.addEventListener(Event.CLOSING, closingApplication); //設置關閉體的事件
}
//public function handleMenuClick(event:Event):void
//{
// mx.controls.Alert.show("ok");
//}
//關閉窗體的事件
public function closingApplication(event:Event):void
{
event.preventDefault(); //阻止默認的事件
Alert.yesLabel="關閉";
Alert.noLabel="最小化";
Alert.show("關閉 OR 最小化?", "", 3, this, alertCloseHandler); //彈出自定義的選擇框, 關於Alert的詳細用法,參考官方文檔或我前面的相關文章.
}
//根據用戶的選擇來判斷做什麼,這裏選擇是就是關閉,選擇否(Mini)就是最小化到托盤.
private function alertCloseHandler(event:CloseEvent):void
{
if (event.detail == Alert.YES)
{
closeApp(event);
}
else
{
dock(); //最小化到托盤
}
}
//生成托盤
public function prepareForSystray(event:Event):void
{
dockImage=event.target.content.bitmapData;
if (NativeApplication.supportsSystemTrayIcon)
{
setSystemTrayProperties(); //設置托盤菜單的事件
SystemTrayIcon(NativeApplication.nativeApplication.icon).menu=createSystrayRootMenu(); //生成托盤菜單
}
}
public function createSystrayRootMenu():NativeMenu
{
var menu:NativeMenu=new NativeMenu();
var openNativeMenuItem:NativeMenuItem=new NativeMenuItem("打開"); //生成OPEN菜單項
var exitNativeMenuItem:NativeMenuItem=new NativeMenuItem("退出"); //同理
openNativeMenuItem.addEventListener(Event.SELECT, undock);
exitNativeMenuItem.addEventListener(Event.SELECT, closeApp); //添加EXIT菜單項事件
menu.addItem(openNativeMenuItem);
menu.addItem(new NativeMenuItem("", true)); //separator
menu.addItem(exitNativeMenuItem); //將菜單項加入菜單
return menu;
}
//設置托盤圖標的事件
private function setSystemTrayProperties():void
{
SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip="TurboSquid squidword";
SystemTrayIcon(NativeApplication.nativeApplication.icon).addEventListener(MouseEvent.CLICK, undock);
stage.nativeWindow.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING, nwMinimized);
}
//最小化窗體
private function nwMinimized(displayStateEvent:NativeWindowDisplayStateEvent):void
{
if (displayStateEvent.afterDisplayState == NativeWindowDisplayState.MINIMIZED)
{
displayStateEvent.preventDefault(); //阻止系統默認的關閉窗體事件
dock(); //將程序放入托盤
}
}
//將本地應用程序放到托盤
public function dock():void
{
stage.nativeWindow.visible=false; //設置本地程序窗體不可見
NativeApplication.nativeApplication.icon.bitmaps=[dockImage]; //設置托盤的圖標
}
//激活程序窗體
public function undock(evt:Event):void
{
stage.nativeWindow.visible=true; //設置本地程序窗體可見
stage.nativeWindow.orderToFront(); //設置本地程序窗體到最前端
NativeApplication.nativeApplication.icon.bitmaps=[]; //將托盤圖標清空
}
//關閉程序窗體
private function closeApp(evt:Event):void
{
stage.nativeWindow.close();
}