ActionScript - Context Menus
Most Windows applications provide context menus that are displayed
when you click the right mouse button.
Flash supports a similar capability ... except that there are 2
menu options that can not be removed
- Settings and About Flash.
The basic procedure is to
- Create a context menu
- Attach the menu to an object
- Create and add menu options
- Attach a function to each menu option
This allows each Button, Text Field, etc to have its own
context menu.
Form Context Menu
Adding a context menu to the form itself follows the same
procedure.
The help explicitly states that you can not use the built-in
context menu - instead you must create one and attach it to ...... ?
Well, that's not real clear.
You are not allowed to assign a context menu to the stage
(though the help file says you can) -
I tried, it produces an error.
As shown below,
just create a new context menu and then assign it to
the application variable.
(Notice that the first *c* is capitalized in one case and not the other.)
contextMenu = new ContextMenu;
|
Adding *About*
When you create a project, you should add your name to it
... or at least some descriptive information.
This example shows how to add an About... selection
to the context (right click) menu.
The following code
- Modifies the default context menu to remove all the
default options except Settings and About Flash
- Adds a menu option
- Adds a function that is executed when the option is selected
- Opens a web page that displays information
var FMenuItem : ContextMenuItem = new ContextMenuItem("About xyz version 1.0...");
FMenuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuHandler);
contextMenu = new ContextMenu;
contextMenu.hideBuiltInItems();
contextMenu.customItems.push(FMenuItem);
var FLink: String = "http://mc-computing.com/WebTools/Generic_mcGraph/Overview.html";
public function menuHandler(event:ContextMenuEvent):void {
var urlReq:URLRequest = new URLRequest(FLink);
navigateToURL(urlReq, "_blank");
}
|
Note -
navigateToURL fails when opening a web page locally
(normal security setting),
but works fine when the swf file is read from a server.
It also works when running inside the Flash CS3 IDE.
Author: Robert Clemenzi -
clemenzi@cpcug.org