The ContextMenu Class

     

The ContextMenu Class

The ContextMenu class, available only in Flash Player 7, allows you to replace or add to the context menu, which normally appears when the user right-clicks (Windows) or Ctrl-clicks (Mac) in the Flash Player.

Use the constructor new ContextMenu() to create a ContextMenu object. Then you can use the methods and attributes of the ContextMenu class to control the display of the built-in context menu items (such as Zoom In and Zoom Out), add custom menu items, or create a copy of the custom menu.

To add a custom menu item, you push a ContextMenuItem object onto the customItems array, which is a standard property of every ContextMenu object. Each push() adds a new item to the customItems array, and a new option on the custom menu. Each item has a label, which is displayed to the user, and an event handler, which is called when the user selects the item.

The ContextMenu class generates an onSelect event, called when a user brings up the custom context menu that you have created, but before the menu actually displays. There is one onSelect event for your entire custom menu, and one onSelect event for each custom item on the menu.

Using the menu property of the Button , MovieClip , or TextField class, you can assign a custom menu to the Main Timeline, or to an individual button, movie clip, or text field object. The menu property is new in Flash MX 2004.

ContextMenu has two methods:

  • copy , which returns a copy of the specified ContextMenu object.

  • hideBuiltInItems , which hides built-in context menu items (with the exception of Settings) while leaving custom menu items visible. This method sets to false the eight Boolean attributes of the builtInItems object: save , zoom , quality , play , loop , rewind , forward_back , and print , removing the menu items of the same names from the ContextMenu.

ContextMenuItem has one method, copy , which makes a copy of the menu item object.

It has four attributes:

  • caption ” Caption text displayed in the context menu

  • enabled ” Boolean, enables or disables the menu item

  • separatorBefore ” A Boolean, false by default, indicating whether a separator bar should appear above the menu item

  • visible ” A Boolean, true by default, indicating whether the menu item is visible

The following program (contextMenu.fla on the CD accompanying this book) illustrates

  • Creating a custom context menu assigned to the Main Timeline, with event handlers for each option on the menu

  • Trapping the onSelect event for the entire menu

  • Assigning the menu to individual objects on the Stage

Note that existing (built-in) labels (such as Zoom In), even if hidden, cannot be used in your custom menu. In addition, you cannot use the following options: Macromedia, Flash Player, Copy, Save, or Paste. If you try to use any of these, they will simply not show up on your menu. However, you may be able to get around this limitation by putting something after the forbidden phrase, as in Zoom In . However, this trick does not work with the Macromedia, Flash Player, or Settings options. Any label that even contains these phrases will not show up.

 // Start basic program myMenu.hideBuiltInItems(); myMenu.customItems.push( new ContextMenuItem("Zoom In ... ",itemHandler1) ); myMenu.customItems.push( new ContextMenuItem("Calculate  ...",itemHandler2) ); myMenu.customItems.push( new ContextMenuItem("Clear",itemHandler3) ); function itemHandler1() { trace(" pressed Zoom In ... ") }; function itemHandler2() { trace(" pressed Calculate ... ") }; function itemHandler3() { trace(" pressed Clear") }; this.menu = myMenu; // End basic program // Start trapping onSelect for entire menu menuHandler = function (obj:Object, menu:ContextMenu) {   if(obj instanceof MovieClip) {     trace("Movie clip: " + obj);     status = "mc";   }   if(obj instanceof TextField) {     trace("Text field: " + obj);         status = "txt";   }   if(obj instanceof Button) {     trace("Button: " + obj);         status = "but";   } } myMenu.onSelect = menuHandler; // End trapping onSelect for entire menu // Assign menu to individual objects on the Stage mybut.menu = myMenu; but2.menu = myMenu; status_txt.menu = myMenu; myClip.menu = myMenu; 



Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net