- Creating Custom Context Menus. - The context menu appears when you right-click a Flash movie (Control-click on a Macintosh). - Appears when you right-click anything in a Flash movie except a text field.. - Appears when you right-click a text field that's editable or selectable.. - Appears when a Flash movie fails to load within a Web page and you right-click in the empty area.. - All the built-in context menu items can be removed except for the Settings item and the Debugger item.. - The Flash player includes two built-in classes to assist you in creating a customized context menu:. - Each item in a context menu is an instance of this class. - Each element in that array is an instance of the ContextMenuItem class.. - The ContextMenu class and the ContextMenuItem class are used together to build custom context menus.. - When creating a new instance of the ContextMenu class, you can specify a function to be called when that ContextMenu is displayed:. - The menuHandler() function is executed just before the context menu appears. - Script within the function can be used to evaluate certain conditions within the application and items on a context menu can be dynamically added, removed, enabled, or disabled. - You can dynamically change the function a context menu calls before it appears, by redefining its onSelect event handler. - myContextMenu.onSelect = anotherFunction;. - The myContextMenu instance will call anotherFunction() instead of menuHandler()—or whatever function you passed into the ContextMenu constructor when creating it—when the context menu is selected (but before it appears).. - When creating custom context menus, you may want to remove the default items that appear. - myContextMenu.hideBuiltInItems();. - With this method, all built-in items are hidden from the context menu except the Settings and Debugger items.. - Instances of the ContextMenu class have only one property—customItems. - This is an array that stores the custom ContextMenuItem objects that form the custom items that appear on the menu. - myContextMenu.customItems.push(new ContextMenuItem("Next Page", nextPageHandler));. - This statement adds a new ContextMenuItem object to the customItems array of the myContextMenu object. - The first parameter is the text to be displayed in the menu. - When the item is selected from the context menu, the callback function is called.. - Custom menu items in a context menu can be referenced in the following manner:. - myContextMenu.customItems[0. - first custom menu item myContextMenu.customItems[1. - second custom menu item myContextMenu.customItems[2. - third custom menu item. - myContextMenu.customItems[1].enabled = false;. - myContextMenu.customItems[3].enabled = false;. - Disabled menu items still appear on the custom context menu, but they're dimmed and won't function when clicked. - You can dynamically change the function that a context menu item calls when selected,. - myContextMenu.customItems[0].onSelect = differentCallbackFunction;. - Just to clarify, the context menu itself has a callback function that is executed just before the menu appears, and each context menu item has a callback function that's executed when that item is selected from the menu.. - To use a custom context menu, it has to be assigned to a particular movie clip, button, or text field instance. - The assignment causes that custom menu to appear when the instance is right-clicked. - When the mouse is right-clicked over the myClip_mc movie clip instance, the myContextMenu context menu is displayed.. - A single custom context menu can be associated with as many movie clip, button, and text field instances as you want.. - When using custom context menus, the timeline with the highest depth always captures the right-click mouse event, which causes its custom menu to be displayed. - For example, if two movie clips are overlapping and each has an associated custom context menu, the clip that's at a higher depth is the one whose menu is shown when the mouse is right- clicked over that clip. - If the mouse is not over a movie clip that has a custom menu, but the main timeline (_root) has a custom menu, the custom menu for _root will be displayed.. - In the following exercise, you'll create a custom context menu with one custom item,. - used to print the contents of an editable text field.. - Open ContextMenu1.fla in the Lesson21/Assets folder.. - The Text Field layer contains an input text field instance with the name entry_txt. - Frame 1 of the Actions layer is where you'll add the ActionScript for this project.. - When the project is complete, you'll be able to add text to the editable text field;. - when you right-click, you'll be able to select Print Fridge Note from the custom context menu.. - Select Frame 1 in the Actions layer, open the Actions panel, and add the following line to create a new instance of the ContextMenu class:. - This code creates a new instance of the ContextMenu class, named. - This custom context menu will eventually be associated with the entry_txt text field. - o Print any text in the entry_txt text field.. - o Delete any text in the entry_txt text field.. - o Reformat any text in the entry_txt text field so that it's red and consists of uppercase characters.. - In the constructor, a reference to a function called menuHandler() is passed in.. - Add the following menuHandler() function definition below the current script:. - var numberOfItems = myContextMenu.customItems.length;. - if (entry_txt.text.length >. - myContextMenu.customItems[i].enabled = true;. - myContextMenu.customItems[i].enabled = false;. - This function is called just before the myContextMenu menu appears. - The purpose of this function is to enable and disable custom items on that menu on the fly, depending on whether the entry_txt text field contains any text. - If there is text in that field, custom items on the context menu are enabled. - otherwise, the custom items are disabled.. - The value of this variable is based on the number of custom menu items that have been added to the myContextMenu instance. - Next, a conditional statement evaluates whether the user has entered any text into the entry_txt text field. - If the field contains text, the first part of the statement uses a loop to quickly enable all the custom items on the myContextMenu instance. - Now let's add some custom items to the myContextMenu instance.. - Add the following line of script just below the menuHandler() function:. - myContextMenu.customItems.push(new ContextMenuItem("Print Fridge Note", printHandler));. - This line of script adds a new ContextMenuItem instance to the customItems array of the myContextMenu instance. - The first parameter of the ContextMenuItem constructor method contains the text that we want to appear in the menu representing this item. - The second parameter is the callback function that should be executed when the item is selected. - This function is called when Print Fridge Note is selected from the context menu.. - The first line creates a new instance of the PrintJob class. - If result is true, we use the addPage() method of the PrintJob class to add contents of the entry_txt text field to be printed. - Let's add the remaining two items to our custom context menu.. - Add the following script at the end of the current script:. - myContextMenu.customItems.push(new ContextMenuItem("Clear Fridge Note", clearHandler));. - entry_txt.text . - This script adds another custom item to the customItems array of the. - myContextMenu instance. - is the text for this item, and the clearHandler() function is called when this item is selected. - remove any text from the entry_txt text field.. - Add the following script below the current script:. - myContextMenu.customItems.push(new ContextMenuItem("Urgent Fridge Note", urgentHandler));. - entry_txt.textColor = 0x990000;. - entry_txt.text = entry_txt.text.toUpperCase();. - This step adds one more custom item to the myContextMenu instance. - This function takes the text entered into the entry_txt field, makes it red, and converts it to uppercase characters.. - entry_txt.menu = myContextMenu;. - This step associates the myContextMenu instance with the entry_txt text field. - If the user right-clicks this text field, the custom context menu appears. - otherwise, the custom menu won't be shown.. - Type some text in the text field, open the custom context menu, and select a custom menu item.. - Notice that if the text field is blank, the custom menu items are disabled in the context menu. - This is the result of the. - Custom context menus provide an entirely new way for users to interact with an application—without having to manually search for a particular button or control.
Xem thử không khả dụng, vui lòng xem tại trang nguồn hoặc xem
Tóm tắt