Jump to content

How to capture mouse click on treeview and msgbox param


Recommended Posts

Hello! I speak english just a little, i'm learning AutoIt code!

Here the code:

#include <GUIConstantsEx.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#Region ### START Koda GUI section ### Form=
$Form2 = GUICreate("Form2", 413, 300, 249, 148)
$TreeView = GUICtrlCreateTreeView(56, 40, 209, 209)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $config="set.ini"
;~ POPOLATING INIFILE
IniWrite($config,"Accounts","IDs","1,2,3")
$IDAccounts = StringSplit(IniRead($config,"Accounts","IDs",""),",")
For $i=1 to $IDAccounts[0]
    IniWrite($config,$i,"ID",$i)
    IniWrite($config,$i,"Name",$i&" name")
    IniWrite($config,$i,"Surname",$i&" surname")
Next


PopolateTreeviewDinamically()

While 1
;~  This run the NotifiIT() function each 3 seconds!
    NotifyIT()
    Sleep(3000)
;~  But i need to run the function NotifiIT() only when selection changes.. Please help me!


    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd


func PopolateTreeviewDinamically()

$Overview = _GUICtrlTreeView_Add($TreeView,0,"Overview")
    _GUICtrlTreeView_SetItemParam($TreeView,$Overview,0)
    $IDs = IniRead($config,"Accounts","IDs","")
    If $IDs <> "" Then
        $IDs = StringSplit($IDs,",")
        _GUICtrlTreeView_BeginUpdate($TreeView)

        For $i = 1 to $IDs[0]
            $ID = int(IniRead($config,$IDs[$i],"ID",0))
            $Name = IniRead($config,$IDs[$i],"Name","ERRORE!")
            $Surname = IniRead($config,$IDs[$i],"Surname","ERRORE!")


            $Hello = _GUICtrlTreeView_AddChild($TreeView,$Overview,$Name)
            _GUICtrlTreeView_SetItemParam($TreeView,$Hello,$ID)

        Next
        _GUICtrlTreeView_EndUpdate($TreeView)
        _GUICtrlTreeView_Expand($TreeView)

    EndIf

EndFunc

Func NotifyIT()

$ID = _GUICtrlTreeView_GetItemParam($TreeView)
Msgbox(0,"Name",$ID)

EndFunc

Is there a method to: onselect item of the treeview msgbox for item param?

Thank you so much for tring help me!

[font=courier new, courier, monospace]Una piccola riflessione: [/font] [font=courier new, courier, monospace]Se guardassi la Terra da un pianeta distante 1 anno luce, vedresti la terra 1 anno prima di quell'istante! Quindi se tu potessi arrivare a quella distanza impiegando solo 6 mesi (quindi viaggiando a 2 volte la velocita della luce) vedresti il mondo com'era 6 mesi prima della partenza.. il passato![/font]
Link to comment
Share on other sites

  • Moderators

Giordano,

You need to look for a mouse click on the treeview and then read the Param data like this: :huh2:

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

; Set flag
$fFlag = False

; Create GUI
$hGUI = GUICreate("Test", 500, 500)

$hTreeView = _GUICtrlTreeView_Create($hGUI, 10, 10, 200, 300, BitOR($TVS_HASLINES, $TVS_LINESATROOT, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)

GUISetState()

; Fill Treeview
_GUICtrlTreeView_BeginUpdate($hTreeView)
$iOverview = _GUICtrlTreeView_Add($hTreeView, 0, "Overview")
_GUICtrlTreeView_SetItemParam($hTreeView, $iOverview, 0)

For $i = 1 To 15
    $iHello = _GUICtrlTreeView_AddChild($hTreeView, $iOverview, $i)
    _GUICtrlTreeView_SetItemParam($hTreeView, $iHello, $i)
Next
_GUICtrlTreeView_EndUpdate($hTreeView)
_GUICtrlTreeView_Expand($hTreeView)

; Register message
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

; Loop
While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ; If flag is set
    If $fFlag  Then
        ; Clear flag
        $fFlag = False
        ; Read and display the parameter
        ConsoleWrite(_GUICtrlTreeView_GetItemParam ($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView)) & @CRLF)
    EndIf

