jycel 发表于 2009-10-18 11:42:27

[已解决]Treeview中怎么读取所选择项目的值?

本帖最后由 jycel 于 2009-10-18 15:52 编辑

解决方法:三楼
第一次使用树型结构,帮助中都代有选择和图片,用不上!
问题如下图:当我点击历史图片下面的所有内容时,读取所选择的值即可!
功能就是读取所选择项目,然后更改对应的图片显示至图片框


#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
Local $_INI_File = @ScriptDir & "\Image.ini",$TreeView1;读取配置文件
#Region ### START Koda GUI section ### Form=C:\Documents and Settings\Administrator\桌面\Form1.kxf
$Form1 = GUICreate("图片查看器", 587, 373, 216, 186)
$Group1 = GUICtrlCreateGroup("", 2, 0, 468, 369)
$Pic1 = GUICtrlCreatePic(@ScriptDir&"\1.jpg", 8, 8, 457, 353)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("", 472, 0, 113, 369)
$TreeView1 = GUICtrlCreateTreeView(480, 14, 97, 345)
$IniSection = IniReadSection($_INI_File, "历史");读取配置文件主值
$item=GUICtrlCreateTreeViewItem("历史图片", $TreeView1)
if not @error Then
        For $i = 1 To $IniSection
                GUICtrlCreateTreeViewItem($IniSection[$i],$item)
        Next
EndIf
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
               
        EndSwitch
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
        #forceref $hWnd, $iMsg, $iwParam
        Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
       
        $hWndTreeview = $TreeView1
        If Not IsHWnd($TreeView1) Then $hWndTreeview = GUICtrlGetHandle($TreeView1)

        $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_CLICK ; The user has clicked the left mouse button within the control
                                        $xz=_GUICtrlTreeView_GetCount($TreeView1);读取共有多少列
                                        MsgBox(0,"","你当前选择的是:"&$xz)
                                Case $NM_DBLCLK ; The user has double-clicked the left mouse button within the control

                                Case $NM_RCLICK ; The user has clicked the right mouse button within the control

                                Case $NM_RDBLCLK ; The user has clicked the right mouse button within the control
                        EndSwitch
        EndSwitch
        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

image.ini配置如下
[历史]
9234=;值为关键字.jpg
8234=;同上

newx 发表于 2009-10-18 13:45:52

给你一个事例,自己参考一下
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("读取所选择项目的值 事例", 382, 139, 214, 150)
GUISetOnEvent($GUI_EVENT_CLOSE, "Event")
$ListView1 = GUICtrlCreateListView("读取所选择项目的值", 40, 24, 305, 89)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 300)
$ListView1_0 = GUICtrlCreateListViewItem("这是第一行", $ListView1)
GUICtrlSetOnEvent($ListView1_0, "Event")
$ListView1_1 = GUICtrlCreateListViewItem("这是第二行", $ListView1)
GUICtrlSetOnEvent(-1, "Event")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
   Sleep(100)
Wend

Func Event()
   Switch @GUI_CtrlId
      Case $GUI_EVENT_CLOSE
         Exit
      Case Else
         $Val = GUICtrlRead(@GUI_CtrlId,1)
         $Val = StringReplace ( $Val, "|","")
         msgbox(0,0,"你点中的文字是“" & $Val & "”")
   EndSwitch
EndFunc

jycel 发表于 2009-10-18 14:00:22

本帖最后由 jycel 于 2009-10-18 15:11 编辑

OK解决了!应该还有其它方法!下来慢慢研究!
代码如下:

#NoTrayIcon
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
Local $_INI_File = @ScriptDir & "\Image.ini";读取配置文件
if not FileExists($_INI_File) then
msgbox(16,"错误提示","未找到Image.ini配置文件"&@CRLF&"请手动建立"&@CRLF&"字段名为[历史]",5)
Exit
EndIf
#Region ### START Koda GUI section ### Form=C:\Documents and Settings\Administrator\桌面\Form1.kxf
$Form1 = GUICreate("图片查看器", 587, 373, 216, 186)
GUISetOnEvent($GUI_EVENT_CLOSE, "Event")
$Group1 = GUICtrlCreateGroup("", 2, 0, 468, 369)
$Pic1 = GUICtrlCreatePic("", 8, 8, 457, 353)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("", 472, 0, 113, 369)
$TreeView1 = GUICtrlCreateTreeView(480, 14, 97, 321)
GUICtrlSetOnEvent(-1, "Event")
$Button1 = GUICtrlCreateButton("退出查看器", 480, 344, 97, 20, -1, $WS_EX_STATICEDGE)
GUICtrlSetOnEvent(-1, "Event")
$item=GUICtrlCreateTreeViewItem("历史图片", $TreeView1)
$IniSection = IniReadSection($_INI_File, "历史");读取配置文件主值
if not @error Then
        For $i = 1 To $IniSection
                $datez=GUICtrlCreateTreeViewItem($IniSection[$i],$item)
                GUICtrlSetOnEvent(-1, "Event")
        Next
EndIf

GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
Sleep(100)
WEnd
Func Event()
   Switch @GUI_CtrlId
      Case $GUI_EVENT_CLOSE
         Exit
          case $Button1
                        Exit
      Case Else
         $Val = GUICtrlRead(@GUI_CtrlId,1)
         $Val = StringReplace ( $Val, "|","")
               GUICtrlSetImage($Pic1,IniRead($_INI_File,"历史",$Val,""))
   EndSwitch
EndFunc

judyzhai 发表于 2010-6-11 17:51:20

学习中。。

neity 发表于 2011-1-17 22:48:02

不错,很好呀

731106 发表于 2011-4-5 17:07:23

这个不错,但是如果查找这个数值呢
页: [1]
查看完整版本: [已解决]Treeview中怎么读取所选择项目的值?