cashiba 发表于 2018-8-28 13:24:21

_WinAPI_GetDlgCtrlID函数转的ID为什么不对呢?[已解决]

本帖最后由 cashiba 于 2018-8-28 21:36 编辑

#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>

Global $a_hTreeView,$b_iTreeView

Example()

Func Example()
    Local $hGUI, $hItem
    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
    $hGUI = GUICreate("(UDF 创建) TreeView", 400, 300)

    $a_hTreeView = _GUICtrlTreeView_Create($hGUI, 2, 2, 180, 268, $iStyle, $WS_EX_CLIENTEDGE)
      ConsoleWrite('$a_hTreeView=' & $a_hTreeView & @CRLF & '$a_idTreeview=' & _WinAPI_GetDlgCtrlID($a_hTreeView) & @CRLF & @CRLF)

      $b_iTreeView =GUICtrlCreateTreeView(200, 2,180, 268, $iStyle, $WS_EX_CLIENTEDGE)
         ConsoleWrite('$b_hTreeView=' & GUICtrlGetHandle($b_iTreeView) & @CRLF & '$b_idTreeview=' & $b_iTreeView & @CRLF)

    GUISetState(@SW_SHOW)

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>Example如上,使用UDF创建树图,返回的是句柄;使用内置函数创建树图,返回id
有时需要句柄和id互转
结果如下:
$a_hTreeView=0x002C0604
$a_idTreeview=10000

$b_hTreeView=0x002D04B6
$b_idTreeview=3UDF创建的树图句柄利用 _WinAPI_GetDlgCtrlID函数转的ID好像不对
是不是还有其它函数或方法呢?


afan 发表于 2018-8-28 13:37:51

UDF创建控件返回的句柄,一般都是没有控件ID的

cashiba 发表于 2018-8-28 14:01:20

本帖最后由 cashiba 于 2018-8-28 14:19 编辑

afan 发表于 2018-8-28 13:37
UDF创建控件返回的句柄,一般都是没有控件ID的
原来如此,还以为函数用得不对。
今天第一次用_GUICtrlTreeView_Create函数,看例子后修改了一下(如下),试试获取当前点击的树项文本,结果总是不对。以为是树图的句柄和id的问题......
A大有空帮看看,问题出在哪里呢?

#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
Global $g_hTreeView
Example()
Func Example()
Local $hGUI, $hItem
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
$hGUI = GUICreate("(UDF 创建) TreeView", 400, 300)
$g_hTreeView = _GUICtrlTreeView_Create($hGUI, 2, 2, 396, 268, $iStyle, $WS_EX_CLIENTEDGE)
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
_GUICtrlTreeView_BeginUpdate($g_hTreeView)
For $x = 1 To Random(2, 10, 1)
$hItem = _GUICtrlTreeView_Add($g_hTreeView, 0, StringFormat("[%02d] 新主项", $x))
For $y = 1 To Random(2, 10, 1)
   _GUICtrlTreeView_AddChild($g_hTreeView, $hItem, StringFormat("[%02d] 新子项", $y))
Next
Next
_GUICtrlTreeView_EndUpdate($g_hTreeView)
; 循环到用户退出.
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
EndFunc   ;==>Example
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $wParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
$hWndTreeview = $g_hTreeView
If Not IsHWnd($g_hTreeView) Then $hWndTreeview = GUICtrlGetHandle($g_hTreeView)
$tNMHDR = DllStructCreate($tagNMHDR, $lParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndTreeview
   Switch $iCode
    Case $NM_CLICK
   Local $iItem = GUICtrlRead($hWndTreeview)
   Local $sItem = _GUICtrlTreeView_GetText($hWndTreeview, GUICtrlGetHandle($iItem))
   ConsoleWrite('1、$iItem=' & $iItem & ',$sItem=' & $sItem & @CRLF & @CRLF)
    Case $TVN_SELCHANGEDW
   Local $iItem = GUICtrlRead($hWndTreeview)
   Local $sItem = _GUICtrlTreeView_GetText($hWndTreeview, GUICtrlGetHandle($iItem))
   ConsoleWrite('2、$iItem=' & $iItem & ',$sItem=' & $sItem & @CRLF & @CRLF)
   EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc

afan 发表于 2018-8-28 19:46:53

cashiba 发表于 2018-8-28 14:01
原来如此,还以为函数用得不对。
今天第一次用_GUICtrlTreeView_Create函数,看例子后修改了一下(如下 ...

我没用过 treeview,你可以试试
_GUICtrlTreeView_GetText($hWndTreeview, _GUICtrlTreeView_GetSelection($hWndTreeview))
但不一定是在 $NM_CLICK 时使用

cashiba 发表于 2018-8-28 21:21:34

afan 发表于 2018-8-28 19:46
我没用过 treeview,你可以试试
_GUICtrlTreeView_GetText($hWndTreeview, _GUICtrlTreeView_GetSelecti ...

谢谢A大!
_GUICtrlTreeView_GetSelection,这个函数你不把它单独拎出来,我看一眼也没什么印象,不知道用在哪里.......
_GUICtrlTreeView_Create和GUICtrlCreateTreeView这两个建树的函数,是走的两个路子?不熟的话,混用起来估计要把自己绕晕
{:face (189):}
页: [1]
查看完整版本: _WinAPI_GetDlgCtrlID函数转的ID为什么不对呢?[已解决]