Flex Air 应用 增加系统托盘图标

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
					   xmlns:s="library://ns.adobe.com/flex/spark"
					   xmlns:mx="library://ns.adobe.com/flex/mx"
					   backgroundAlpha="0.5" backgroundColor="#727070" 
					   width="245" height="505" creationComplete="initSystary()"
					   showStatusBar="false" usePreloader="false"
					   mouseDown="this.nativeWindow.startMove()">
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<fx:Style>
		@namespace s "library://ns.adobe.com/flex/spark";
		@namespace mx "library://ns.adobe.com/flex/mx";
		mx|Alert {
			titleStyleName: "alertTitle";
			messageStyleName: "alertMessage";
			button-style-name:"alertButtons";
			borderColor: #831201;
			borderAlpha: 0.5;
			backgroundColor: #cccccc;
			backgroundAlpha: 0.5;
			cornerRadius:5; 
			headerHeight:0; 
		}
		
		.alertTitle {
			letterSpacing: 0;
			fontSize: 14;
			color: #000000;
			fontFamily: "_sans";
		}
		
		.alertMessage {
			letterSpacing: 0;
			fontSize: 12;
			fontWeight: bold;
			color: #000000;
			fontFamily: "_sans";
		}
		.alertButtons{ 
			cornerRadius:5;
			fillAlphas:0.0,0.0,0.0,0.0; 
			fillColors:#aaaaaa, #aaaaaa, #aaaaaa, #aaaaaa; 
			color:#000000; 
			textRollOverColor:#FFFFFF; 
			borderColor:#862e1d; 
			themeColor:#862e1d; 
			fontFamily:Arial; 
			fontSize:12; fontWeight:bold; 
		}
		mx|ToolTip{
			backgroundAlpha: 0.5;
			cornerRadius: 6;
			backgroundColor: #999999;
			color: #0b333c;
			fontSize: 12;
			fontWeight: bold;
		}
	</fx:Style>
	<fx:Script>
		<![CDATA[
			import flash.events.Event;
			
			import mx.controls.Alert;
			import mx.events.CloseEvent;
			
			private var dockImage:BitmapData;   
			//初始化
			public function initSystary():void{
				var loader:Loader=new Loader();   
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,prepareForSystray);//这里就是完成第一步的任务须,这个prepareForSystray就是对托盘的生在和菜单的控制   
				loader.load(new URLRequest("assets/icon_16.png"));//这里先要加载托盘图标的小图片
			}
			
			//生成托盘   
			public function prepareForSystray(event:Event):void{   
				dockImage=event.target.content.bitmapData;   
				if(NativeApplication.supportsSystemTrayIcon){   
					setSystemTrayProperties();//设置托盘菜单的事件   
					SystemTrayIcon(NativeApplication.nativeApplication.icon).menu=createSystrayRootMenu();//生成托盘菜单   
				}      
			}   
			
			//关闭窗体的事件   
			public function closingApplication(event:Event):void{   
				event.preventDefault();//阻止默认的事件   
				Alert.yesLabel="关闭";   
				Alert.noLabel="最小化";   
				Alert.show("关闭 或者 最小化?", "", 3, this, alertCloseHandler);//弹出自定义的选择框, 关于Alert的详细用法,参考官方文档或我前面的相关文章.   
			}   
			//根据用户的选择来判断做什么,这里选择是就是关闭,选择否(Mini)就是最小化到托盘.  
			private function alertCloseHandler(event:CloseEvent):void{   
				if(event.detail==Alert.YES){   
					closeApp(event);   
				}else{   
					dock();//最小化到托盘   
				}   
			}  
			
			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 = "迷你虾米";   
				SystemTrayIcon(NativeApplication.nativeApplication .icon).addEventListener(MouseEvent.CLICK, undock);   
				this.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 {   
				this.stage.nativeWindow.visible = false; //设置本地程序窗体不可见   
				NativeApplication.nativeApplication.icon.bitmaps = [dockImage];//设置托盘的图标   
			}   
			
			//激活程序窗体   
			public function undock(evt:Event):void {   
				this.stage.nativeWindow.visible = true;//设置本地程序窗体可见   
				this.stage.nativeWindow.orderToFront();//设置本地程序窗体到最前端   
				NativeApplication.nativeApplication .icon.bitmaps = [];//将托盘图标清空   
			}   
			
			//关闭程序窗体   
			private function closeApp(evt:Event):void {   
				this.stage.nativeWindow.close();   
			}  
		]]>
	</fx:Script>
	<mx:HTML left="5" right="5" top="20" bottom="5" borderVisible="false" dropShadowVisible="true">
		<mx:htmlText><![CDATA[<embed src="http://www.xiami.com/widget/933399_3647936,92815,1769398935,1770041365,358220,2859781,1017664,3598821,2117684,1770465833,1769836929,1768925137,1769893096,1769880107,1769880770,1769872306,1769529110,1769865841,1769731118,1769863941,1769571068,1769833050,1769868710,1769832126,3430594,1769701067,1769802256,1769832129,1768983626,1769470092,1769642275,1769399774,3492156,1769845026,1769908306,1769227477,1769372162,1769292118,7336,3480144,1769899857,384840,1769000347,1769183201,3455559,3567313,3484274,1769472424,72256,1769472432,_235_480_5695c1_457cb4/multiPlayer.swf" type="application/x-shockwave-flash" width="235" height="480" wmode="opaque"></embed>]]></mx:htmlText>
	</mx:HTML>
	<mx:LinkButton right="0" top="0" width="25" height="20" label="X" cornerRadius="4"
				   fontWeight="bold" toolTip="关闭" click="closingApplication(event)"/>
	<mx:LinkButton right="25" top="0" width="25" height="20" label="-" fontSize="16"
				   fontWeight="bold" toolTip="最小化" click="dock()"/>
	
</s:WindowedApplication>
 

猜你喜欢

转载自ljxyuer.iteye.com/blog/1171054