Adding a link to context menu in Flash Player 10

by Peter deHaan on August 26, 2008

in ActionScript, ContextMenu, URLRequest

The following example shows how you can add a custom link to a context menu in Flex Gumbo and Flash Player 10 by setting the link and contextMenu properties.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see Using the beta Gumbo SDK in Flex Builder 3″.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/26/adding-a-link-to-context-menu-in-flash-player-10/ -->
<Application name="Image_contextMenu_link_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        xmlns:mx="library:adobe/flex/halo"
        xmlns:net="flash.net.*"
        xmlns:ui="flash.ui.*"
        layout="flex.layout.BasicLayout">

    <mx:Label id="lbl"
            text="Right-click image control for context menu"
            left="10"
            top="10" />

    <mx:Image id="img"
            source="@Embed('assets/fx_appicon-tn.gif')"
            verticalCenter="0"
            horizontalCenter="0">
        <mx:contextMenu>
            <ui:ContextMenu id="imageContextMenu">
                <ui:link>
                    <net:URLRequest url="http://www.adobe.com/go/flex"/>
                </ui:link>
            </ui:ContextMenu>
        </mx:contextMenu>
    </mx:Image>

</Application>

View source is enabled in the following example.

You can also set the link and contextMenu properties using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/26/adding-a-link-to-context-menu-in-flash-player-10/ -->
<Application name="Image_contextMenu_link_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        xmlns:mx="library:adobe/flex/halo"
        layout="flex.layout.BasicLayout"
        initialize="init();">

    <Script>
        <![CDATA[
            [Embed("assets/fx_appicon-tn.gif")]
            private var fxIcon:Class;

            private var imageContextMenu:ContextMenu;
            private var urlReq:URLRequest;

            private function init():void {
                urlReq = new URLRequest();
                urlReq.url = "http://www.adobe.com/go/flex";

                imageContextMenu = new ContextMenu();
                imageContextMenu.link = urlReq;

                lbl.text = "Right-click image control for context menu";
                lbl.setStyle("left", 10);
                lbl.setStyle("top", 10);

                img.source = fxIcon;
                img.setStyle("horizontalCenter", 0);
                img.setStyle("verticalCenter", 0);
                img.contextMenu = imageContextMenu;
            }
        ]]>
    </Script>

    <mx:Label id="lbl" />
    <mx:Image id="img" />

</Application>

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: