#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <GuiTreeView.au3>
#include <GuiMenu.au3>
#include <Misc.au3>
Global $hTreeCurValue
Local $Form1 = GUICreate('Form1', 640, 450)
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
Local $TreeView1 = GUICtrlCreateTreeView(16, 24, 297, 329, $iStyle, $WS_EX_CLIENTEDGE)
Local $hTreeView = GUICtrlGetHandle(-1)
$Input1 = GUICtrlCreateInput('Input1', 16, 376, 585, 21)
$Label1 = GUICtrlCreateLabel('Label1', 16, 408, 588, 17)
$Button1 = GUICtrlCreateButton('Button1', 368, 24, 75, 25)
$Button2 = GUICtrlCreateButton('Button2', 368, 58, 75, 25)
$Button3 = GUICtrlCreateButton('Button3', 368, 91, 75, 25)
$Button4 = GUICtrlCreateButton('Button4', 368, 125, 75, 25)
$Button5 = GUICtrlCreateButton('Button5', 368, 158, 75, 25)
$Button6 = GUICtrlCreateButton('Button6', 368, 192, 75, 25)
#cs
Local Enum $MenuAdd = 1000, $MenuEdit, $MenuDel
Local $Menu1 = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($Menu1, 0, ' 添加数据 ', $MenuAdd)
_GUICtrlMenu_InsertMenuItem($Menu1, 1, ' 修改数据 ', $MenuEdit)
_GUICtrlMenu_InsertMenuItem($Menu1, 2, ' 删除数据 ', $MenuDel)
#ce
Global $aMenu = StringSplit(" 添加本级节点 | 删除此节点 | 修改节点名称 || 添加子节点 | 删除所有子节点", "|",1)
$iMenu = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
$hMenu = GUICtrlGetHandle($iMenu) ;GUICtrlGetHandle ( 控件ID )
For $i = 1 to $aMenu[0]
$aMenu[$i] = GUICtrlCreateMenuItem($aMenu[$i], $iMenu)
;Assign("iMenuItem" & $i, GUICtrlCreateMenuItem($aMenu[$i], $iMenu), 2 )
;msgbox(48,0, Eval("iMenuItem" & $i),0,$Form1)
next
GUIRegisterMsg($WM_NOTIFY, '_MY_WM') ;注册事件
GUISetState()
$hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 110)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 131)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 165)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 168)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 137)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 146)
_GUICtrlTreeView_SetNormalImageList($hTreeView, $hImage)
;Local $hItem[6]
Global $hItem = StringSplit(" 北京市 | 天津市 | 上海市 | 贵州省 | 云南省 | 四川省 ", "|",2)
For $x = 0 To Ubound($hItem)-1 ;_GUIImageList_GetImageCount($hImage) - 1
;$hItem[$x] = _GUICtrlTreeView_Add($TreeView1, 0, StringFormat('[%02d] New Item', $x + 1), $x, $x)
$hItem[$x] = _GUICtrlTreeView_Add($TreeView1, 0, $hItem[$x], $x, $x)
Next
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
EndSwitch
WEnd
Func _MY_WM($hWnd, $Msg, $wParam, $lParam)
Local $tNMHdr = DllStructCreate($tagNMHDR, $lParam), $tNM_TREEVIEW
Local $hWndFrom = DllStructGetData($tNMHdr, 'hWndFrom')
Local $iIDFrom = DllStructGetData($tNMHdr, 'IDFrom')
Local $iCode = DllStructGetData($tNMHdr, 'Code')
If $iIDFrom = $TreeView1 Then
Switch $iCode ;根据事件ID处理
Case $TVN_SELCHANGINGA, $TVN_SELCHANGINGW ;//某个项目正在发生变化
GUICtrlSetData( $Label1,"项目正在变化!" & $hTreeCurValue)
Case $TVN_SELCHANGEDA, $TVN_SELCHANGEDW ;//某个项已经发生变化
$Index = _GUICtrlTreeView_GetSelection($iIDFrom)
$hTreeCurValue = _GUICtrlTreeView_GetText($hTreeView , $Index)
GUICtrlSetData( $Input1,"项目变化!" & $hTreeCurValue)
Case $TVN_KEYDOWN
if Not _GUICtrlTreeView_GetEditControl($hTreeView) = 0 Then
;如果正在编辑,则退出
Return $GUI_RUNDEFMSG
Endif
;取按键值
$tInfo = DllStructCreate($tagNMLVKEYDOWN, $LParam) ;$ilParam)
$psx = DllStructGetData($tInfo, "VKey")
Switch $psx
Case 113 ;F2键 - 开始编辑
;GUICtrlSetData( $Input1,"按键" & $psx)
$Index = _GUICtrlTreeView_GetSelection($iIDFrom)
_GUICtrlTreeView_EditText($hTreeView, $Index)
Case 13
GUICtrlSetData( $Input1,"按键" & $psx)
Case Else
EndSwitch
;_GUICtrlTreeView_EndEdit($hTreeView)
;Endif
Case $TVN_ENDLABELEDITA, $TVN_ENDLABELEDITW ; 文本编辑结束
;GUICtrlSendMsg($TreeView, $TVM_ENDEDITLABELNOW, 0, 0) ;与下行作用相同, 终止编辑
_GUICtrlTreeView_EndEdit($hTreeView, False)
$Index = _GUICtrlTreeView_GetSelection($iIDFrom)
$hNewValue = _GUICtrlTreeView_GetText($hTreeView , $Index)
;$new_Name = ControlGetText($hTreeView, '', $hEditControl)
GUICtrlSetData( $Input1,StringFormat("编辑结束,将[%s]改为[%s]",$hTreeCurValue, $hNewValue) )
Case $TVN_BEGINLABELEDITA, $TVN_BEGINLABELEDITW ; 开始文本编辑
;$hItem = _GUICtrlTreeView_GetEditControl($hTreeView)
;$hIndex = _GUICtrlTreeView_Index($hTreeView, $hItem)
;$hText = _GUICtrlTreeView_GetText($hTreeView , $hIndex)
GUICtrlSetData( $Input1,"编辑开始了[" & $hTreeCurValue & "]" )
;$dll = DllOpen("user32.dll")
;While 1
; Sleep ( 250 )
; If _IsPressed("0D", $dll) Then ;检测是否回车键
; _GUICtrlTreeView_EndEdit($hTreeView, True)
; ;MsgBox(0,"检查按键", "点击 End 键")
; ExitLoop
; EndIf
;WEnd
;DllClose($dll)
Case $NM_CLICK ; 左击
;单击时记录值
;$Index = _GUICtrlTreeView_GetSelection($iIDFrom)
;$hTreeCurValue = _GUICtrlTreeView_GetText($hTreeView , $Index)
Case $NM_DBLCLK ; 双击
Case $NM_RCLICK ; 右击
;//这里用以判断是否选定了TreeViewItem
$Index = _GUICtrlTreeView_GetSelection($iIDFrom)
If Not $Index Then Return $GUI_RUNDEFMSG
#cs
;//MouseGetPos()返回鼠标坐标的2元素一维数组array: $array[0] = X 坐标 (横向), $array[1] = Y 坐标 (垂直)
Local $aPos = MouseGetPos()
;//C/C++ 样式的数据结构供 DllCall 使用.
Local $tag = DllStructCreate("long X;long Y")
DllStructSetData($tag, 'X', $aPos[0])
DllStructSetData($tag, 'Y', $aPos[1])
;//转换屏幕指定点坐标到客户端屏幕坐标
_WinAPI_ScreenToClient($hTreeView, $tag)
Local $iX = DllStructGetData($tag, 'X')
Local $iY = DllStructGetData($tag, 'Y')
$tag = 0 ;//
Local $iX = _WinAPI_GetMousePosX(True, $hTreeView)
Local $iY = _WinAPI_GetMousePosY(True, $hTreeView)
#ce
;//$tagPOINT = "struct; long X;long Y; endstruct"
Local $tag = _WinAPI_GetMousePos(True, $hTreeView)
Local $iX = DllStructGetData($tag, 'X')
Local $iY = DllStructGetData($tag, 'Y')
;StringFormat('鼠标:[X:%.f,Y:%.f] [左键:%u 右键:%u] 控件[%u = %u]', $a[0], $a[1], $a[2], $a[3], $a[4], $iIDFrom)
;//检索测试点位置相对于控件的信息,返回结果代码.
Local $iPos = _GUICtrlTreeView_HitTest($hTreeView, $iX, $iY)
If ($iPos <> 2) And ($iPos <> 4) And ($iPos <> 64) Then Return $GUI_RUNDEFMSG
;1 - 在客户区末项的下面.
;8 - 位于项目缩进
;16 - 位于项目按钮
;32 - 位于项目右边
;2 - 位于项目位图 -通过
;4 - 位于项目文本 -通过
;64 - 位于项目用户自定义状态图标 -通过
;128 - 位于客户区上方
;256 - 位于客户区下方
;512 - 位于客户区左边
;1024 - 位于客户区右边
;//检索指定坐标的项目,如果不是当前选定的项目,则退出
Local $hItem = _GUICtrlTreeView_HitTestItem($hTreeView, $iX, $iY)
if $Index <> $hItem Then Return $GUI_RUNDEFMSG
;//选定项目,弹出菜单
;_GUICtrlTreeView_SelectItem($hTreeView, $hItem)
Local $iMenuId = _GUICtrlMenu_TrackPopupMenu($hMenu, $hWndFrom, -1, -1, 1, 1, 2)
Switch $iMenuId
Case 0 ;没有选择或错误
Case $aMenu[1] ;//添加本级节点
Case $aMenu[2] ;//删除此节点
_GUICtrlTreeView_Delete($hTreeView, $hItem)
Case $aMenu[3] ;//修改节点
_GUICtrlTreeView_EditText($hTreeView, $hItem)
Case $aMenu[5] ;//添加子节点
$jb = _GUICtrlTreeView_Level($hTreeView, $hItem)
if $jb< 2 Then
if Not _GUICtrlTreeView_GetChildren($hTreeView, $hItem) Then
_GUICtrlTreeView_SetChildren($hTreeView, $hItem , True)
Endif
$subitem = _GUICtrlTreeView_AddChild($hTreeView, $hItem, "新建节点")
;_GUICtrlTreeView_Expand($hTreeView, $hItem, True)
_GUICtrlTreeView_SelectItem($hTreeView, $subitem , $TVGN_CARET)
_GUICtrlTreeView_EditText($hTreeView, $subitem)
Endif
Case $aMenu[6] ;//删除所有子节点
_GUICtrlTreeView_DeleteChildren($hTreeView, $hItem)
_GUICtrlTreeView_SetChildren($hTreeView, $hItem , False)
Case Else ;其他
MsgBox( 48, $iMenuId , _GUICtrlMenu_GetItemText($hMenu,$iMenuId, False), 0, $Form1 )
EndSwitch
#cs
Switch _GUICtrlMenu_TrackPopupMenu($Menu1, $hWndFrom, -1, -1, 1, 1, 2)
Case $MenuAdd
MsgBox(48, '', '您点击了添加', 0, $hWndFrom)
Case $MenuEdit
$hTreeCurValue = _GUICtrlTreeView_GetText($hTreeView , $Index)
GUICtrlSetData( $Input1,"编辑开始了[" & $hTreeCurValue & "]" )
_GUICtrlTreeView_EditText($hTreeView, $hItem) ;
;MsgBox(48, '', '您点击了编辑', 0, $hWndFrom)
Case $MenuDel
MsgBox(48, '', '您点击了删除', 0, $hWndFrom)
EndSwitch
#ce
EndSwitch
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>_MY_WM