Customize the Right-click menu in Flash | ContextMenuItem Tutorial

Overview:

Flash give publishers the opportunity to customize the right-click menu which pops up in the swf file with a context menu item in actionscript.

ContextMenuItem
ContextMenuItem(caption:String, callbackFunction:Function, [separatorBefore:Boolean], [enabled:Boolean], [visible:Boolean])
Creates a new ContextMenuItem object that can be added to the ContextMenu.customItems array.

Steps:

The menu item has a caption, which is displayed to the user in the right click menu. It also has a a callback function handler by naming the function in the code to be invoked once the menu item is selected. It then has three boolean values which specify whether the item has a separator before it, is enabled, and is visible.

To add a new context menu item to a context menu, you simply create the context menu items and then push them into the customItems array.
You can enable or disable specific menu items, make items visible or invisible, or change the caption or callback handler associated with a menu item at any time.
In the example here the menu items about clearing and rewriting the text are set to toggle each other, so you can’t rewrite the text if it hasn’t yet been cleared and vice versa.

To further customize the context menu flash allows us to hide the built in items in the menu with hideBuiltInItems(). This hides all the built in item from view (except ‘settings’) by setting their visibility to false.

Example:

[kml_flashembed publishmethod=”dynamic” fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2008/01/right-clickmenu.swf” width=”550″ height=”550″ targetclass=”flashmovie”]

Get Adobe Flash player

[/kml_flashembed]
Actionscript:
[cc lang=”actionscript” tab_size=”2″ lines=”60″]
var myMenu:ContextMenu = new ContextMenu();
myMenu.hideBuiltInItems();
var ccs:ContextMenuItem = new ContextMenuItem(“Visit Circle Cube Studio”, visitCCS, false, true, true);
var pog:ContextMenuItem = new ContextMenuItem(“Visit Interactive Flash Portfolio”, visitPOG, false, true, true);
var ct:ContextMenuItem = new ContextMenuItem(“Clear Text”, clearText, true, true, true);
var rw:ContextMenuItem = new ContextMenuItem(“Rewrite Text”, rwText, true, false, true);
var mt:ContextMenuItem = new ContextMenuItem(“Move Text”, moveText, false, true, true);

myMenu.customItems.push(ccs, pog, ct, mt, rw);
_root.menu = myMenu;

function visitCCS () {
getURL(“https://circlecube.com/circlecube”, “_blank”);
}
function visitPOG () {
getURL(“http://www.circlecube.com/test/”, “_blank”);
}
function clearText() {
myText = “”;
ct.enabled = false;
rw.enabled = true;
}
function rwText() {
myText = “Rewrite: \nRight-click to see the customized menu”;
ct.enabled = true;
rw.enabled = false;
}
function moveText() {
theText._y += 10;
}
[/cc]

Download:

Download the Zip file (right-clickMenu.zip)

5 thoughts on “Customize the Right-click menu in Flash | ContextMenuItem Tutorial

  1. Hello,
    can you tell me how to implement this?

    I have a script:

    function AC_AddExtension(src, ext)
    {
    if (src.indexOf(‘?’) != -1)
    return src.replace(/\?/, ext+’?’);
    else
    return src + ext;
    }

    function AC_Generateobj(objAttrs, params, embedAttrs)
    {
    var str = ”;
    for (var i in params)
    str += ‘ ‘;
    str += ”;

    document.write(str);
    }

    function AC_FL_RunContent(){
    var ret =
    AC_GetArgs
    ( arguments, “.swf”, “movie”, “clsid:d27cdb6e-ae6d-11cf-96b8-444553540000”
    , “application/x-shockwave-flash”
    );
    AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
    }

    function AC_SW_RunContent(){
    var ret =
    AC_GetArgs
    ( arguments, “.dcr”, “src”, “clsid:166B1BCA-3F9C-11CF-8075-444553540000”
    , null
    );
    AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
    }

    function AC_GetArgs(args, ext, srcParamName, classid, mimeType){
    var ret = new Object();
    ret.embedAttrs = new Object();
    ret.params = new Object();
    ret.objAttrs = new Object();
    for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();

    switch (currArg){
    case “classid”:
    break;
    case “pluginspage”:
    ret.embedAttrs[args[i]] = args[i+1];
    break;
    case “src”:
    case “movie”:
    args[i+1] = AC_AddExtension(args[i+1], ext);
    ret.embedAttrs[“src”] = args[i+1];
    ret.params[srcParamName] = args[i+1];
    break;
    case “onafterupdate”:
    case “onbeforeupdate”:
    case “onblur”:
    case “oncellchange”:
    case “onclick”:
    case “ondblClick”:
    case “ondrag”:
    case “ondragend”:
    case “ondragenter”:
    case “ondragleave”:
    case “ondragover”:
    case “ondrop”:
    case “onfinish”:
    case “onfocus”:
    case “onhelp”:
    case “onmousedown”:
    case “onmouseup”:
    case “onmouseover”:
    case “onmousemove”:
    case “onmouseout”:
    case “onkeypress”:
    case “onkeydown”:
    case “onkeyup”:
    case “onload”:
    case “onlosecapture”:
    case “onpropertychange”:
    case “onreadystatechange”:
    case “onrowsdelete”:
    case “onrowenter”:
    case “onrowexit”:
    case “onrowsinserted”:
    case “onstart”:
    case “onscroll”:
    case “onbeforeeditfocus”:
    case “onactivate”:
    case “onbeforedeactivate”:
    case “ondeactivate”:
    case “type”:
    case “codebase”:
    ret.objAttrs[args[i]] = args[i+1];
    break;
    case “width”:
    case “height”:
    case “align”:
    case “vspace”:
    case “hspace”:
    case “class”:
    case “title”:
    case “accesskey”:
    case “name”:
    case “id”:
    case “tabindex”:
    ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
    break;
    default:
    ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
    }
    ret.objAttrs[“classid”] = classid;
    if (mimeType) ret.embedAttrs[“type”] = mimeType;
    return ret;
    }

    Can I implement it in here somewhere?

  2. Can you customoize a right click to have customized menus on different areas of a screen instead of having those options only available globaly?

  3. I try this same tutorials in action script 3.0. I got all the menu.
    But I am not able to give a email link to my “email me” item.

    So can you create a this same tutorials in action script 3

    Thanks.

Comments are closed.