Flex自定义右键菜单具体实现
网络编程 2021-07-05 11:32www.168986.cn编程入门
自定义右键菜单在flex的实现想必有很多朋友都没有尝试过吧,狼蚁网站SEO优化与大家分享下如何实现,具体代码如下
1.自定义右键菜单注册类:
项目中新增注册类 RightClickManager,代码如下
package .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://.uza.lt
Dan Florio
http://.polygeek.
Project website:
http://code.google./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.sPropagation) eventObject.sPropagation();
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. 打开自己的Flex工程下的html-template文件夹下的index.template.html文件(右击-Open With-Text Editor),在var params = {};语句的狼蚁网站SEO优化添加狼蚁网站SEO优化的语句
params.wmode = "opaque";//屏蔽系统右键菜单的关键
--------------------------------------------------------------------------------
3. 在主程序文件中引入
//初始化
protected function init():void
{
if (!rightClickRegisted)
{
maxNumText.text=rightClickRegisted.toString();
RightClickManager.regist();
rightClickRegisted = true;
}
this.addEventListener(RightClickManager.RIGHT_CLICK,rightClickHandler);
maxNumText.text+="init";
}
//创建菜单项
private function createMenuItems():Array
{
var menuItems:Array = new Array();
var menuItem:Object;
menuItem = new Object;
menuItem.label = '刷新'; //菜单项名称
//menuItem.itemIcon = this.menu_SX;//菜单项图标
menuItems.push(menuItem);
return menuItems;
}
//生成右键菜单
private function initMenu():void
{
menu = Menu.createMenu(this, createMenuItems(), false);
//menu.iconField="itemIcon";//右键菜单的图标
//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";
}
//菜单项点击事件
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.完整代码如下
项目中新增注册类 RightClickManager,代码如下
代码如下:
package .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://.uza.lt
Dan Florio
http://.polygeek.
Project website:
http://code.google./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.sPropagation) eventObject.sPropagation();
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. 打开自己的Flex工程下的html-template文件夹下的index.template.html文件(右击-Open With-Text Editor),在var params = {};语句的狼蚁网站SEO优化添加狼蚁网站SEO优化的语句
params.wmode = "opaque";//屏蔽系统右键菜单的关键
--------------------------------------------------------------------------------
3. 在主程序文件中引入
代码如下:
//初始化
protected function init():void
{
if (!rightClickRegisted)
{
maxNumText.text=rightClickRegisted.toString();
RightClickManager.regist();
rightClickRegisted = true;
}
this.addEventListener(RightClickManager.RIGHT_CLICK,rightClickHandler);
maxNumText.text+="init";
}
//创建菜单项
private function createMenuItems():Array
{
var menuItems:Array = new Array();
var menuItem:Object;
menuItem = new Object;
menuItem.label = '刷新'; //菜单项名称
//menuItem.itemIcon = this.menu_SX;//菜单项图标
menuItems.push(menuItem);
return menuItems;
}
//生成右键菜单
private function initMenu():void
{
menu = Menu.createMenu(this, createMenuItems(), false);
//menu.iconField="itemIcon";//右键菜单的图标
//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";
}
//菜单项点击事件
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.完整代码如下
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程