#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <TreeViewConstants.au3>
#include <GuiTreeView.au3>
#include <GuiMenu.au3>
#include <GuiImageList.au3>
#include <Array.au3>
#Region ### START Koda GUI section ### Form=
Local $hItem[6], $hImage, $tRect, $hTreeView
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
$Form1 = GUICreate("Form1", 640, 450)
$TreeView1 = GUICtrlCreateTreeView(16, 24, 297, 329, $iStyle, $WS_EX_CLIENTEDGE)
$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)
Global Enum $MenuAdd = 1000, $MenuEdit, $MenuDel
$Menu1 = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($Menu1, 0, " 添加数据 ", $MenuAdd)
_GUICtrlMenu_InsertMenuItem($Menu1, 1, " 修改数据 ", $MenuEdit)
_GUICtrlMenu_InsertMenuItem($Menu1, 2, " 删除数据 ", $MenuDel)
GUIRegisterMsg($WM_NOTIFY, 'MY_WM') ;注册事件
GUISetState(@SW_SHOW)
$Debug_TV = False
; 检查传递给函数的类名, 设置为真并使用另一控件的句柄可以看出它是否有效
;//设置图标
$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)
;//增加节点
For $x = 0 To _GUIImageList_GetImageCount($hImage) - 1
$hItem[$x] = _GUICtrlTreeView_Add($TreeView1, 0, StringFormat("[%02d] New Item", $x + 1), $x, $x)
Next
;//得到项目的坐标
$tRect = _GUICtrlTreeView_DisplayRectEx($hTreeView, $hItem[2])
#EndRegion ### END Koda GUI section ###
Dim $text, $tRect
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
;MsgBox(4160, "信息", )
Case $Button2
Case $Button3
Case $Button4
Case $Button5
Case $Button6
EndSwitch
WEnd
#cs
OnSelchangedTREEMachineInfo(NMHDR* pNMHDR, LRESULT* pResult)
树形控件消息映射使用ON_NOTIFY宏,有两个消息响应函数,这两个消息具备完全一样的入口参数(NMHDR结构体 指针及long型指针),
但完全是两种不同的效果,所用结构体都为(NMHDR)。
用错了不会有正确的结果。
而TVN_SELCHANGED是我们所用。一般都要求选中后都改变
TVN_SELCHANGE: 该消息为当前页改变(后)发送。
TVN_SELCHANGING:在当前页改变(时)发送,可以通过返回TRUE来禁止页面的改变,即不改变,我们看到的都是上一次的结果而非当前选中的结果。
#ce
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')
Local $text, $oCtlID, $oCtlData
If $iIDFrom = $TreeView1 Then
Switch $iCode ;根据事件ID处理
Case $NM_CLICK ; 左击
;
$a = GUIGetCursorInfo()
GUICtrlSetData( $Input1, StringFormat("鼠标:[X:%.f,Y:%.f] [左键:%u 右键:%u] 控件[%u = %u]",$a[0],$a[1],$a[2],$a[3],$a[4],$iIDFrom) )
#cs
$array[0] = X 坐标(水平轴)
$array[1] = Y 坐标(垂直轴)
$array[2] = 鼠标左键被按下 (1 为按下, 0 为未按下)
$array[3] = 鼠标右键被按下 (1 未按下, 0 为未按下)
$array[4] = 鼠标下面的控件的控件ID( 0 为没有或者无法获取)
#ce
Case $NM_DBLCLK ; 双击
;GUICtrlSetData($Input1,"双击")
Case $NM_RCLICK ; 右击
;GUICtrlSetData($Input1,"右键")
$Index = _GUICtrlTreeView_GetSelection($iIDFrom)
;If Not $Index Then Return ; 这里用以判断是否选定了TreeViewItem
If Not $Index Then Return; 这里用以判断是否选定了TreeViewItem
;//在指定区域显示一个快捷菜单(弹出菜单)
;右键菜单
;$Menu1 = GUICtrlCreateContextMenu($TreeView1)
;$rclk = GUICtrlCreateMenuItem("萌萌的菜单", $Menu1)
$hhW = _GUICtrlTreeView_GetItemHandle($hWnd , $Index)
;GUICtrlSetData($Input1,"单击" & GUICtrlRead($iIDFrom, 1))
$oCtlID = GUICtrlRead($iIDFrom, 0)
$oCtlData = GUICtrlRead($iIDFrom, 1)
$tRect = _GUICtrlTreeView_DisplayRectEx($iIDFrom, $hhW)
$text = StringFormat("项目: [Left:%d, Top:%d, Right: %d, Button:%s = %u]", DllStructGetData($tRect, "Left"), DllStructGetData($tRect, "Top"), DllStructGetData($tRect, "Right"), DllStructGetData($tRect, "Bottom"), $Index)
GUICtrlSetData($Label1,$text)
if DllStructGetData($tRect, "Bottom")>0 Then
;//在指定区域显示一个快捷菜单(弹出菜单)
$SelectedID = _GUICtrlMenu_TrackPopupMenu($Menu1, $hWndFrom, -1, -1, 1, 1, 2)
Switch $SelectedID ;根据所选菜单项执行功能
Case $MenuAdd
MsgBox(48, "", "您点击了添加", 0, $hWndFrom)
Case $MenuEdit
MsgBox(48, "", "您点击了编辑", 0, $hWndFrom)
Case $MenuDel
MsgBox(48, "", "您点击了删除", 0, $hWndFrom)
EndSwitch
Endif
Case $TVN_SELCHANGEDA, $TVN_SELCHANGEDW
; $array[0] = X 坐标(水平轴)
; $array[1] = Y 坐标(垂直轴)
; $array[2] = 鼠标左键被按下 (1 为按下, 0 为未按下)
; $array[3] = 鼠标右键被按下 (1 未按下, 0 为未按下)
; $array[4] = 鼠标下面的控件的控件ID( 0 为没有或者无法获取)
#cs
If GUICtrlRead($iIDFrom) > 0 Then
;GUICtrlRead ( 控件ID [, 高级] )
;0 = (默认) 返回控件的句柄或ID
;1 = 返回控件的扩展信息 (参考 注意 部分).
$oCtlID = GUICtrlRead($iIDFrom, 0)
$oCtlData = GUICtrlRead($iIDFrom,1)
if _GUICtrlTreeView_GetChildren($iIDFrom, $oCtlID) Then
;有子项
$text = StringFormat(" 控件ID: [%s] \r\n 文本: [%s] \r\n 子项:[有子项]", GUICtrlRead($TreeView,0), GUICtrlRead($TreeView,1) )
Else
;没有子项(最底层节点)
$text = StringFormat(" 控件ID: [%s] \r\n 文本: [%s] \r\n 子项:[无]", GUICtrlRead($TreeView,0), GUICtrlRead($TreeView,1) )
;MsgBox(48,"单击提示","选择了" & GUICtrlRead($treeview, 1), 0, $Manage)
Endif
$tRect = _GUICtrlTreeView_DisplayRectEx($iIDFrom, $oCtlID)
$text = StringFormat("项目: [Left:%d, Top:%d, Right: %d, Button:%d]", DllStructGetData($tRect, "Left"), DllStructGetData($tRect, "Top"), DllStructGetData($tRect, "Right"), DllStructGetData($tRect, "Bottom"))
;StringFormat("鼠标:[X:%.f,Y:%.f] [左键:%u 右键:%u] 控件[%u]",$a[0],$a[1],$a[2],$a[3],$a[4])
GUICtrlSetData($Label1,$text)
;GUICtrlSetData ($Edit1, $text )
;MsgBox(0, 0, 'ID: ' & GUICtrlRead($TreeView,0) & @CRLF & 'Text: ' & GUICtrlRead($TreeView, 1))
Endif
#ce
EndSwitch
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>_OnNotify