#include <GUIConstantsEx.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GUITreeView.au3>
Local $ini, $treeview, $hTreeView
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
Global $Form1 = GUICreate("获取父项id及文本示例", 216, 358, 525, 139)
Global $treeview = GUICtrlCreateTreeView(8, 8, 201, 337, -1, $WS_EX_CLIENTEDGE)
$hTreeView = GUICtrlGetHandle(-1)
GUISetState(@SW_SHOW)
readinfo("data.ini", $treeview)
Do
Sleep(10)
Until GUIGetMsg() = -3
Func readinfo($ini, $htv, $csname = "class", $hClass = 0)
If Not $hClass Then $hClass = $htv
$aSec = IniReadSection($ini, $csname)
If @error Then Return False
For $i = 1 To $aSec[0][0]
$child = IniReadSection($ini, $aSec[$i][0])
If @error Then;频道
$hItem = GUICtrlCreateTreeViewItem($aSec[$i][0], $hClass)
Else;子类
$hClass1 = GUICtrlCreateTreeViewItem($aSec[$i][0], $hClass)
readinfo($ini, $hClass, $aSec[$i][0], $hClass1)
EndIf;频道或分类
Next
Return True
EndFunc ;==>readinfo
Func WM_NOTIFY($hWndGUI, $MsgID, $WParam, $LParam)
Local $tNMHDR, $IDFrom, $code
Local $iPos_X, $iPos_Y, $uFlags
$tNMHDR = DllStructCreate($tagNMHDR, $LParam)
$IDFrom = DllStructGetData($tNMHDR, "IDFrom")
$code = DllStructGetData($tNMHDR, "Code")
Switch $code
Case $NM_CLICK ; 点击鼠标左键
If $IDFrom = $treeview Then ; 点击 TreeView 控件时,获取点击到的控件句柄
$iPos_X = _WinAPI_GetMousePosX(True, $hTreeView)
$iPos_Y = _WinAPI_GetMousePosY(True, $hTreeView)
$uFlags = _GUICtrlTreeView_HitTest($hTreeView, $iPos_X, $iPos_Y)
If BitAND($uFlags, $TVHT_ONITEM) Then
Local $hItem, $hParent, $sText_I, $sText_P = '无父项目'
$hItem = _GUICtrlTreeView_HitTestItem($hTreeView, $iPos_X, $iPos_Y)
MsgBox(0, '', _GUICtrlTreeView_GetTree($hTreeView, $hItem))
$sText_I = _GUICtrlTreeView_GetText($treeview, $hItem)
$hParent = _GUICtrlTreeView_GetParentHandle($hTreeView, $hItem)
If $hParent Then $sText_P = _GUICtrlTreeView_GetText($treeview, $hParent)
MsgBox(0, '父项目:' & $sText_P, '子项目:' & $sText_I)
EndIf
EndIf
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY