找回密码
 加入
搜索
查看: 3572|回复: 5

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

  [复制链接]
发表于 2009-10-18 11:42:27 | 显示全部楼层 |阅读模式
本帖最后由 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[0][0]
                GUICtrlCreateTreeViewItem($IniSection[$i][0],$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=;同上

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 1金钱 +10 收起 理由
afan + 10 感谢主动将修改帖子分类为[已解决],请继续 ...

查看全部评分

发表于 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
 楼主| 发表于 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[0][0]
                $datez=GUICtrlCreateTreeViewItem($IniSection[$i][0],$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
发表于 2010-6-11 17:51:20 | 显示全部楼层
学习中。。
发表于 2011-1-17 22:48:02 | 显示全部楼层
不错,很好呀
发表于 2011-4-5 17:07:23 | 显示全部楼层
这个不错,但是如果查找这个数值呢
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-10-2 10:24 , Processed in 0.078652 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表