咖啡伴侣

呆在上海
posts - 163, comments - 156, trackbacks - 0, articles - 2

Flex自定义右键菜单-1屏蔽默认菜单

Posted on 2011-11-28 17:01 oathleo 阅读(2287) 评论(0)  编辑  收藏 所属分类: Flex
不要试图改变flash插件自带的右键菜单,没有办法解决
自带的右键菜单坏处大大
1.不能去掉默认的几项(关于)
2.不能实现多层

只能使用js屏蔽掉默认右键事件,然后弹出自己的右键菜单来实现
搜索了半天,找了个可行的方案,实现第一步:
1.屏蔽默认菜单,并响应右键事件

var RightClick = {
    
/**
     *  Constructor
     
*/ 
    init: function () {
        
this.FlashObjectID = "customRightClick";
        
this.FlashContainerID = "flashcontent";
        
this.Cache = this.FlashObjectID;
        
if(window.addEventListener){
             window.addEventListener(
"mousedown"this.onGeckoMouse(), true);
        } 
else {
            document.getElementById(
this.FlashContainerID).onmouseup = function() { document.getElementById(RightClick.FlashContainerID).releaseCapture(); }
            document.oncontextmenu 
= function(){ if(window.event.srcElement.id == RightClick.FlashObjectID) { return false; } else { RightClick.Cache = "nan"; }}
            document.getElementById(
this.FlashContainerID).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.FlashContainerID).setCapture();
            
if(window.event.srcElement.id)
            RightClick.Cache 
= window.event.srcElement.id;
        }
    },
    
/**
     * Main call to Flash External Interface
     * 'flexview_rightClick'
     
*/
    call: function() {
        document.getElementById(
this.FlashObjectID).flexview_rightClick();
    }
}


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
<html>
    
<head>
    
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
<title>TEST</title>
    
    
<script type="text/javascript" src="swfobject.js"></script>
    
<script type="text/javascript" src="rightClick.js"></script>

    
    
</head>
    
<body onload="RightClick.init();">
        
<div id="flashcontent">Flash Player 10 required</div>
        
<script type="text/javascript">
           var so 
= new SWFObject("RightClickAS3.swf""customRightClick""560""420""9""#CCCCCC");
            so.addParam(
"quality""high");
            so.addParam(
"name""customRightClick");
            so.addParam(
"id""customRightClick");
            so.addParam(
"AllowScriptAccess""always");
            so.addParam(
"wmode""opaque");
            so.addParam(
"menu""false");
            so.addVariable(
"variable1""value1");
            so.write(
"flashcontent");
        
</script>
    
</body>
    
</html>
   


package com
{
    
import flash.display.*;
    
import flash.external.ExternalInterface;
    
public class RightClick extends Sprite
    {
        
public function RightClick()
        {
            var methodName:String 
= "flexview_rightClick";
            var method:Function 
= onRightClick;
            ExternalInterface.addCallback(methodName, method);
        }
        
private function onRightClick():void {
            var mx:
int = stage.mouseX;
            var my:
int = stage.mouseY;
            trace(mx 
+ ":" + my);
            
if(my> 0 && my <stage.stageHeight && mx> 0 && mx <stage.stageWidth) {
                
// YOUR CODE HERE
            }
        }
    }
}

只有注册用户登录后才能发表评论。


网站导航: