破帽遮颜 发表于 2010-10-12 23:32:52

GUICtrlCreateTreeView 单击后怎么样获取当前项目[已解决]

本帖最后由 破帽遮颜 于 2011-5-11 10:48 编辑

希望高人出手帮忙解决个问题
双击是没有问题的,但单击子项目后怎么样才能获取当前项目?循环的方法就算了。希望使用消息能解决这个问题。感谢。{:face (59):}

下面已解决。该例子没问题了。#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <TreeViewConstants.au3>
#include <StaticConstants.au3>
#Include <GuiListView.au3>
#include <GUIConstantsEx.au3>
#Include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <Constants.au3>

Local $treeview, $generalitem, $displayitem, $aboutitem, $compitem
Local $useritem, $resitem, $otheritem, $startlabel, $aboutlabel, $compinfo
Local $togglebutton, $infobutton, $statebutton, $cancelbutton
Local $msg, $item, $hItem, $text

GUICreate("My GUI with treeview", 350, 215)

$treeview = GUICtrlCreateTreeView(6, 6, 200, 180, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
$generalitem = GUICtrlCreateTreeViewItem("General", $treeview)
GUICtrlSetColor(-1, 0x0000C0)
$displayitem = GUICtrlCreateTreeViewItem("Display", $treeview)
GUICtrlSetColor(-1, 0x0000C0)
$aboutitem = GUICtrlCreateTreeViewItem("About", $generalitem)
$compitem = GUICtrlCreateTreeViewItem("Computer", $generalitem)
$useritem = GUICtrlCreateTreeViewItem("User", $generalitem)
$resitem = GUICtrlCreateTreeViewItem("Resolution", $displayitem)
$otheritem = GUICtrlCreateTreeViewItem("Other", $displayitem)

GUICtrlSetState($generalitem, BitOR($GUI_EXPAND, $GUI_DEFBUTTON)) ; Expand the "General"-item and paint in bold
GUICtrlSetState($displayitem, BitOR($GUI_EXPAND, $GUI_DEFBUTTON)) ; Expand the "Display"-item and paint in bold

GUIRegisterMsg(0x004E, 'MY_WM')
GUISetState()
While 1
        $msg = GUIGetMsg()
        Select
                Case $msg = $GUI_EVENT_CLOSE
                        ExitLoop
        EndSelect
WEnd
GUIDelete()


Func MY_WM($hWnd, $msg, $wParam, $lParam)
        Local $tNMHDR, $hWndFrom, $iIDFrom, $iCode, $tNMTOOLBAR, $iItem
        $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
        $hWndFrom = HWnd(DllStructGetData($tNMHDR, 'hWndFrom'))
        $iIDFrom = DllStructGetData($tNMHDR, 'IDFrom')
        $iCode = DllStructGetData($tNMHDR, 'Code')
        $tNMTOOLBAR = DllStructCreate($tagNMTOOLBAR, $lParam)
        $iItem = DllStructGetData($tNMTOOLBAR, 'iItem')
        Switch $hWndFrom
                Case GUICtrlGetHandle($treeview)
                        If $iCode <> -400 - 51 Then Return
                                $read = GUICtrlRead(GUICtrlRead($treeview), 1)
                                MsgBox(4096, '', $read)
                        EndIf
        EndSwitch
EndFunc   ;==>MY_WM

lpxx 发表于 2010-10-12 23:39:54

这个好像是帮助中例子?

破帽遮颜 发表于 2010-10-12 23:41:33

回复 2# lpxx

是啊~随便改改做个实例给大家。有办法米?

lpxx 发表于 2010-10-12 23:41:49

看了,暂时没有好的思路和那个能力帮你解决,等待能人帮你解决。

破帽遮颜 发表于 2010-10-12 23:46:24

回复 4# lpxx

呵呵~谢谢

republican 发表于 2010-10-13 00:13:41

WM消息是不能获取的(原因我忘了...),所以,目前靠谱的方法是使用SetOnevenet的方法,至少我是这么做的。

pusofalse 发表于 2010-10-13 00:15:19

回复 6# republican


    能把你 用注册WM消息的方法 贴上来看下吗?~

破帽遮颜 发表于 2010-10-13 00:17:41

回复 7# pusofalse

P版能帮忙解决这个问题吗?拜托了。{:face (198):}

破帽遮颜 发表于 2010-10-13 00:22:30

回复 6# republican

有简单的实例看看吗?{:face (59):}

破帽遮颜 发表于 2010-10-13 00:28:16

回复 6# republican

哦,明白了。给每个项目GUICtrlSetOnEvent

pusofalse 发表于 2010-10-13 00:30:31

回复 8# 破帽遮颜


If $iCode = $NM_CLICK ..., 这里不应该判断是否是鼠标单击,鼠标单击这个通知发送得太早,TreeView中的项目的状态和颜色等信息 还没来得及改变,就已经收到了这个NM_CLICK。另外,如果是用上下键选择呢?

要截取的通知应该是在NM_CLICK之后接收到的,仔细看下TreeViewConstants.au3中以TVN_*开头的常量,相信很快就能修正过来了。

破帽遮颜 发表于 2010-10-13 00:44:42

回复 11# pusofalse

谢谢P版~解决了。感谢。
消息上用If $iCode <> -400 - 51 Then Return搞定

3mile 发表于 2010-10-13 09:42:38

回复 1# 破帽遮颜
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <TreeViewConstants.au3>
#include <GuiTreeView.au3>

Local $treeview, $generalitem, $displayitem, $aboutitem, $compitem
Local $useritem, $resitem, $otheritem, $startlabel, $aboutlabel, $compinfo
Local $togglebutton, $infobutton, $statebutton, $cancelbutton
Local $msg, $item, $hItem, $text

GUICreate("My GUI with treeview", 350, 215)

$treeview = GUICtrlCreateTreeView(6, 6, 200, 180, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
$generalitem = GUICtrlCreateTreeViewItem("General", $treeview)
GUICtrlSetColor(-1, 0x0000C0)
$displayitem = GUICtrlCreateTreeViewItem("Display", $treeview)
GUICtrlSetColor(-1, 0x0000C0)
$aboutitem = GUICtrlCreateTreeViewItem("About", $generalitem)
$compitem = GUICtrlCreateTreeViewItem("Computer", $generalitem)
$useritem = GUICtrlCreateTreeViewItem("User", $generalitem)
$resitem = GUICtrlCreateTreeViewItem("Resolution", $displayitem)
$otheritem = GUICtrlCreateTreeViewItem("Other", $displayitem)

GUICtrlSetState($generalitem, BitOR($GUI_EXPAND, $GUI_DEFBUTTON)) ; Expand the "General"-item and paint in bold
GUICtrlSetState($displayitem, BitOR($GUI_EXPAND, $GUI_DEFBUTTON)) ; Expand the "Display"-item and paint in bold

GUIRegisterMsg($WM_NOTIFY, 'MY_WM')
GUISetState()
While 1
        $msg = GUIGetMsg()
        Select
                Case $msg = $GUI_EVENT_CLOSE
                        ExitLoop
        EndSelect
WEnd
GUIDelete()

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 = $TreeView Then
      Switch $iCode
            Case $TVN_SELCHANGEDA, $TVN_SELCHANGEDW
                If GUICtrlRead($TreeView) > 0 Then _
                  MsgBox(0, 0, 'ID: ' & GUICtrlRead($TreeView) & @CRLF & 'Text: ' & GUICtrlRead($TreeView, 1))
      EndSwitch
    EndIf
   
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_OnNotify

liufenglg 发表于 2010-10-13 11:09:55

现在对au3中的常量很头大
常量没解释,看着晕忽忽的

破帽遮颜 发表于 2010-10-13 12:00:25

回复 13# 3mile

呵呵~没分送了~不然送上点金币。
页: [1] 2
查看完整版本: GUICtrlCreateTreeView 单击后怎么样获取当前项目[已解决]