-- online Search!

Context Menus in Flash MX 2004

by kirupa   | filed under Flash and ActionScript

TL;DR

Flash MX 2004's ContextMenu class lets you control the right-click menu in Flash Player. You can hide built-in items with hideBuiltInItems(), add your own ContextMenuItem entries that call a function when selected, and attach a menu to the whole movie or to a specific button, movie clip, or text field through its menu property.

  • Create a ContextMenuItem with a caption and a handler function, then push it into the menu's customItems array.
  • To hide only some built-in items, set properties like builtInItems.zoom or builtInItems.print to false instead of hiding them all.
  • The handler can do anything you want; the example opens a mail client with getURL using a mailto: link with a preset subject and body.
  • Assigning mail_mc.menu = mail_cm gives just that clip its own menu, while _root.menu applies to the movie in general.

From the official Macromedia documentation:

The ContextMenu class provides runtime control over the items in the Flash Player context menu, which appears when a user right-clicks (Windows) or Control-clicks (Macintosh) on Flash Player. You can use the methods and properties of the ContextMenu class to add custom menu items, control the display of the built-in context menu items (for example, Zoom In and Print), or create copies of menus.

You can attach a ContextMenu object to a specific button, movie clip, or text field object, or to an entire movie level. You use the menu property of the Button, MovieClip, or TextField classes to do this.

Having read the official Macromedia definition of this cool new feature of Flash MX2004, lets see how you can use this.
 

[ right click on the three graphic, text, etc. ]

First, we get rid of (most of) the normal menu items:

var newMenu = new ContextMenu(); //newMenu will be the name of our ContextMenu object
newMenu.hideBuiltInItems(); //this method is the one that hides stuff such as print
/*if you wanted to hide just ceratin items, youd use newMenu.builtInItems.[propName]=false; where propName can be:
save, zoom, quality,play,loop,rewind,forward, back, and print*/
_root.menu = newMenu; //attached to _root, so valid for the swf in general

If you check out the properties and methods for ContextMenu in the help panel, youll see that we got:

  • customItems (an array)

  • hideBuiltInItems (we just used that above..)

And for the menu items themselves, we can use:

  • ContextMenuItem.caption

  • ContextMenuItem.copy

  •  ContextMenuItem.enabled

  • ContextMenuItem.onSelect

  • ContextMenuItem. separatorBefore

  • ContextMenuItem.visible

The names being pretty much self-explanatory!


Now, lets see how we use these features to build our own menu: (all script and a few clips)

  1. Select frame 1 and open the ActionScript panel.
     

  2. Here, Im going to run you through just one of the 5 menus you can see in the example fla/swf above. Theres one on _root, one on each clip, one on the button, and you also get new options on selectable textfields and urls as you can see.

// the mail clip:
var mail_cm = new ContextMenu(); //declare new menu object
// create the item, then push into the Item Array of the new menu object:
//name of new item (Label that gets displayed, function to execute when clicked) is the idea
var mailItem_cm = new ContextMenuItem("Send Email to Kirupa", mailHandler);
//push the new item into the the menu array
mail_cm.customItems.push(mailItem_cm);
//now define the function you set up above to be called when the item is used (clicked)
function mailHandler(obj, menuItem){
getURL("mailto:kirupa@not_kirupa.com?subject=From the ContextMenu tutorial&body=Hi Kirupa");
}
/*the function can be anything you want, it will have to take these 2 parameters, but then the code to execute is really all up to you, I used (here) a getUrl to open the mail client with a predefined subject and message header; in the swf also a gotoAndPlay/Stop, a regular getURL to open a _blank webpage to the forums, another item linking to Flashkit which is set to disabled, and finally a function to have the bugs head clip play to another frame, it is really anything you can come up with..!*/
//finally, we need to associate the new menu with a clip (or button, or textfield), and were done.
mail_mc.menu = mail_cm;
//do not get confused with the code hint extensions : _cm for contextmenu, _mc for movieclip

Thats it, quite simple, no?

Download Download Source

Please look at the code in the fla, check the comments, and then get imaginative and  make your own menus; if you come up with something cool, let us know!! ;)

Also, if you have any questions or comments, feel free to post them in the appropriate forum on the kirupaForum.

Cheers,
Eye icon Eyezberg
flasheyez.com

 

Before you go, here are a few things you can do:

  1. Keep reading

    Read the next article in the series, Using CSS Styles, or check out some related content:

  2. Ask a question

    Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

:: Copyright © KIRUPA 2026 //--