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>
 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

Leave a Reply

Your email address will not be published.

You may 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