Flex Custom Right button menu to implement _flex

Source: Internet
Author: User
1. Custom Right Key menu registration class:
New registration Class Rightclickmanager in the project, the code is as follows:
Copy Code code as follows:

Package Com.siloon.plugin.rightClick
{
Import Flash.display.DisplayObject;
Import Flash.display.InteractiveObject;
Import flash.events.ContextMenuEvent;
Import flash.events.MouseEvent;
Import Flash.external.ExternalInterface;
Import mx.core.Application;
public class Rightclickmanager
{
Static private Var Rightclicktarget:displayobject;
static Public Const Right_click:string = "RightClick";
static Private Const JAVASCRIPT:XML =
<script>
<! [cdata[
/**
*
* Copyright 2007
*
* Paulius Uza
* http://www.uza.lt
*
* Dan Florio
* http://www.polygeek.com
*
* Project Website:
* http://code.google.com/p/custom-context-menu/
*
* --
* RightClick for Flash Player.
* Version 0.6.2
*
*/
function (Flashobjectid)
{
var RightClick = {
/**
* Constructor
*/
Init:function (Flashobjectid) {
This. Flashobjectid = Flashobjectid;
This. Cache = this. Flashobjectid;
if (Window.addeventlistener) {
Window.addeventlistener ("MouseDown", This.ongeckomouse (), true);
} else {
document.getElementById (this. Flashobjectid). Parentnode.onmouseup = function () {document.getElementById (Rightclick.flashobjectid). Parentnode.releasecapture (); }
Document.oncontextmenu = function () {if (window.event.srcElement.id = = Rightclick.flashobjectid) {return false;} else {R Ightclick.cache = "Nan"; }}
document.getElementById (this. Flashobjectid). Parentnode.onmousedown = Rightclick.oniemouse;
}
},
/**
* Gecko/webkit Event Overkill
* @param {Object} eventobject
*/
Killevents:function (EventObject) {
if (EventObject) {
if (eventobject.stoppropagation) eventobject.stoppropagation ();
if (Eventobject.preventdefault) Eventobject.preventdefault ();
if (eventobject.preventcapture) eventobject.preventcapture ();
if (eventobject.preventbubble) eventobject.preventbubble ();
}
},
/**
* Gecko/webkit Call Right click
* @param {Object} ev
*/
Ongeckomouse:function (EV) {
return function (EV) {
if (Ev.button!= 0) {
Rightclick.killevents (EV);
if (ev.target.id = = Rightclick.flashobjectid && Rightclick.cache = = Rightclick.flashobjectid) {
Rightclick.call ();
}
Rightclick.cache = ev.target.id;
}
}
},
/**
* IE Call Right click
* @param {Object} ev
*/
Oniemouse:function () {
if (Event.button > 1) {
if (window.event.srcElement.id = = Rightclick.flashobjectid && Rightclick.cache = = Rightclick.flashobjectid) {
Rightclick.call ();
}
document.getElementById (Rightclick.flashobjectid). Parentnode.setcapture ();
if (window.event.srcElement.id)
Rightclick.cache = window.event.srcElement.id;
}
},
/**
* Main call to Flash External Interface
*/
Call:function () {
document.getElementById (this. Flashobjectid). RightClick ();
}
}
Rightclick.init (Flashobjectid);
}
]]>
</script>;
Public Function Rightclickmanager ()
{
Return
}
static public Function regist (): Boolean
{
if (externalinterface.available)
{
Externalinterface.call (JavaScript, Externalinterface.objectid);
Externalinterface.addcallback ("RightClick", dispatchrightclickevent);
Application.application.addEventListener (Mouseevent.mouse_over,mouseoverhandler);
}//End If
return true;
}
Static Private Function Mouseoverhandler (event:mouseevent): void
{
Rightclicktarget = Displayobject (event.target);
Rightclicktarget = Interactiveobject (event.target);
Return
}
Static Private Function dispatchrightclickevent (): void
{
var event:mouseevent;
if (rightclicktarget!= null)
{
event = new MouseEvent (Right_click, True, False, Rightclicktarget.mousex, Rightclicktarget.mousey);
event = new Contextmenuevent (Right_click, True, False, Rightclicktarget as Interactiveobject, Rightclicktarget as Inter Activeobject);
Rightclicktarget.dispatchevent (event);
}//End If
Return
}
}
}

2. Open your own Flex project under the Html-template folder under the Index.template.html file (right-click-open with-text Editor), add the following statement under var params = {}; statement:
Params.wmode = "opaque";//the key to the right button menu of the shielding system
--------------------------------------------------------------------------------
3. Introducing in the main program file
Copy Code code as follows:

Class
protected function init (): void
{
if (!rightclickregisted)
{
Maxnumtext.text=rightclickregisted.tostring ();
Rightclickmanager.regist ();
Rightclickregisted = true;
}
This.addeventlistener (Rightclickmanager.right_click,rightclickhandler);
maxnumtext.text+= "Init";
}
Create a menu item
Private Function Createmenuitems (): Array
{
var menuitems:array = new Array ();
var menuitem:object;
MenuItem = new Object;
Menuitem.label = ' Refresh '; menu item Name
Menuitem.itemicon = this.menu_sx;//menu item icon
Menuitems.push (MenuItem);
return MenuItems;
}
Generate Right-click menu
Private Function Initmenu (): void
{
Menu = Menu.createmenu (this, Createmenuitems (), false);
Menu.iconfield= "Itemicon";//Right-click icon for menu
menu.labelfield= "Label"; Right-click the name of the menu
Menu.variablerowheight = true;
menu.width=100;
Menu.addeventlistener (Menuevent.item_click, menuitemselected); Right-click event for menu
var point:point = new Point (Mousex,mousey);
Point = Localtoglobal (point);
Menu.show (POINT.X,POINT.Y); Show Right Button menu
maxnumtext.text= "Initmenu";
}
Delete the right button menu
Private Function Removemenu (): void
{
if (menu!=null)
{
Menu.hide ();
Menu.removeeventlistener (menuevent.item_click,menuitemselected);
Menu=null;
}
maxnumtext.text= "Removemenu";
}
menu item Click event
Private Function menuitemselected (event:menuevent): void
{
var menuitem:object = Event.menu.selectedItem as Object;
......
Switch (Menuitem.label)
{
Case "Refresh":
AddLine ();
Break
......
}
}
Private Function AddLine (): void
{
maxnumtext.text= "AddLine";
}
Control right-click event
Private Function Rightclickhandler (event:mouseevent): void
{
Tree_onrightclicked (event);
maxnumtext.text= "RightClickHandler0";
Removemenu ();
Initmenu ();
maxnumtext.text= "Rightclickhandler";
}

4. The complete code is as follows
Sample code file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.