主题:  如何在D中实现右键菜单?

rainy_5d

职务:普通成员
等级:2
金币:1.0
发贴:225
注册:2000/10/23 19:44:16
#12001/1/7 23:11:29
以前在MMC的论坛中好像有讨论,众说纷纭,有的朋友建议用flash、有的用MIAW、有的用图片,我更倾向用图片。前几天写了一个Behavior。发过来和大家一起讨论,若你有更好的,呵呵......

该行为支持最多8个菜单项,请制作菜单项图片和一个菜单弹出范围的shape,并将行为赋给它们。注意事项:shape应选择透明且比菜单图片的通道低;应将菜单图片sprite放在Movie中较高的通道中;菜单图片的注册点均应在左上角"菜单执行命令根据自己的情况,将调用函数放在on mouseUp me的case中就可以执行了。

--**********************************************
--** Rainy 2001/01/03
--** 右键快捷菜单
--** 菜单约束框通道号应低于菜单,
--** 最好将几个菜单图片放在较高的通道中
--**********************************************

global gMenuChannelList
property pMenu1NLocH,pMenu1NLocV
property pMenuItemH,pMenuItemW
property pHowItem
property pItemNum
property pMenuInk,pMenuRovInk
property pMenuW,pMenuH
property pStageWidth,pStageHeight
property pConChannel
--------------------------------

on getPropertyDescriptionList me
mylist=[:]
addprop mylist,#pHowItem,[#comment:"共有多少个菜单项?",#format:#Integer,#default:4,#range:[#min:1,#max:8]]
addprop mylist,#pItemNum,[#comment:"这是第几个菜单项?",#format:#Integer,#default:1,#range:[#min:1,#max:8]]
addprop mylist,#pConChannel,[#comment:"菜单弹出范围框的通道号是:",#format:#Integer,#default:1,#range:[#min:1,#max:120]]
return mylist
end

-------------------------------
on beginSprite me
pStageWidth=the stage.rect.width
pStageHeight=the stage.rect.height
pMenuInk=0 --ink copy
pMenuRovInk=4 --ink NOT copy
pMenu1NLocH=-1000
pMenu1NlocV=-1000
if not (listP(gMenuChannelList)) then gMenuChannelList=[]
pMySprite=me.spriteNum
me.getMenuChannelList()
if pMysprite=pConChannel or (gMenuChannelList.getOne(0) or gMenuChannelList.count() lastmenuchannellist=gMenuChannelList.duplicate()
lastmenuchannellist.sort()
lastmenuchannel=gMenuChannelList.getLast()
pMenuItemW=sprite(lastmenuchannel).width
pMenuItemH=sprite(lastmenuchannel).height
pMenuW=pMenuItemW
pMenuH=pMenuItemH*pHowItem
x=[pMenuItemW,pMenuItemH,pMenuW,pMenuH]
sendsprite pConChannel,#sendMenuChannelList,x
AllMenuItemShow me,pMenu1NLocH,pMenu1NLocV
end

---------------------------------
on mousewithin me
if pMySprite<>pConChannel then
sprite(pMySprite).ink=pMenuRovInk
cursor 280
end if
end

on mouseLeave me
if pMySprite<>pConChannel then
sprite(pMySprite).ink=pMenuInk
cursor 0
end if
end


---------------------------------
on getMenuChannelList me
if pMySprite<>pConChannel then gMenuChannelList[pItemNum]=pMySprite
end
---------------------------------
on sendMenuChannelList me,x
pMenuItemW=x.getAt(1)
pMenuItemH=x.getAt(2)
pMenuW=x.getAt(3)
pMenuH=x.getAt(4)
end

---------------------------------
on mouseUp me
cursor 0
if pMySprite=pConChannel then
allMenuItemShow me,pMenu1NLocH,pMenu1NLocV
return()
end if

case (pItemNum) of
1:
put "menu1 click" --将右键执行的命令函数放到这里
2:
put "menu2 click"
3:
put "menu3 click"
4:
put "menu4 click"
5:
put "menu5 click"
6:
put "menu6 click"
7:
put "menu7 click"
8:
put "menu8 click"
end case
allMenuItemShow me,pMenu1NLocH,pMenu1NLocV
pass
end

----------------------------------
on rightMouseDown me
if pMySprite<>pConChannel then return()
if ((pStageHeight-the mouseV) h=the mouseH
v=the mouseV-pMenuH
allMenuItemShow me,h,v
return()
end if

if ((pStageWidth-the mouseH) if ((pStageHeight-the mouseV) h=the mouseH-pMenuW
v=the mouseV-pMenuH
allMenuItemShow me,h,v
return()
else
h=the mouseH-pMenuW
v=the mouseV
allMenuItemShow me,h,v
return()
end if
end if
h=the mouseH
v=the mouseV
allMenuItemShow me,h,v
end

---------------------------------
on allMenuItemShow me,h,v
repeat with x=1 to pHowItem
sprite(gMenuChannellist[x]).locH=h
sprite(gMenuChannellist[x]).locV=v+(x-1)*pMenuItemH
end repeat
end
----------------------------------
on getBehaviorTooltip me
return \
"该行为支持最多8个菜单项,请制作菜单项图片和一个菜单弹出范围的shape," & \
"注意事项:shape应选择透明且比菜单图片的通道低;应将菜单图片sprite放在" & \
"Movie中较高的通道中;菜单图片的注册点均应在左上角。" & RETURN & RETURN & \
"菜单执行命令根据自己的情况,将调用函数放在on mouseUp me的case中" & \
"就可以执行了。"
end

----------------------------------
on getBehaviorDescription me
return \
"该行为支持最多8个菜单项,制作菜单项图片和一个菜单弹出范围的shape。" & RETURN & RETURN & \
"注意事项:" & RETURN &\
"*shape应选择透明且比菜单图片的通道低;" & RETURN & \
"*应将菜单图片sprite放在Movie中较高的通道中;" & RETURN & \
"*菜单图片的注册点均应在左上角。" & RETURN & RETURN & \
"菜单执行命令根据自己的情况,将调用函数放在on mouseUp me的case中" & \
"就可以执行了。"
end
----------------------------------
on isOKToAttach (me, aSpriteType, aSpriteNum)
case aSpriteType of
#graphic:
return getPos([#bitmap,#shape], sprite(aSpriteNum).member.type) <> 0
#script:
return FALSE
end case
end

编辑历史:[这消息被rainy编辑过(编辑时间2001-01-07 23:15:47)]
[这消息被rainy编辑过(编辑时间2001-01-07 23:17:30)]
[这消息被rainy编辑过(编辑时间2001-01-07 23:25:41)]