AIR-最小化到系统托盘

package   
{
	
	import flash.desktop.*;
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	
	/**
	 * 最小化后生成系统托盘处理类
	 */
	public class SystemTray extends Sprite
	{
		private var dockImage:BitmapData;//托盘图标
		
		public function SystemTray()
		{
			
		}
		
		public function createSystemTray():void
		{
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, prepareForSystray);
			loader.load(new URLRequest("../assets/icon32.png")); //加载托盘图标的小图片
			stage.nativeWindow.addEventListener(Event.CLOSING, closingApplication); //监听关闭窗体事件
			stage.nativeWindow.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING, nwMinimized);//监听最小化窗体事件
			
		}
		
		
		//关闭窗体的事件 
		private function closingApplication(event:Event):void
		{
		        closeApp(event);
		}
		

		//生成托盘 
		private function prepareForSystray(event:Event):void
		{
			dockImage=event.target.content.bitmapData;
			if (NativeApplication.supportsSystemTrayIcon)
			{
				setSystemTrayProperties(); //设置托盘菜单的事件 
				SystemTrayIcon(NativeApplication.nativeApplication.icon).menu=createSystrayRootMenu(); //生成托盘菜单   
			}
		}
		
		//生成托盘菜单项 
		private function createSystrayRootMenu():NativeMenu
		{
			var menu:NativeMenu=new NativeMenu();//菜单
			var openNativeMenuItem:NativeMenuItem=new NativeMenuItem("打开"); //菜单子项
			var exitNativeMenuItem:NativeMenuItem=new NativeMenuItem("退出");  
			openNativeMenuItem.addEventListener(Event.SELECT, undock);
			exitNativeMenuItem.addEventListener(Event.SELECT, closeApp); 
			menu.addItem(openNativeMenuItem);//将菜单项加入菜单
			menu.addItem(new NativeMenuItem("", true)); //菜单子项分隔符
			menu.addItem(exitNativeMenuItem); //将菜单项加入菜单 
			return menu;
		}
		
		//设置托盘图标的事件   
		private function setSystemTrayProperties():void
		{
			SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip="教师助手";
			SystemTrayIcon(NativeApplication.nativeApplication.icon).addEventListener(MouseEvent.CLICK, undock);
			
			
		}
		//最小化窗体   
		public function nwMinimized(event:NativeWindowDisplayStateEvent):void
		{
			
			if(event.afterDisplayState ==  "minimized")
				dock(); 
		}
		//将本地应用程序放到托盘   
		private function dock():void
		{
			stage.nativeWindow.visible=false; //设置本地程序窗体不可见   
			NativeApplication.nativeApplication.icon.bitmaps=[dockImage]; //设置托盘的图标   
		}
		//激活程序窗体   
		private function undock(evt:Event):void
		{
			stage.nativeWindow.restore();
			stage.nativeWindow.visible=true; //设置本地程序窗体可见   
			stage.nativeWindow.orderToFront(); //设置本地程序窗体到最前端   
			NativeApplication.nativeApplication.icon.bitmaps=[]; //将托盘图标清空  
			
		}
		//关闭程序窗体   
		private function closeApp(evt:Event):void
		{
			stage.nativeWindow.close();
		}
		
	}
}


你可能感兴趣的:(ActionScript3)