|
本帖最后由 CHEARV 于 2013-10-12 09:40 编辑
如题,我创建了两个LISTVIEW控件,如果要为每个控件创建一个右键菜单,这个容易实现。我现在想的是,能不能给两个控件创建同一个右键菜单呢?
如果能,那后面的代码又是如何区分是哪个控件触发的事件呢?#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
Opt("GUIOnEventMode", 1)
$Form1 = GUICreate("Test", 800, 600, 203, 80, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_TABSTOP))
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Click")
$ListView1 = GUICtrlCreateListView("1|2|3|4", 40, 80, 640, 228, -1, $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT )
GUICtrlSetOnEvent($ListView1, "ListView1Click")
$ListViewA = GUICtrlCreateListView("A|B|C|D", 40, 336, 640, 236, -1, $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT)
GUICtrlSetOnEvent($ListViewA, "ListView1Click")
$ContextMenu = GUICtrlCreateContextMenu($ListView1)
;$ContextMenu = GUICtrlCreateContextMenu($ListViewA)
$ContextMenu1 = GUICtrlCreateMenuItem("菜单一", $ContextMenu)
GUICtrlSetOnEvent(-1, "ContextMenuClick")
$ContextMenu2 = GUICtrlCreateMenuItem("菜单二", $ContextMenu)
GUICtrlSetOnEvent(-1, "ContextMenuClick")
GUISetState(@SW_SHOW)
While 1
Sleep(100)
WEnd
Func Form1Click()
Exit
EndFunc
Func ListView1Click()
EndFunc
Func ContextMenuClick()
Switch @GUI_CtrlId
Case $ListView1
Exit
Case $ListViewA
EndSwitch
ENDFunc
最近参考了几个注册消息的代码,能实现上面的功能了,但不能获取产生事件的控制,哪位高手能再指点一下,我对那个注册消息的几个参数根本不懂,代码是照抄过来的。#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include<GuiMenu.au3>
Opt("GUIOnEventMode", 1)
Global Enum $id1 = 1000, $id2, $id3
$Form1 = GUICreate("Test", 800, 600, 203, 80, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_TABSTOP))
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Click")
$ListView1 = GUICtrlCreateListView("1|2|3|4", 40, 80, 640, 228, -1, $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT)
$ListViewA = GUICtrlCreateListView("A|B|C|D", 40, 336, 640, 236, -1, $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT)
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
While 1
Sleep(100)
WEnd
Func Form1Click()
Exit
EndFunc ;==>Form1Click
Func WM_NOTIFY($hWndGUI, $MsgID, $WParam, $LParam)
Local $tagNMHDR, $Event, $hWndFrom, $IDFrom
Local $tagNMHDR = DllStructCreate("int;int;int", $LParam)
If @error Then Return $GUI_RUNDEFMSG
$IDFrom = DllStructGetData($tagNMHDR, 2)
$Event = DllStructGetData($tagNMHDR, 3)
$tagNMHDR = 0
Switch $Event; 选择产生的事件
Case $NM_RCLICK ; 右击
$hMenu = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_AddMenuItem($hMenu, "菜单1", $id1)
_GUICtrlMenu_AddMenuItem($hMenu, "菜单2", $id2)
_GUICtrlMenu_AddMenuItem($hMenu, "菜单3", $id3)
_GUICtrlMenu_TrackPopupMenu($hMenu, $Form1)
_GUICtrlMenu_DestroyMenu($hMenu)
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
Local $tagNMHDR, $Event, $hWndFrom, $IDFrom
Local $tagNMHDR = DllStructCreate("int;int;int", $iLParam)
;If @error Then Return $GUI_RUNDEFMSG
;奇怪,这段代码是从上面移植过来的,但他好像获取不到选择产生事件的控件?谁能在这里帮我获得跟上面那段代码一样的$IDFrom吗?
$IDFrom = DllStructGetData($tagNMHDR, 2)
$Event = DllStructGetData($tagNMHDR, 3)
$tagNMHDR = 0
Switch $iwParam
Case $id1
MyFunc1(1)
Case $id2
MyFunc1(2)
Case $id3
MyFunc1(3)
EndSwitch
EndFunc ;==>WM_COMMAND
|
|