Jump to content
Sign in to follow this  
dexto

GUICtrlTreeView Right Click Event

Recommended Posts

dexto

Trying to display a right click menu for an item in the Tree View.

Is there a way to detect what item in the TreeView was right clicked?

I am using GUIOnEventMode and closest I got to the goal was using:

GUISetOnEvent($GUI_EVENT_SECONDARYDOWN, "rclick")

in combination with

_GUICtrlTreeView_HitTestItem()

It didn't work since _GUICtrlTreeView_HitTestItem returned 0x000000

Thank you for any tips or just tell me that this is not going to work.

Share this post


Link to post
Share on other sites
seandisanti

Here ya go

***edit*** removed unnecessary quote

Edited by cameronsdad

Share this post


Link to post
Share on other sites
dexto

Here ya go

***edit*** removed unnecessary quote

Thanks for looking into this. I did try it that way. With that method there is no way to find out what item was clicked unfortunately.

Share this post


Link to post
Share on other sites
seandisanti

Thanks for looking into this. I did try it that way. With that method there is no way to find out what item was clicked unfortunately.

kind of seems like it does...

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <TreeViewConstants.au3>
#Include <GuiTreeView.au3> ; only needed if you want to use _GUICtrlTreeView_GetText

$mainform = GUICreate("TreeView", 400, 300)

$TreeView = GUICtrlCreateTreeView(10, 10, 380, 280, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_Checkboxes), $WS_EX_CLIENTEDGE)
$var = GUICtrlCreateContextMenu($TreeView)
$preview = GUICtrlCreateMenuItem("Preview", $var)

Dim $myArray[10][2]

For $i = 1 To UBound($myArray)-1

    $Char = Chr(64+$i)

    $myArray[$i][0] = "This element contains " & $Char
    $myArray[$i][1] = GUICtrlCreateTreeViewItem($Char, $TreeView)

Next

GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()

    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $preview
            MsgBox (0, "Preview", "Handle: " & GUICtrlRead ($TreeView) & @CRLF & "Item: " & _GUICtrlTreeView_GetText ($TreeView, GUICtrlRead ($TreeView)))
    EndSwitch

WEnd
Edited by cameronsdad

Share this post


Link to post
Share on other sites
exodius

How about something like this?

#include <Misc.au3>
#include <GUIConstants.au3>
#Include <GuiTreeView.au3>
#include <TreeviewConstants.au3>
#include <WindowsConstants.au3>
#Include <WinAPI.au3>

Opt ("GuiOnEventMode", 1)

$guiPrinterTool = GUICreate("Printer Tool", 350, 216, Default, Default, BitOR ($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU, $WS_SIZEBOX))
GUISetOnEvent($GUI_EVENT_CLOSE, "_GUI_Event_Close")

$tviewPI = GUICtrlCreateTreeView(160, 26, 329, 185, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP), $WS_EX_CLIENTEDGE)
$tviewPICon = GUICtrlCreateContextMenu($tviewPI)
$tviewPIConInstallAPrinterOn = GUICtrlCreateMenuItem("Install a Printer", $tviewPICon)
$tviewPIConchangePrinterName = GUICtrlCreateMenuItem("Change Printer Name", $tviewPICon)
$tviewPIConDeletePrinter = GUICtrlCreateMenuItem("Delete Printer", $tviewPICon)
$tviewPIConSendTestPage = GUICtrlCreateMenuItem("Send Test Page", $tviewPICon)
$tviewPIConSetAsDefault = GUICtrlCreateMenuItem("Set as Default", $tviewPICon)
GUICtrlCreateMenuItem("", $tviewPICon)
$tviewPIConchangePrinterPort = GUICtrlCreateMenuItem("Change Printer Port", $tviewPICon)
GUICtrlCreateMenuItem("", $tviewPICon)
$tviewPIConRemoveItem = GUICtrlCreateMenuItem("Remove computer from list", $tviewPICon)
$tviewPIConRemoveAllItems = GUICtrlCreateMenuItem("Remove all computers from list", $tviewPICon)


