Flex自定义右键菜单具体实现
程序员文章站
2022-03-07 18:38:13
1.自定义右键菜单注册类: 项目中新增注册类 rightclickmanager,代码如下: 复制代码 代码如下: package com.siloon.plugin.ri...
1.自定义右键菜单注册类:
项目中新增注册类 rightclickmanager,代码如下:
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. 打开自己的flex工程下的html-template文件夹下的index.template.html文件(右击-open with-text editor),在var params = {};语句的下面添加下面的语句:
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 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. 打开自己的flex工程下的html-template文件夹下的index.template.html文件(右击-open with-text editor),在var params = {};语句的下面添加下面的语句:
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.完整代码如下:
示例代码文件
上一篇: WML学习之六 事件