« UpdateManager Updated :-) | Main | RIA Developer Camp »
AIR ContextWindow
| By Rich Tretola | October 18, 2007 | |
| 8,189 views |
Looking for an easy way to add window controls as a context menu to your AIR windows? Try my new ContextWindow class.

OK, so this is not rocket science by any means, it is just a class that extends mx.core.Window and creates a NativeMenu within the constructor and assigns it to the contextMenu property. To use it, simply download the class and create an instance of it just as you would with the regular Window class.
Here is a sample application file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <![CDATA[ import com.everythingflex.air.components.ContextWindow; private function createWindow():void{ var w:ContextWindow = new ContextWindow(); w.width=200; w.height=100; w.open(); } ]]> </mx:Script> <mx:Button click="createWindow()" label="Create Wndow" horizontalCenter="0" verticalCenter="0"/> </mx:WindowedApplication> |
Here is the ContextWindow.as class file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package com.everythingflex.air.components { import flash.display.NativeMenu; import flash.display.NativeMenuItem; import flash.events.Event; import mx.core.Window; public class ContextWindow extends Window { public function ContextWindow():void{ super(); createMenu(); } private function createMenu():void{ var mainMenu:NativeMenu = new NativeMenu(); var minimizeMenu:NativeMenuItem = new NativeMenuItem("Minimize"); var maximizeMenu:NativeMenuItem = new NativeMenuItem("Maximize"); var restoreMenu:NativeMenuItem = new NativeMenuItem("Restore"); var closeMenu:NativeMenuItem = new NativeMenuItem("Close"); 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(restoreMenu); mainMenu.addItem(closeMenu); this.contextMenu=mainMenu;; } private function handleMenuClick(e:Event):void{ var menuItem:NativeMenuItem = e.target as NativeMenuItem; if(menuItem.label == "Minimize") this.minimize(); if(menuItem.label == "Maximize") this.maximize(); if(menuItem.label == "Restore") this.restore(); if(menuItem.label == "Close") this.close(); } } } |
Topics: Adobe AIR, Components | 15 Comments »









October 19th, 2007 at 9:33 pm
public class ContextWindow extends Window
but why:
ContextWindow can’t find open();
Reply to this comment
October 22nd, 2007 at 12:32 pm
Not sure, this works fine for me:
var w:ContextWindow = new ContextWindow();
w.open();
Reply to this comment
December 12th, 2007 at 11:38 am
Thanks for all the different interesting articles. We like
to visit your website and the diverse postings.
Reply to this comment
June 27th, 2008 at 7:27 am
Is there a way to specify a context menu for the main application instead of one for a popup ? Thanks for this usefull article
Reply to this comment
June 27th, 2008 at 8:17 am
Yes, but not with this component. When you create a context menu, you can attach it to any object (Images, Panel, Boxes, etc).
Reply to this comment
August 11th, 2008 at 8:31 am
please any body can help me in achieving the functionality of minimising and maximising the menu in flex with the same button??
thanks in advance
Reply to this comment
October 30th, 2008 at 7:44 am
thanks.
Reply to this comment
January 28th, 2009 at 2:43 pm
For sharing thank you very much good very beautiful work
Reply to this comment
July 15th, 2009 at 7:51 pm
Has anyone had success with adding a contextmenu to an HTML control in AIR?
I cannot get it to fire, though I am able using same method of setting the “contextMenu” with any other control, without any issue.
Interesting though, I can get the rightClick event to fire.
Thoughts?
Any insight would be uuberly appreciated.
Reply to this comment
July 15th, 2009 at 7:52 pm
Has anyone had success with adding a contextmenu to an HTML control in AIR?
I cannot get it to fire, though I am able using same method of setting the “contextMenu” with any other control, without any issue.
Interesting though, I can get the rightClick event to fire.
Thoughts?
Any insight would be uuberly appreciated.
Reply to this comment
September 3rd, 2009 at 1:37 pm
[...] conciseness, and don’t forget bindability. This makes it a much cleaner solution than a component which creates a commonly used menu or using XML data providers to define the [...]
September 5th, 2009 at 3:11 am
For sharing thank you very much good very beautiful work
Reply to this comment
November 14th, 2009 at 3:10 am
thank you admin
Reply to this comment
December 29th, 2009 at 10:43 pm
thank you admin
Reply to this comment
January 13th, 2010 at 11:28 am
thanks very goodd
Reply to this comment