天天看点

Air实现系统托盘功能

最近做的一个应用类似于QQ,如果用户已经登录,关闭的时候自动最小化到系统托盘位置 主要实现如下:

1,添加窗体的closing事件,在事件处理中阻止系统默认的关闭事件

[Embed(source=

"resources/images/logo/tray.png"

)]

public

static

const

tray:Class;

private

var trayImage:BitmapData;

private

function initTray():

void

{

this

.addEventListener(Event.CLOSING, handleClosingApplication);

if

(NativeApplication.supportsSystemTrayIcon) {

trayImage=Bitmap(

new

tray()).bitmapData;

SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip=

this

.title;

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

SystemTrayIcon(NativeApplication.nativeApplication.icon).menu=createSystrayRootMenu();

}

}

private

function handleClosingApplication(event:Event):

void

{

if

(stage.nativeWindow.visible) {

//判断是否需要退出

if

(UserGenerator.user) {

stage.nativeWindow.visible=

false

;

//初始化系统托盘图标

NativeApplication.nativeApplication.icon.bitmaps=[trayImage];

if

(event) {

event.preventDefault();

}

}

else

{

exit();

}

}

}

2,初始化系统托盘右键菜单

private

function createSystrayRootMenu():NativeMenu {

var menu:NativeMenu=

new

NativeMenu();

var openNativeMenuItem:NativeMenuItem=

new

NativeMenuItem(

"Open"

);

var exitNativeMenuItem:NativeMenuItem=

new

NativeMenuItem(

"Exit"

);

openNativeMenuItem.addEventListener(Event.SELECT, undock);

exitNativeMenuItem.addEventListener(Event.SELECT, closeApp);

menu.addItem(openNativeMenuItem);

menu.addItem(

new

NativeMenuItem(

""

,

true

));

menu.addItem(exitNativeMenuItem);

return

menu;

}

//系统退出

private

function closeApp(evt:Event):

void

{

exit();

}

//还原界面

public

function undock(evt:Event):

void

{

stage.nativeWindow.visible=

true

;

stage.nativeWindow.orderToFront();

NativeApplication.nativeApplication.icon.bitmaps=[];

}

转载:http://www.wiui.net/?cat=6