1.自定義右鍵菜單注冊(cè)類:
項(xiàng)目中新增注冊(cè)類 RightClickManager,代碼如下:
復(fù)制代碼 代碼如下:
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 { RightClick.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 InteractiveObject);
rightClickTarget.dispatchEvent(event);
}// end if
return;
}
}
}
2. 打開(kāi)自己的Flex工程下的html-template文件夾下的index.template.html文件(右擊-Open With-Text Editor),在var params = {};語(yǔ)句的下面添加下面的語(yǔ)句:
params.wmode = "opaque";//屏蔽系統(tǒng)右鍵菜單的關(guān)鍵
--------------------------------------------------------------------------------
3. 在主程序文件中引入:
復(fù)制代碼 代碼如下:
//初始化
protected function init():void
{
if (!rightClickRegisted)
{
maxNumText.text=rightClickRegisted.toString();
RightClickManager.regist();
rightClickRegisted = true;
}
this.addEventListener(RightClickManager.RIGHT_CLICK,rightClickHandler);
maxNumText.text+="init";
}
//創(chuàng)建菜單項(xiàng)
private function createMenuItems():Array
{
var menuItems:Array = new Array();
var menuItem:Object;
menuItem = new Object;
menuItem.label = '刷新'; //菜單項(xiàng)名稱
//menuItem.itemIcon = this.menu_SX;//菜單項(xiàng)圖標(biāo)
menuItems.push(menuItem);
return menuItems;
}
//生成右鍵菜單
private function initMenu():void
{
menu = Menu.createMenu(this, createMenuItems(), false);
//menu.iconField="itemIcon";//右鍵菜單的圖標(biāo)
//menu.labelField="label"; //右鍵菜單的名稱
menu.variableRowHeight = true;
menu.width=100;
menu.addEventListener(MenuEvent.ITEM_CLICK, menuItemSelected); //右鍵菜單的事件
var point:Point = new Point(mouseX,mouseY);
point = localToGlobal(point);
menu.show(point.x,point.y); //顯示右鍵菜單
maxNumText.text="initMenu";
}
//刪除右鍵菜單
private function removeMenu():void
{
if(menu!=null)
{
menu.hide();
menu.removeEventListener(MenuEvent.ITEM_CLICK,menuItemSelected);
menu=null;
}
maxNumText.text="removeMenu";
}
//菜單項(xiàng)點(diǎn)擊事件
private function menuItemSelected(event:MenuEvent):void
{
var menuItem:Object = event.menu.selectedItem as Object;
//……
switch(menuItem.label)
{
case "刷新":
addLine();
break;
// ……
}
}
private function addLine():void
{
maxNumText.text="addLine";
}
//控件右擊事件
private function rightClickHandler(event:MouseEvent):void
{
//tree_onRightClicked(event);
maxNumText.text="rightClickHandler0";
removeMenu();
initMenu();
maxNumText.text="rightClickHandler";
}
4.完整代碼如下:
示例代碼文件
您可能感興趣的文章:- 右鍵發(fā)送(sendto),創(chuàng)建快捷方式到自定義的位置
- 右鍵發(fā)送(sendto),創(chuàng)建快捷方式到自定義的位置 的vbs
- 自定義右鍵屬性覆蓋瀏覽器默認(rèn)右鍵行為實(shí)現(xiàn)代碼
- JavaScript 對(duì)任意元素,自定義右鍵菜單的實(shí)現(xiàn)方法
- js自定義鼠標(biāo)右鍵的實(shí)現(xiàn)原理及源碼
- js實(shí)現(xiàn)完全自定義可帶多級(jí)目錄的網(wǎng)頁(yè)鼠標(biāo)右鍵菜單方法
- Js實(shí)現(xiàn)自定義右鍵行為