Adobe AIR right-click menu and system tray (tray) Functions and implementers

Source: Internet
Author: User

<? XML version = "1.0" encoding = "UTF-8"?>
<S: Drawing wedapplication xmlns: FX = "http://ns.adobe.com/mxml/2009"
Xmlns: S = "Library: // ns.adobe.com/flex/spark"
Xmlns: MX = "Library: // ns.adobe.com/flex/mx" currentstate = "loginstate" initialize = "initapplication ()" xmlns: COM = "com. *">
<FX: declarations>
<! -- Place non-visual elements (such as services and value objects) Here -->
</FX: declarations>
<S: States>
<S: State name = "loginstate"/>
<S: State name = "mainstate"/>
</S: States>

<FX: SCRIPT>
<! [CDATA [
IMPOrt mx. Controls. Alert;
IMPOrt mx. Events. closeevent;
IMPOrt mx. Events. flexevent;

IMPORT spark. components. supportclasses. range;

[Bindable]
Private var AAA: number;

Private function handlemenuclick (Event: Event): void
{
VaR select: String = event.tar get. label;
If (select = "minimal ")
{
This. Minimize ();
}
If (select = "maximize ")
{
This. Maximize ();
}
If (select = "Restore ")
{
This. Restore ();
}
If (select = "close ")
{
This. Close ();
}
}

//////////////////////////////////////// //////
Private var dockimage: bitmapdata;



// Call this method in the initialization application to complete the first step above:

Public Function initapplication (): void {

VaR Loader: loader = new loader ();

Loader. contentloaderinfo. addeventlistener (event. Complete, prepareforsponray); // This is the first step to complete the task. This prepareforsponray is the control of the birth and menu of the tray.

Loader. Load (New URLRequest ("assets/vm.png"); // you must first load the small image of the tray icon

This. addeventlistener (event. Closing, closingapplication); // you can specify an event to close the object.


This. Maximize ();
//////
VaR mainmenu: nativemenu = new nativemenu ();
VaR minimizemenu: nativemenuitem = new nativemenuitem ("minimized ");
VaR maximizemenu: nativemenuitem = new nativemenuitem ("maximize ");
VaR sepmenu: nativemenuitem = new nativemenuitem ("", true );
VaR restoremenu: nativemenuitem = new nativemenuitem ("Restore ");
VaR closemenu: nativemenuitem = new nativemenuitem ("disabled ");
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;

}



// Close the form event

Public Function closingapplication (Event: Event): void {

Event. preventdefault (); // block default events

Alert. yeslabel = "Disable ";

Alert. nolabel = "minimal ";

Alert. Show ("close or minimize? "," Close? ", 3, this, alertclosehandler); // The Custom selection box is displayed. For detailed usage of alert, refer to the official documentation or my previous articles.

}

// Determine what to do based on the user's choice. Here, the choice is disabled, and the choice of "no" (mini) is minimized to the tray.

Private function alertclosehandler (Event: closeevent): void {

If (event. Detail = alert. Yes ){

Closeapp (event );

} Else {

Dock (); // minimize to tray

}

}



// Generate a tray

Public Function prepareforpolicray (Event: Event): void {

Dockimageappsevent.tar get. content. bitmapdata;

If (nativeapplication. supportssystemtrayicon ){

Setsystemtrayproperties (); // set the tray menu event

Systemtrayicon (nativeapplication. nativeapplication. Icon). Menu = createsystrayrootmenu (); // generate the tray menu

}

}



Public Function createsystrayrootmenu (): nativemenu {

VaR menu: nativemenu = new nativemenu ();

VaR opennativemenuitem: nativemenuitem = new nativemenuitem ("open"); // generate an Open menu item

VaR exitnativemenuitem: nativemenuitem = new nativemenuitem ("quit"); // likewise

Opennativemenuitem. addeventlistener (event. Select, undock );

Exitnativemenuitem. addeventlistener (event. Select, closeapp); // Add an exit menu item event

Menu. additem (opennativemenuitem );

Menu. additem (New nativemenuitem ("", true); // Separator

Menu. additem (exitnativemenuitem); // Add the menu item to the menu



Return menu;



}

// Set the tray icon event

Private function setsystemtrayproperties (): void {

Systemtrayicon (nativeapplication. nativeapplication. Icon). tooltip = "turbosquid squidword ";

Systemtrayicon (nativeapplication. nativeapplication. Icon). addeventlistener (mouseevent. Click, undock );

Stage. nativewindow. addeventlistener (nativewindowdisplaystateevent. display_state_changing, nwminimized );

}



// Minimize the form

Private function nwminimized (displaystateevent: nativewindowdisplaystateevent): void {

If (displaystateevent. afterdisplaystate = nativewindowdisplaystate. Minimized ){

Displaystateevent. preventdefault (); // prevents the system from closing the form event by default.

Dock (); // put the program into the tray

}

}



// Place the local application on the tray

Public Function dock (): void {

Stage. nativewindow. Visible = false; // sets the local program form to be invisible.

Nativeapplication. nativeapplication. Icon. bitmaps = [dockimage]; // set the tray icon

}



// Activate the program form

Public Function undock (EVT: Event): void {

Stage. nativewindow. Visible = true; // sets the local program form to be visible.

Stage. nativewindow. ordertofront (); // set the local program form to the frontend

Nativeapplication. nativeapplication. Icon. bitmaps = []; // clear the tray icon

}



// Close the program form

Private function closeapp (EVT: Event): void {

Stage. nativewindow. Close ();

}


//////////////////////////////////////// /////////////////////////////////
]>
</FX: SCRIPT>
<Mx: tabnavigator implements dein = "mainstate" id = "appmodules" doubleclickenabled = "true" Top = "0" Bottom = "0" Left = "0" Right = "0" cornerradius = "2 "taboffset =" 5 "dropshadowvisible =" false ">
</MX: tabnavigator>
<Mx: form x = "132" Y = "151" id = "login" verticalcenter = "0" horizontalcenter = "0" includein = "loginstate">
<Mx: formheading label = "login"/>
<Mx: formitem label = "name">
<S: textinput id = "username"/>
</MX: formitem>
<Mx: formitem label = "password">
<S: textinput id = "password" displayaspassword = "true"/>
</MX: formitem>
<Mx: formitem>

</MX: formitem>
</MX: Form>
<Mx: hslider includein = "loginstate" Top = "100" Left = "100" thumbcount = "2">

</MX: hslider>
<COM: daTa id = "my" verticalcenter = "0" horizontalcenter = "0" includein = "mainstate" width = "100%" Height = "100%" Top = "0" Left = "0 "Right =" 0 "/>
 
</S: windowedapplication>

Reprinted: http://wangjianchao-hb.blog.163.com/blog/static/171188092010320105916839/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.