现在的位置: 首页 > 综合 > 正文

flex系统托盘

2013年04月09日 ⁄ 综合 ⁄ 共 4782字 ⁄ 字号 评论关闭

var mainMenu:NativeMenu = new NativeMenu();
var minimizeMenu:NativeMenuItem = new NativeMenuItem("Minimize");
var maximizeMenu:NativeMenuItem = new NativeMenuItem("Maximize");
var sepMenu:NativeMenuItem = new NativeMenuItem("",true);
var restoreMenu:NativeMenuItem = new NativeMenuItem("Restore");
var closeMenu:NativeMenuItem = new NativeMenuItem("Close");
minimizeMenu.addEventListener(Event.Select, handleMenuClick);
maximizeMenu.addEventListener(Event.Select, handleMenuClick);
restoreMenu.addEventListener(Event.Select, handleMenuClick);
closeMenu.addEventListener(Event.Select, handleMenuClick);
mainMenu.addItem(minimizeMenu);
mainMenu.addItem(maximizeMenu);
mainMenu.addItem(sepMenu);
mainMenu.addItem(restoreMenu);
mainMenu.addItem(closeMenu);
this.contextMenu=mainMenu;

系统托盘:

var sysTray:SystemTrayIcon =
   NativeApplication.nativeApplication.icon as SystemTrayIcon;
   sysTray.tooltip = "测试tray";
   //
   sysTray.menu = this.contextMenu;
   sysTray.addEventListener(MouseEvent.CLICK,trayClick);

 

如何将应用程序最小化到托盘,然后给托盘图标加右键菜单以及添加相应的事件,来激活应用程序窗体.

整个流程:

1.       初始化应用时改变系统默认情况下以系统X事件的处理.

2.       定义新处理逻辑(本文是弹出对话框)

3.       生成托盘图标并置于状态栏

4.       给托盘图标加上右键菜单(本文是exit,和open两项)

5.       给菜单加上相应的事件

6.       大功告成

 

主要方法:

 

  1. private var dockImage:BitmapData;   

  2.                

  3.         //初始化Application里调用此方法,完成上面的第一步:   

  4.             public function initApplication():void{   

  5.                 var loader:Loader=new Loader();   

  6.                 loader.contentLoaderInfo.addEventListener(Event.COMPLETE,prepareForSystray);//这里就是完成第一步的任务须,这个prepareForSystray就是对托盘的生在和菜单的控制   

  7.                 loader.load(new URLRequest("assets/images/32.png"));//这里先要加载托盘图标的小图片   

  8.                 this.addEventListener(Event.CLOSING,closingApplication);//设置关闭体的事件   

  9.             }   

  10.                

  11.         //关闭窗体的事件   

  12.             public function closingApplication(event:Event):void{   

  13.                 event.preventDefault();//阻止默认的事件   

  14.                 Alert.yesLabel="Close";   

  15.                 Alert.noLabel="Mini";   

  16.                 Alert.show("Close or Minimize?""Close?"3this, alertCloseHandler);//弹出自定义的选择框, 关于Alert的详细用法,参考官方文档或我前面的相关文章.   

  17.             }   

  18.         //根据用户的选择来判断做什么,这里选择是就是关闭,选择否(Mini)就是最小化到托盘.   

  19.         private function alertCloseHandler(event:CloseEvent):void{   

  20.             if(event.detail==Alert.YES){   

  21.                 closeApp(event);   

  22.             }else{   

  23.                 dock();//最小化到托盘   

  24.             }   

  25.         }   

  26.            

  27.     //生成托盘   

  28.         public function prepareForSystray(event:Event):void{   

  29.             dockImage=event.target.content.bitmapData;   

  30.             if(NativeApplication.supportsSystemTrayIcon){   

  31.                 setSystemTrayProperties();//设置托盘菜单的事件   

  32.                 SystemTrayIcon(NativeApplication.nativeApplication.icon).menu=createSystrayRootMenu();//生成托盘菜单   

  33.             }      

  34.         }   

  35.            

  36.         public function createSystrayRootMenu():NativeMenu{   

  37.             var menu:NativeMenu = new NativeMenu();   

  38.             var openNativeMenuItem:NativeMenuItem = new NativeMenuItem("Open");//生成OPEN菜单项   

  39.             var exitNativeMenuItem:NativeMenuItem = new NativeMenuItem("Exit");//同理   

  40.             openNativeMenuItem.addEventListener(Event.SELECT, undock);   

  41.             exitNativeMenuItem.addEventListener(Event.SELECT, closeApp);//添加EXIT菜单项事件   

  42.             menu.addItem(openNativeMenuItem);   

  43.             menu.addItem(new NativeMenuItem("",true));//separator    

  44.             menu.addItem(exitNativeMenuItem);//将菜单项加入菜单   

  45.   

  46.             return menu;   

  47.   

  48.         }   

  49.         //设置托盘图标的事件   

  50.         private function setSystemTrayProperties():void{   

  51.             SystemTrayIcon(NativeApplication.nativeApplication .icon).tooltip = "TurboSquid squidword";   

  52.             SystemTrayIcon(NativeApplication.nativeApplication .icon).addEventListener(MouseEvent.CLICK, undock);   

  53.             stage.nativeWindow.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING, nwMinimized);    

  54.         }   

  55.            

  56. //最小化窗体   

  57.         private function nwMinimized(displayStateEvent:NativeWindowDisplayStateEvent):void {   

  58.             if(displayStateEvent.afterDisplayState == NativeWindowDisplayState.MINIMIZED) {   

  59.                 displayStateEvent.preventDefault();//阻止系统默认的关闭窗体事件   

  60.                 dock();//将程序放入托盘   

  61.             }   

  62.         }   

  63.   

  64.         //将本地应用程序放到托盘   

  65.         public function dock():void {   

  66.             stage.nativeWindow.visible = false//设置本地程序窗体不可见   

  67.             NativeApplication.nativeApplication.icon.bitmaps = [dockImage];//设置托盘的图标   

  68.         }   

  69.            

  70.         //激活程序窗体   

  71.         public function undock(evt:Event):void {   

  72.         stage.nativeWindow.visible = true;//设置本地程序窗体可见   

  73.         stage.nativeWindow.orderToFront();//设置本地程序窗体到最前端   

  74.         NativeApplication.nativeApplication .icon.bitmaps = [];将托盘图标清空   

  75.         }   

  76.            

  77.         //关闭程序窗体   

  78.         private function closeApp(evt:Event):void {   

  79.             stage.nativeWindow.close();   

  80.         }  

【上篇】
【下篇】

抱歉!评论已关闭.