单个窗体文件托盘处理:简单步骤如下

1,侦听窗体最小化事件。

2,创建托盘图标,创建点击托盘恢复窗体事件,创建托盘右键菜单。

3,响应窗体最小化事件,进行处理。隐藏窗体,给托盘icon 赋值。

4,恢复窗体。恢复窗体初始化大小,并让窗体显示,并将窗体置于最高层。将托盘图标清空。

 

下边贴出code:

文档类:

package com.vini123
{
    import flash.display.Sprite;
    import com.vini123.utils.TrayIcon;

    public class Main extends Sprite
    {
        var _trayIcon:TrayIcon;
        public function Main():void
        {
            _trayIcon = new TrayIcon(this);
        }
    }
}

 

托盘图标类:

package com.vini123.utils
{
    import com.vini123.Main;
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.display.NativeWindow;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.NativeMenu;
    import flash.display.NativeMenuItem;
    
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    
    import flash.desktop.NativeApplication;
    import flash.desktop.SystemTrayIcon;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.NativeWindowDisplayStateEvent;
    import flash.net.URLRequest;
    import flash.net.navigateToURL;


    public class TrayIcon extends Sprite
    {
        private var _stage:Stage;
        private var _window:NativeWindow;
        private var _dockIcon:BitmapData;

        public function TrayIcon(_root:Main):void
        {
            _stage = _root.stage;
            _window = _stage.nativeWindow;
            _window.title = "我用三生烟火,换你一世迷离:莫失莫忘";
            _window.stage.align = StageAlign.TOP_LEFT;
            _window.stage.scaleMode = StageScaleMode.NO_SCALE;
            _window.addEventListener(Event.CLOSE,closHandler);
            _window.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGE,displayStateChangeHandler);
            creatTrayIcon();
        }

        private function closHandler(e:Event):void
        {
            NativeApplication.nativeApplication.exit();
        }

        private function displayStateChangeHandler(e:NativeWindowDisplayStateEvent):void
        {
            var code:String = e.afterDisplayState;
            if (code == "minimized")
            {
                dock();
            }
        }

        private function creatTrayIcon():void
        {
            var myRequest:URLRequest = new URLRequest();
            myRequest.url = "icon/48.png";
            var myLoader:Loader = new Loader();
            myLoader.load(myRequest);
            myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
        }

        private function completeHandler(e:Event):void
        {
            _dockIcon = e.target.content.bitmapData;
            if (NativeApplication.supportsSystemTrayIcon)
            {
                creatSystemTrayProperties();
            }
        }

        private function dock():void
        {
            trace("快到碗里来");
            _window.visible = false;
            NativeApplication.nativeApplication.icon.bitmaps = [_dockIcon];
        }
        
        private function trayRightMenu():NativeMenu
        {
            var _nativeMenu:NativeMenu = new NativeMenu();
            var _resumeNativeMenuItem:NativeMenuItem = new NativeMenuItem("恢复(O)");
            var _exitNativeMenuItem:NativeMenuItem = new NativeMenuItem("退出(X)");
            var _aboutNativeMenuItem:NativeMenuItem = new NativeMenuItem("关于(A)");
            _nativeMenu.addItem(_resumeNativeMenuItem);
            _nativeMenu.addItem(_exitNativeMenuItem);
            _nativeMenu.addItem(_aboutNativeMenuItem);
            _resumeNativeMenuItem.addEventListener(Event.SELECT, undock);
            _exitNativeMenuItem.addEventListener(Event.SELECT ,closHandler);
            _aboutNativeMenuItem.addEventListener(Event.SELECT ,aboutHandler);
            return _nativeMenu;
        }
        
        private function aboutHandler(e:Event):void
        {
            navigateToURL(new URLRequest("http://blog1.vini123.com"));
        }
        
        private function creatSystemTrayProperties():void
        {
            SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = "我的托盘";
            SystemTrayIcon(NativeApplication.nativeApplication.icon).addEventListener(MouseEvent.CLICK,undock);
            SystemTrayIcon(NativeApplication.nativeApplication.icon).menu = trayRightMenu();
        }

        private function undock(e:Event):void
        {
            trace("快出碗里去");
            _window.restore(); //从最小化或最大化状态还原此窗口。
            _window.visible = true; //显示该窗口
            _window.orderToFront(); //将此窗口放在任何其他可见窗口前面
            NativeApplication.nativeApplication.icon.bitmaps = []; //清除托盘图标
        }
    }
}

 

来源:http://blog1.vini123.com/?p=248