请教下高手一个关于树形项目对应数组的问题。
我想在树形控件中触发某个item就能调用对应的数组应该怎么实现呢例如:
数组a包含100个元素,我将这100个元素用循环把他们创建成从 1-100 的treeviewitem 项目。
现在我在已经创建好的树形项目中双击第n个item项目,我应该怎么能让他调用对应第n个数组呢?
可能表达的不是很清楚,麻烦高手们能回答我的问题,非常感谢! $iArrayIndex = 77
GUICtrlCreateTreeViewItem("Table-Driver", $iTreeView)
Assign("." & GUICtrlGetHandle(-1), $iArrayIndex) 热心人真多,感谢感谢!!! 另外我还想知道怎么用鼠标双击能触发事件呢.
我很菜!麻烦上面的朋友了! Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
$hWndTreeview = $hTreeView
If Not IsHWnd($hTreeView) Then $hWndTreeview = GUICtrlGetHandle($hTreeView)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndTreeview
Switch $iCode
Case $NM_DBLCLK ; The user has double-clicked the left mouse button within the control
MsgBox(0, '', '鼠标左键双击')
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY 回复 5# 水木子
感谢,请问怎么调用该函数呢
函数内的参数什么含义呢,麻烦高手举个例子好吗? 双击项目试试!#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
Opt('MustDeclareVars', 1)
$Debug_TV = False ; Check ClassName being passed to functions, set to True and use a handle to another control to see it work
Global $hTreeView, $edit
_Main()
Func _Main()
Local $GUI, $hItem
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
$GUI = GUICreate("(UDF Created) TreeView Create", 400, 540)
$hTreeView = _GUICtrlTreeView_Create($GUI, 2, 2, 396, 268, $iStyle, $WS_EX_CLIENTEDGE)
$edit = GUICtrlCreateEdit("", 2, 277, 394, 258, BitOR($WS_VSCROLL, $ES_AUTOVSCROLL))
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
_GUICtrlTreeView_BeginUpdate($hTreeView)
For $x = 1 To Random(2, 10, 1)
$hItem = _GUICtrlTreeView_Add($hTreeView, 0, StringFormat("[%02d] New Item", $x))
For $y = 1 To Random(2, 10, 1)
_GUICtrlTreeView_AddChild($hTreeView, $hItem, StringFormat("[%02d] New Child", $y))
Next
Next
_GUICtrlTreeView_EndUpdate($hTreeView)
; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
EndFunc ;==>_Main
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom,$iCode, $tNMHDR, $hWndTreeview
$hWndTreeview = $hTreeView
If Not IsHWnd($hTreeView) Then $hWndTreeview = GUICtrlGetHandle($hTreeView)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndTreeview
Switch $iCode
Case $NM_DBLCLK ; The user has double-clicked the left mouse button within the control
MsgBox(0, '', '鼠标左键双击项目')
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY 回复 7# 水木子
Thanks a lot. 要的就是这种效果. 回复 2# pusofalse
这位大哥可能误会我意思了.
我是想执行数组中的一串命令,不是负值.
我这个a100个元素分别存放了不同的命令。
现在想点击第10个项目就执行$a的命令.
辛苦俩为了!!!!! 回复 9# kxing
$a的值是什么?函数的名称?
在截取到双击的消息时,你如何知道是双击的哪一个树形条目,这个树形条目对应数组中的第几个元素?
当然可以用遍历数组的方法来判断出条目对应的数组元素,但这样做不会牺牲效率吗?
在创建树形条目之初,就用句柄的值作为一个变量,赋值为它所对应的数组序号,不必担心会有混淆,因为“句柄”是唯一的。变量中再套一个变量,在截取到消息时,直接用“‘变量的值’的值”进行操作就好了。或者,楼主的意思我理解错了?#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
GUICreate("test", 400, 300)
$iTree = GUICtrlCreateTreeView(5, 5, 390, 290)
Local $aArray
For $i = 0 To 99
$aArray[$i] = $i
GUICtrlCreateTreeViewItem("Item " & ($i + 1), $iTree)
Assign("." & GUICtrlGetHandle(-1), $i)
Next
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
While GUIGetMsg() <> -3
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
Local $tNMTV, $iCode, $hItem
$tNMTV = DllStructCreate($tagNMTVDISPINFO, $ilParam)
$iCode = DllStructGetData($tNMTV, "Code")
If ($iCode = $NM_DBLCLK) Then
$hItem = _GUICtrlTreeView_GetSelection($iTree)
Msgbox(0, $hItem, Eval("." & $hItem))
EndIf
EndFunc ;==>WM_NOTIFY 嗯嗯,感谢老大的指点.
我应该明白了,十分感谢!!! 新手!学习了!! 再来麻烦下两位高手.
请问这种多条目的item增加context上下文菜单后怎么检测事件呢?
我想了很久都没有很好的办法,用过数组也不行,所以只能来求助两位。
修改了下上面这段代码,麻烦高手看下:
#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
GUICreate("test", 400, 300)
$iTree = GUICtrlCreateTreeView(5, 5, 390, 290)
Local $aArray
For $i = 0 To 99
$aArray[$i] = $i
$Treeitem=GUICtrlCreateTreeViewItem("Item " & ($i + 1), $iTree)
$Context_Main=GUICtrlCreateContextMenu($Treeitem)
GUICtrlCreateMenuItem("关于 Item "&($I+1),$Context_Main)
Assign("." & GUICtrlGetHandle(-1), $i+2)
Next
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
While GUIGetMsg() <> -3
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
Local $tNMTV, $iCode, $hItem
$tNMTV = DllStructCreate($tagNMTVDISPINFO, $ilParam)
$iCode = DllStructGetData($tNMTV, "Code")
If ($iCode = $NM_DBLCLK) Then
$hItem = _GUICtrlTreeView_GetSelection($iTree)
Msgbox(0, $hItem, Eval("." & $hItem))
EndIf
EndFunc ;==>WM_NOTIFY希望能得到你们的帮助,谢谢! Re 13#:
没有必要创建那么多的上下文菜单,与树形条目一一对应。
只需创建一个虚拟菜单(GUICtrlCreateDummy),在截取到右键消息后,修改菜单条目再弹出就可以了。#include <GUIMenu.au3>
#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
GUICreate("test", 400, 300)
$iTree = GUICtrlCreateTreeView(5, 5, 390, 290)
$hTree = GUICtrlGetHandle($iTree)
$iContext = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
$hContext = GUICtrlGetHandle($iContext)
$iMenuItem = GUICtrlCreateMenuItem("About", $iContext)
Local $aArray
For $i = 0 To 99
$aArray[$i] = $i
$Treeitem=GUICtrlCreateTreeViewItem("Item " & ($i + 1), $iTree)
Assign("." & GUICtrlGetHandle(-1), $i+1)
Next
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
While GUIGetMsg() <> -3
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
Local $tNMTV, $iCode, $hItem
$tNMTV = DllStructCreate($tagNMTVDISPINFO, $ilParam)
$iCode = DllStructGetData($tNMTV, "Code")
If ($iCode = $NM_DBLCLK) Then
$hItem = _GUICtrlTreeView_GetSelection($iTree)
Msgbox(0, $hItem, Eval("." & $hItem))
ElseIf ($iCode = $NM_RCLICK) Then
Local $iX, $iY, $aXY, $tXY, $iTest, $iMenuID
; Converts screen coordinate to client coordinate.
$aXY = MouseGetPos()
$tXY = DllStructCreate("long X;long Y")
DllStructSetData($tXY, "X", $aXY)
DllStructSetData($tXY, "Y", $aXY)
_WinAPI_ScreenToClient($hTree, $tXY)
$iX = DllStructGetData($tXY, "X")
$iY = DllStructGetData($tXY, "Y")
$iTest = _GUICtrlTreeView_HitTest($hTree, $iX, $iY)
If ($iTest <> 4) Then Return ; The mouse point is not hovering on an item.
$hItem = _GUICtrlTreeView_HitTestItem($hTree, $iX, $iY)
_GUICtrlTreeView_SelectItem($hTree, $hItem)
$iIndex = Eval("." & $hItem)
GUICtrlSetData($iMenuItem, "About Item " & $iIndex)
$iMenuId = _GUICtrlMenu_TrackPopupMenu($hContext, $hTree, -1, -1, 1, 1, 2)
If ($iMenuId <> 0) Then Msgbox(48, "About", "About Item " & $iIndex)
$tXY = 0
EndIf
EndFunc ;==>WM_NOTIFY 高手就是高手,总会让我得到满意的结果.哈哈
非常感谢哦!!!!
页:
[1]
2