<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" width="200" height="100" initialize="testAlert()"> <mx:Script> <!--[CDATA[ import com.everythingflex.air.managers.IconManager; [Embed(source="images/no_red.png")] private var Icon16:Class; private var bitmap16:Bitmap = new Icon16(); private var defaultSysDockIconBitmaps:Array = [bitmap16.bitmapData]; private var i:IconManager = new IconManager(defaultSysDockIconBitmaps); private var timer:Timer; private function testAlert():void{ timer = new Timer(3000,1); timer.addEventListener(TimerEvent.TIMER_COMPLETE,timerComplete); timer.start(); } private function timerComplete(event:TimerEvent):void{ /** * 设置提示信息 (Windows) */ i.startAlert('消息提示') } ]]--> </mx:Script> <mx:Button click="testAlert()" label="开始提示" horizontalCenter="0" verticalCenter="0"/> </mx:WindowedApplication> 这个例子的IconManager封装在了一个SWC里,下面是它的Class源代码: /* Copyright (c) 2008 EverythingFlex.com. http://code.google.com/p/everythingflexairlib/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package { import flash.desktop.NativeApplication; import flash.desktop.NotificationType; import flash.desktop.SystemTrayIcon; import flash.display.Bitmap; import flash.display.BitmapData; import flash.events.TimerEvent; import flash.filters.ColorMatrixFilter; import flash.geom.Point; import flash.geom.Rectangle; import flash.utils.Timer; public class IconManagertest { /** * @private * stores the Array of bitmaps tobe used as icons */ private var sysDockIconBitmaps:Array = new Array(); /** * @private * stores the Array of altered bitmaps tobe used as icons */ private var alteredSysDockIconBitmaps:Array = new Array(); /** * @private * stores the alertType, defaults to NotificationType.CRITICAL */ private var alertType:String = NotificationType.CRITICAL; /** * @private * used as an indicator to determine which state of icon to display */ private var _count:int; /** * @private * timer used to alternate between sysDockIcon and alteredSysDockIcon */ private static var ALERT_TIMER:Timer; /** * Constructor. */ public function IconManagertest(sysDockIconBitmaps:Array, alteredSysDockIconBitmaps:Array=null, alertType:String="critical") { this.sysDockIconBitmaps = sysDockIconBitmaps; this.alteredSysDockIconBitmaps = alteredSysDockIconBitmaps; this.alertType = alertType; handleIcons(); } /** * @private * called by constructor to initialize the icons sets */ private function handleIcons():void{ stopAlert(); if(NativeApplication.supportsDockIcon || NativeApplication.supportsSystemTrayIcon){ if(sysDockIconBitmaps.length > 0){ NativeApplication.nativeApplication.icon.bitmaps = sysDockIconBitmaps; if(alteredSysDockIconBitmaps == null){ alteredSysDockIconBitmaps = new Array(); for (var i:int=0; i<sysDockIconBitmaps.length;i++){ alteredSysDockIconBitmaps.push(applyAlertFilter(sysDockIconBitmaps.clone(),i)); } } } } } /** * @private * alters icon to reverse of original using a filter */ private function applyAlertFilter(bitmapData:BitmapData,i:int):BitmapData { var matrix:Array = new Array(); matrix = matrix.concat([-1, 0, 0, 0, 255]); matrix = matrix.concat([0, -1, 0, 0, 255]); matrix = matrix.concat([0, 0, -1, 0, 255]); matrix = matrix.concat([0, 0, 0, 1, 0]); var r:Rectangle; if(i == 0)r= new Rectangle(0,0,16,16); if(i == 1)r= new Rectangle(0,0,32,32); if(i == 2)r= new Rectangle(0,0,48,48); if(i == 3)r= new Rectangle(0,0,128,128); bitmapData.applyFilter(bitmapData,r,new Point(),new ColorMatrixFilter(matrix)); return new Bitmap(bitmapData).bitmapData; } /** * @public * starts an alert and shows toolTip message (Windows Only) */ public function startAlert(message:String="Alert"):void{ IconManager.ALERT_TIMER = new Timer(500,0); IconManager.ALERT_TIMER.addEventListener(TimerEvent.TIMER,changeIcon) IconManager.ALERT_TIMER.start(); if(NativeApplication.supportsSystemTrayIcon){ SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = message; } } /** * @public * stops an alert */ public function stopAlert():void{ if(NativeApplication.supportsSystemTrayIcon){ SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = ""; } try{ IconManager.ALERT_TIMER.stop(); } catch (e:Error){ } } /** * @private * timer handler, alters state of icon */ private function changeIcon(event:TimerEvent):void{ if(_count == 0){ if(sysDockIconBitmaps.length){ NativeApplication.nativeApplication.icon.bitmaps = sysDockIconBitmaps; } _count = 1; } else { if(alteredSysDockIconBitmaps.length){ NativeApplication.nativeApplication.icon.bitmaps = alteredSysDockIconBitmaps; } _count = 0; } } } } 看看这段代码: [Embed(source="assets/Aqua.png")] private var icon1:Class; [Embed(source="assets/alpha.png")] private var icon2:Class; private var _sysTray:SystemTrayIcon; private var _notifyTimer:Timer; //监听图标闪动 private var _state:int=0; //闪动状态 private var _bitmapDataArray:Array = []; //系统任务栏图标数组 /** 设置任务栏监听 **/ private function init(){ _notifyTimer = new Timer(500); _notifyTimer.addEventListener(TimerEvent.TIMER,onNotify); //监听Timer触发有新消息时任务栏闪动事件 /** 设置任务栏图标 **/ initSystenTray(); //初始化任务栏图标 _notifyTimer.start(); } /** * *初始化任务栏图标 * * */ private function initSystenTray():void{ /** 初始化任务栏菜单 **/ var iconMenu:NativeMenu = new NativeMenu(); var showMenuItem:NativeMenuItem = iconMenu.addItem(new NativeMenuItem("显示主窗体")); var settingMenuItem:NativeMenuItem = iconMenu.addItem(new NativeMenuItem("设置...")); iconMenu.addItem(new NativeMenuItem("-", true)); var exitMenuItem:NativeMenuItem = iconMenu.addItem(new NativeMenuItem("退出程序")); exitMenuItem.addEventListener(Event.SELECT, onExitApp); /** 初始化任务栏显示图标 **/ _bitmapDataArray[0] = (new icon1()).bitmapData; _bitmapDataArray[1] = (new icon2()).bitmapData; NativeApplication.nativeApplication.icon.bitmaps = [_bitmapDataArray[0]]; /* *绑定菜单、提示信息 **/ _sysTray = NativeApplication.nativeApplication.icon as SystemTrayIcon; /**.cp 添加任务图标单击事件**/ _sysTray.addEventListener(MouseEvent.CLICK, undock); //任务栏图标点击事件 _sysTray.tooltip = "在线提示"; _sysTray.menu = iconMenu; } /** * *退出系统 * * */ private function onExitApp(evt:Event):void{ NativeApplication.nativeApplication.icon.bitmaps = []; NativeApplication.nativeApplication.exit(); } /** * *每隔500ms触发Timer状态栏闪动提醒事件 * * */ private function onNotify(evt:TimerEvent):void{ if (_state == 0) _state = 1; else _state = 0; NativeApplication.nativeApplication.icon.bitmaps = [_bitmapDataArray[_state]]; } /** * * 系统任务图标点击事件 * * */ /**.cp 有新消息则显示新消息,没有暂不处理**/ public function undock(evt:Event):void { //自己写处理代码 } 来源:http://forum.airia.cn/archiver/FLEX/thread-14645-1.html 用Flex AIR 做一个类似QQ消息提示在系统托盘闪烁图标