GUICtrlCreateTreeViewItem("Test", $tviewPI)
$vTest2 = GUICtrlCreateTreeViewItem("Test2", $tviewPI)
$vTest3 = GUICtrlCreateTreeViewItem("Test3", $vTest2)
GUICtrlCreateTreeViewItem("Test4", $vTest3)

GUISetState()


GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    Sleep (100)
WEnd


Func _GUI_EVENT_CLOSE()
    Exit
EndFunc


Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
;~  ConsoleWrite('@@ (' & @ScriptLineNumber & ') :(' & @MIN & ':' & @SEC & ') WM_NOTIFY()' & @CR) ;### Function Trace
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeView, $tInfo
    $hWndTreeView = $tviewPI
    If Not IsHWnd($tviewPI) Then $hWndTreeView = GUICtrlGetHandle($tviewPI)

    $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_RCLICK
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                    Local $tPoint, $tTVHEx, $TVhItem, $mX, $mY
                    $tPoint = _WinAPI_GetMousePos(True, $hWndTreeView)
                    $mX = DllStructGetData($tPoint, "X")
                    $mY = DllStructGetData($tPoint, "Y")
                    $TVhItem = _GUICtrlTreeView_HitTestItem($tviewPI, $mX, $mY)

                    _GUICtrlTreeView_SetFocused($tviewPI, $TVhItem)

                    $var = 0

                    $A = _GUICtrlTreeView_GetItemHandle($tviewPI, $TVhItem)
                    $B = _GUICtrlTreeView_GetParentHandle($tviewPI, $TVhItem)
                    $C = _GUICtrlTreeView_GetParentHandle($tviewPI, _GUICtrlTreeView_GetParentHandle($tviewPI, $TVhItem))

                    If $C = 0 And $B = 0 Then
                        $vLvl1 = _GUICtrlTreeView_GetText($tviewPI, $A)
                        $vLvl2 = ""
                        $vLvl3 = ""

                    ElseIf $C = 0 And $B <> 0 Then
                        $vLvl1 = _GUICtrlTreeView_GetText($tviewPI, $B)
                        $vLvl2 = _GUICtrlTreeView_GetText($tviewPI, $A)
                        $vLvl3 = ""

                    ElseIf $C <> 0 And $B <> 0 Then
                        $vLvl1 = _GUICtrlTreeView_GetText($tviewPI, $C)
                        $vLvl2 = _GUICtrlTreeView_GetText($tviewPI, $B)
                        $vLvl3 = _GUICtrlTreeView_GetText($tviewPI, $A)
                    EndIf

                    ToolTip ("$A: " & $A & @CRLF & "$B: " & $B & @CRLF & "$C: " & $C & @CRLF & @CRLF & $vLvl1 & @CRLF & $vLvl2 & @CRLF & $vLvl3, 0, 0)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Edited by exodius

Share this post


Link to post
Share on other sites
dexto

kind of seems like it does...

That is what I thought at first. What it is doing for me is returning the last item selected with the left click.

Share this post


Link to post
Share on other sites
seandisanti

That is what I thought at first. What it is doing for me is returning the last item selected with the left click.

Right, so if your right click generated a left click at the mouse position, then did what you want, you'd have the info and be able to use it however you want

Share this post


Link to post
Share on other sites
dexto

How about something like this?

Wow... To think I looked at it before you did mod it a bit by hardcoding the TreeView Handle and it works!!!

Thank you!!!

Share this post


Link to post
Share on other sites
dexto

Right, so if your right click generated a left click at the mouse position, then did what you want, you'd have the info and be able to use it however you want

The only way to use mouse position to get handle of the item clicked that I know of is _GUICtrlTreeView_HitTestItem() that takes coordinates and get the handle. Only this it returned for me was 0x000000 that is nothing...

Share this post


Link to post
Share on other sites
exodius

You're welcome, on a side note I believe credit belongs to rasim for the original concept:

http://www.autoitscript.com/forum/index.php?showtopic=86734&st=0&p=622039&#entry622039

Share this post


Link to post
Share on other sites
dexto

Now that is think about it I might have not used _GUICtrlTreeView_HitTestItem(). I'll post if I find any easier solution for this. Thank you for the help!

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.