WEnd

; Intercept the NOTIFY leassages
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    ; Read the data
    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    ; See if it was out treeview and the required code
    Switch $hWndFrom
        Case $hTreeview
            Switch $iCode
                Case $NM_CLICK
                    ; The user has clicked the left mouse button within the control so set the flag
                    $fFlag = True
                    Return 0
            EndSwitch
    EndSwitch
EndFunc

I hope the comments are clear enough - please ask if not. ;)

If you are not used to coding with GUIRegisterMsg, you might find the GUIRegisterMsg tutorial in the Wiki useful. :alien:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thank so much!

You solved me 2 problems:

The problem posted and the GuiRegisterMsg for what i was looking for! I like so much that function, i want to learn all of it (GuiRegisterMsg)

I was almost arrived to the same solution but i was missing this tutorial!

Thank you so much from italy! :huh2:

[font=courier new, courier, monospace]Una piccola riflessione: [/font] [font=courier new, courier, monospace]Se guardassi la Terra da un pianeta distante 1 anno luce, vedresti la terra 1 anno prima di quell'istante! Quindi se tu potessi arrivare a quella distanza impiegando solo 6 mesi (quindi viaggiando a 2 volte la velocita della luce) vedresti il mondo com'era 6 mesi prima della partenza.. il passato![/font]
Link to comment
Share on other sites

  • Moderators

Giordano,

Glad I could help. ;)

GUIRegisterMsg is a very powerful tool - there are plenty of examples on the forum to help you learn all about it once you have read the tutorial. :huh2:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

This could be useful for who is looking for the same thing:

-ADDED control of the last clicked item! So it write the ID only when the selection change! :huh2:

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

; Set flag
$fFlag = False

; Create GUI
$hGUI = GUICreate("Test", 500, 500)

$hTreeView = _GUICtrlTreeView_Create($hGUI, 10, 10, 200, 300, BitOR($TVS_HASLINES, $TVS_LINESATROOT, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)

GUISetState()

; Fill Treeview
_GUICtrlTreeView_BeginUpdate($hTreeView)
$iOverview = _GUICtrlTreeView_Add($hTreeView, 0, "Overview")
_GUICtrlTreeView_SetItemParam($hTreeView, $iOverview, 0)

For $i = 1 To 15
    $iHello = _GUICtrlTreeView_AddChild($hTreeView, $iOverview, $i)
    _GUICtrlTreeView_SetItemParam($hTreeView, $iHello, $i)
Next
_GUICtrlTreeView_EndUpdate($hTreeView)
_GUICtrlTreeView_Expand($hTreeView)

; Register message
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
$LastClicked = _GUICtrlTreeView_GetItemParam ($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView))
; Loop
While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ; If flag is set
    If $fFlag  Then

        ; Clear flag
        $fFlag = False
        If $LastClicked <> _GUICtrlTreeView_GetItemParam ($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView)) Then
            ; Read and display the parameter
            ConsoleWrite(_GUICtrlTreeView_GetItemParam ($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView)) & @CRLF)
        EndIf

    EndIf

WEnd

; Intercept the NOTIFY leassages
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    ; Read the data
    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    ; See if it was out treeview and the required code
    Switch $hWndFrom
        Case $hTreeview

            Switch $iCode

                Case $NM_CLICK
                    ; The user has clicked the left mouse button within the control so set the flag

                        $fFlag = True
                        $LastClicked = _GUICtrlTreeView_GetItemParam ($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView)) ;ADDED
                        Return 0



            EndSwitch
    EndSwitch
EndFunc
[font=courier new, courier, monospace]Una piccola riflessione: [/font] [font=courier new, courier, monospace]Se guardassi la Terra da un pianeta distante 1 anno luce, vedresti la terra 1 anno prima di quell'istante! Quindi se tu potessi arrivare a quella distanza impiegando solo 6 mesi (quindi viaggiando a 2 volte la velocita della luce) vedresti il mondo com'era 6 mesi prima della partenza.. il passato![/font]
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...