把AIR程序最小化到系统托盘.
非常酷!
新建一个AIR工程,拷贝代码进去,运行。
<?xml version="1.0" encoding="utf-8"?> 
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApplication()">
<mx:Script>
< )
  ) 
if (NativeApplication.supportsSystemTrayIcon){
setSystemTrayProperties();
  
//Set some systray menu options, so that the user can right-click and access functionality //without needing to open the application
SystemTrayIcon(NativeApplication.nativeApplication .icon).menu = createSystrayRootMenu();
}
}
  
/**
* Create a menu that can be accessed from the systray
*
* @Author: S.Radovanovic
*/
private function createSystrayRootMenu():NativeMenu{
//Add the menuitems with the corresponding actions
var menu:NativeMenu = new NativeMenu();
var openNativeMenuItem:NativeMenuItem = new NativeMenuItem("Open");
var exitNativeMenuItem:NativeMenuItem = new NativeMenuItem("Exit");
  
//What should happen when the user clicks on something 
 
  
openNativeMenuItem.addEventListener(Event.SELECT, undock);
  
exitNativeMenuItem.addEventListener(Event.SELECT, closeApp);
  
//Add the menuitems to the menu
menu.addItem(openNativeMenuItem);
menu.addItem(new NativeMenuItem("",true));
//separator
menu.addItem(exitNativeMenuItem);
  
return menu;
}
  
/**
* To be able to dock and undock we need to set some eventlisteners
*
* @Author: S.Radovanovic
*/
private function setSystemTrayProperties():void{
//Text to show when hovering of the docked application icon
SystemTrayIcon(NativeApplication.nativeApplication .icon).tooltip = "Systray test application";
  
//We want to be able to open the application after it has been docked
SystemTrayIcon(NativeApplication.nativeApplication .icon).addEventListener(MouseEvent.CLICK, undock);
  
//Listen to the display state changing of the window, so that we can catch the minimize
stage.nativeWindow.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING, nwMinimized); //Catch the minimize event
}
  
/**
* Do the appropriate actions after the windows display state has changed.
* E.g. dock when the user clicks on minize
*
* @Author: S.Radovanovic
*/
private function nwMinimized(displayStateEvent:NativeWindowDisplayStateEvent):void {
  
//Do we have an minimize action? //The afterDisplayState hasn't happened yet, but only describes the state the window will go to, //so we can prevent it!
if(displayStateEvent.afterDisplayState == NativeWindowDisplayState.MINIMIZED) {
//Prevent the windowedapplication minimize action from happening and implement our own minimize //The reason the windowedapplication minimize action is caught, is that if active we're not able to //undock the application back neatly. The application doesn't become visible directly, but only after clicking //on the taskbars application link. (Not sure yet what happens exactly with standard minimize)
displayStateEvent.preventDefault();
  
//Dock (our own minimize)
dock();
}
}
  
/**
* Do our own 'minimize' by docking the application to the systray (showing the application icon in the systray)
*
* @Author: S.Radovanovic
*/
public function dock():void {
//Hide the applcation
stage.nativeWindow.visible = false;
  
//Setting the bitmaps array will show the application icon in the systray
NativeApplication.nativeApplication .icon.bitmaps = [dockImage];
}
  
/**
* Show the application again and remove the application icon from the systray
*
* @Author: S.Radovanovic
*/
public function undock(evt:Event):void {
//After setting the window to visible, make sure that the application is ordered to the front, //else we'll still need to click on the application on the taskbar to make it visible
stage.nativeWindow.visible = true;
stage.nativeWindow.orderToFront();
  
//Clearing the bitmaps array also clears the applcation icon from the systray
NativeApplication.nativeApplication .icon.bitmaps = [];
}
  
  
/**
* Close the application
*
* @Author: S.Radovanovic
*/
private function closeApp(evt:Event):void {
stage.nativeWindow.close();
}
]]>
</mx:Script>
  
</mx:WindowedApplication>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApplication()">
<mx:Script>
< )
  ) if (NativeApplication.supportsSystemTrayIcon){
setSystemTrayProperties();
//Set some systray menu options, so that the user can right-click and access functionality //without needing to open the application
SystemTrayIcon(NativeApplication.nativeApplication .icon).menu = createSystrayRootMenu();
}
}
/**
* Create a menu that can be accessed from the systray
*
* @Author: S.Radovanovic
*/
private function createSystrayRootMenu():NativeMenu{
//Add the menuitems with the corresponding actions
var menu:NativeMenu = new NativeMenu();
var openNativeMenuItem:NativeMenuItem = new NativeMenuItem("Open");
var exitNativeMenuItem:NativeMenuItem = new NativeMenuItem("Exit");
//What should happen when the user clicks on something
 
 openNativeMenuItem.addEventListener(Event.SELECT, undock);
exitNativeMenuItem.addEventListener(Event.SELECT, closeApp);
//Add the menuitems to the menu
menu.addItem(openNativeMenuItem);
menu.addItem(new NativeMenuItem("",true));
//separator
menu.addItem(exitNativeMenuItem);
return menu;
}
/**
* To be able to dock and undock we need to set some eventlisteners
*
* @Author: S.Radovanovic
*/
private function setSystemTrayProperties():void{
//Text to show when hovering of the docked application icon
SystemTrayIcon(NativeApplication.nativeApplication .icon).tooltip = "Systray test application";
//We want to be able to open the application after it has been docked
SystemTrayIcon(NativeApplication.nativeApplication .icon).addEventListener(MouseEvent.CLICK, undock);
//Listen to the display state changing of the window, so that we can catch the minimize
stage.nativeWindow.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING, nwMinimized); //Catch the minimize event
}
/**
* Do the appropriate actions after the windows display state has changed.
* E.g. dock when the user clicks on minize
*
* @Author: S.Radovanovic
*/
private function nwMinimized(displayStateEvent:NativeWindowDisplayStateEvent):void {
//Do we have an minimize action? //The afterDisplayState hasn't happened yet, but only describes the state the window will go to, //so we can prevent it!
if(displayStateEvent.afterDisplayState == NativeWindowDisplayState.MINIMIZED) {
//Prevent the windowedapplication minimize action from happening and implement our own minimize //The reason the windowedapplication minimize action is caught, is that if active we're not able to //undock the application back neatly. The application doesn't become visible directly, but only after clicking //on the taskbars application link. (Not sure yet what happens exactly with standard minimize)
displayStateEvent.preventDefault();
//Dock (our own minimize)
dock();
}
}
/**
* Do our own 'minimize' by docking the application to the systray (showing the application icon in the systray)
*
* @Author: S.Radovanovic
*/
public function dock():void {
//Hide the applcation
stage.nativeWindow.visible = false;
//Setting the bitmaps array will show the application icon in the systray
NativeApplication.nativeApplication .icon.bitmaps = [dockImage];
}
/**
* Show the application again and remove the application icon from the systray
*
* @Author: S.Radovanovic
*/
public function undock(evt:Event):void {
//After setting the window to visible, make sure that the application is ordered to the front, //else we'll still need to click on the application on the taskbar to make it visible
stage.nativeWindow.visible = true;
stage.nativeWindow.orderToFront();
//Clearing the bitmaps array also clears the applcation icon from the systray
NativeApplication.nativeApplication .icon.bitmaps = [];
}
/**
* Close the application
*
* @Author: S.Radovanovic
*/
private function closeApp(evt:Event):void {
stage.nativeWindow.close();
}
]]>
</mx:Script>
</mx:WindowedApplication>
来自:http://www.saskovic.com/blog/?p=5
 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号