Internet Explorer 6 is not supported.
Upgrade to view this site.

Custom Right Click Menu

Posted on February 18, 2008 at 2:18 pm

To make a custom right click menu, you will need to use 3 classes. First, use the ContextMenuItem class to make new contextual menu items. Then, make an empty context menu with the ContextMenu class. Lastly those contextual menu items need to be passed to the Array class.

Create a new variable and data type it to ContextMenuItem. Make as many new contextual menu item vars as you want. The new ContextMenuItem takes 4 parameters. The first is a string, written in quotes. This is the text that will appear in the menu. The rest of the parameters are determined by Booleans, true or false. The next parameter is separator before. False means no separator, true means include one. Then the next is enabled. Default is true or enabled, this parameter is optional. Last parameter is visible, default is true.

1
2
var cmItem1:ContextMenuItem = new ContextMenuItem("First Item", false, false, true);
var cmItem2:ContextMenuItem = new ContextMenuItem("Second Item", false, false, true);

Then make a new instance of ContextMenu. That new instance has to be set to be the context menu for the main timeline. Assuming that you are writing this on the main timeline use the keyword this and use the contextMenu property of the ContextMenu class and make it equal to the new ContextMenu variable.

1
2
var cm:ContextMenu = new ContextMenu();
this.contextMenu = cm;

Then create an Array object to contain the contextual menu items. An array can be written like any other new instance of a class or it can be written shorthand. In the shorthand use only the brackets [] to contain the array items.

1
var cmArray:Array = [cmItem1, cmItem2];

Then those custom items in the array have to be added to the menu. This is done by using the customItems property and setting it equal to the array variable. Optionally, you can hide the built in menu items with the hideBuiltInItems method. Below is the completed code for the custom contextual menus.

1
2
3
4
5
6
7
8
//custom context menu
var cmItem1:ContextMenuItem = new ContextMenuItem("First Item", false, false, true);
var cmItem2:ContextMenuItem = new ContextMenuItem("Second Item", false, false, true);
var cm:ContextMenu = new ContextMenu();
this.contextMenu = cm;
var cmArray:Array = [cmItem1, cmItem2];
cm.customItems = cmArray;
cm.hideBuiltInItems();

Leave a Comment

*Required fields. Email will not be published.