Flex4 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" creationComplete="windowedapplication1_creationCompleteHandler(event)">
	<s:layout>
		<s:BasicLayout/>
	</s:layout>
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<fx:Script>
		<![CDATA[
			import flashx.textLayout.elements.Configuration;
			
			import mx.controls.Alert;
			import mx.events.CloseEvent;
			import mx.events.FlexEvent;
			
			import spark.components.Window;
			private var icon:BitmapData = null;
			private var w:Window = new Window();
			
			
			protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
			{
				w.title = "通知";
				initApplication();
			}
			
			//====================================系统关闭和托盘小图标处理=================
			
			//系统小图标
			private var dockImage:BitmapData; 
			
			//初始化Application里调用此方法,完成上面的第一步: 
			public function initApplication():void{ 
				var loader:Loader=new Loader(); 
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,prepareForSystray);//这里就是完成第一步的任务项,这个prepareForSystray就是对托盘的生在和菜单的控制 
				//var file:File = new File("com/xlcpIcon.png");
				//loader.loadBytes(file.data);
				loader.load(new URLRequest("com/xlcpIcon.png"));//这里先要加载托盘图标的小图片 
				this.addEventListener(Event.CLOSING,closingApplication);//设置关闭体的事件 
				
			} 
			
			//关闭窗体的事件 
			public function closingApplication(event:Event):void{ 
				event.preventDefault();//阻止默认的事件 
				Alert.yesLabel="退出"; 
				Alert.noLabel="最小化"; 
				Alert.show("退出应用程序或者最小化窗口到系统小图标?", "系统提示", Alert.YES|Alert.NO|Alert.CANCEL, this, alertCloseHandler);//弹出自定义的选择框, 关于Alert的详细用法,参考官方文档或我前面的相关文章.
				Alert.yesLabel="是"; 
				Alert.noLabel="否";   
			} 
			//根据用户的选择来判断做什么,这里选择是就是关闭,选择否(Mini)就是最小化到托盘. 
			private function alertCloseHandler(event:CloseEvent):void{ 
				if(event.detail==Alert.YES){ 
					closeApp(event); 
				}else if(event.detail==Alert.NO){ 
					dock(true);//最小化到托盘 
				}
			} 
			
			//生成托盘 
			public function prepareForSystray(event:Event):void{ 
				dockImage=event.target.content.bitmapData; 
				if(NativeApplication.supportsSystemTrayIcon){ 
					setSystemTrayProperties();//设置托盘菜单的事件 
					SystemTrayIcon(NativeApplication.nativeApplication.icon).menu=createSystrayRootMenu();//生成托盘菜单 
				}
				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 ="xinton"; 
				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(hidden:Boolean = false):void {
				if(hidden){
					stage.nativeWindow.visible = false; //设置本地程序窗体不可见 
				}
				NativeApplication.nativeApplication.icon.bitmaps = [dockImage];//设置托盘的图标 
			} 
			
			//激活程序窗体 
			public function undock(evt:Event):void { 
				createSub();
				//stage.nativeWindow.visible = true;//设置本地程序窗体可见 
				//stage.nativeWindow.orderToFront();//设置本地程序窗体到最前端 
				//NativeApplication.nativeApplication .icon.bitmaps = [];//将托盘图标清空 
				
			} 
			
			//关闭程序窗体 
			private function closeApp(evt:Event):void {
				stage.nativeWindow.close(); 
			}
			
			private function createSub():void{
				var opt:NativeWindowInitOptions = new NativeWindowInitOptions();
				var window:NativeWindow = new NativeWindow(opt);
				trace(Capabilities.screenResolutionX);
				trace(Capabilities.screenResolutionY);
				//window.bounds = new Rectangle(Capabilities.screenResolutionX - this.stage.nativeWindow.bounds.width, 0,  nativeWindow.bounds.width,  nativeWindow.bounds.height);
				window.title = "代码创建空白子窗体";
				window.width = 300;
				window.height = 200;
				window.x = Capabilities.screenResolutionX - window.width;
				window.y = Capabilities.screenResolutionY - window.height;
				
				window.activate();
			}
			

		]]>
	</fx:Script>
	
</s:WindowedApplication>

你可能感兴趣的:(flex4)