Что нового

Открыть контекстное меню только под элементом Treeview

Vint91

Чайник
Сообщения
9
Репутация
0
Добрый день, уважаемые знатоки. Подскажите как это сделать? Я нашел код как это реализована с Listview, но моих знаний не хватает его преобразовать под Treeview.
Вот собственно рабочий код с Listview
Код:
#include <GUIConstantsEx.au3>
#include <GUIMenu.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>
;

Global $hListView , $iLast_LV_Index, $Form1

$Form1 = GUICreate("Context Menu on ListView Items - Example", 400, 300)

$DummyMenu = GUICtrlCreateDummy()
$ContextMenu = GUICtrlCreateContextMenu($DummyMenu)

$SubMenu = GUICtrlCreateMenu("Submenu", $ContextMenu)
$SB_Item = GUICtrlCreateMenuItem("Item in submenu", $SubMenu)

GUICtrlCreateMenuItem("", $ContextMenu)

$Some_Item = GUICtrlCreateMenuItem("Some Item", $ContextMenu)
$Exit_Item = GUICtrlCreateMenuItem("Exit", $ContextMenu)

$hListView = _GUICtrlListView_Create($Form1, "", 32, 16, 340, 260) ; LEFT],[TOP],WIDTH],[HEIGHT]
_GUICtrlListView_SetExtendedListViewStyle($hListView, _
    BitOR($LVS_AUTOARRANGE ,$LVS_EX_CHECKBOXES,$LVS_EX_FULLROWSELECT,$LVS_EX_DOUBLEBUFFER))

; Add columns
_GUICtrlListView_InsertColumn($hListView, 0, "Column 1", 100)

; Add items
_GUICtrlListView_AddItem($hListView, "Row 1: Col 1", 0)
_GUICtrlListView_AddItem($hListView, "Row 2: Col 1", 1)
_GUICtrlListView_AddItem($hListView, "Row 3: Col 1", 2)

GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    $nMsg = GUIGetMsg()

    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $SB_Item To $Exit_Item
            Local $sMenu_Item_Text = GUICtrlRead($nMsg, 1)
            Local $sLV_Item_Text = _GUICtrlListView_GetItemTextString($hListView, $iLast_LV_Index)

            MsgBox(64, 'Context Menu', _
                StringFormat('Menu Item Clicked: %s\nLV Item Index: %s\nLV Item Text: %s', _
                    $sMenu_Item_Text, $iLast_LV_Index, $sLV_Item_Text), 0, $Form1)

            If $nMsg = $Exit_Item Then Exit
    EndSwitch
WEnd

; Show a menu in a given GUI window which belongs to a given GUI ctrl
Func ShowMenu($hWnd, $nContextID, $nContextControlID, $iMouse=0)
    Local $hMenu = GUICtrlGetHandle($nContextID)
    Local $iCtrlPos = ControlGetPos($hWnd, "", $nContextControlID)

    Local $X = $iCtrlPos[0]
    Local $Y = $iCtrlPos[1] + $iCtrlPos[3]

    ClientToScreen($hWnd, $X, $Y)

    If $iMouse Then
        $X = MouseGetPos(0)
        $Y = MouseGetPos(1)
    EndIf

    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $X, "int", $Y, "hwnd", $hWnd, "ptr", 0)
EndFunc

; Convert the client (GUI) coordinates to screen (desktop) coordinates
Func ClientToScreen($hWnd, ByRef $x, ByRef $y)
    Local $stPoint = DllStructCreate("int;int")

    DllStructSetData($stPoint, 1, $x)
    DllStructSetData($stPoint, 2, $y)

    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPoint))

    $x = DllStructGetData($stPoint, 1)
    $y = DllStructGetData($stPoint, 2)
    ; release Struct not really needed as it is a local
    $stPoint = 0
EndFunc

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom,  $iCode, $tNMHDR, $hWndListView, $tInfo

    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_RCLICK ; Sent by a list-view control when the user clicks an item with the right mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                    $iIndex = DllStructGetData($tInfo, "Index")

                    If $iIndex <> -1 Then
                        $iLast_LV_Index = $iIndex
                        ShowMenu($hWnd, $ContextMenu, $hListView, 1)
                    EndIf
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

А вот мой код где я просто заменил listview на tree
Код:
#include <GUIConstantsEx.au3>
#include <GUIMenu.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>

Global $idTreeview , $iLast_LV_Index, $Form1

$Form1 = GUICreate("Context Menu on ListView Items - Example", 400, 300)

$DummyMenu = GUICtrlCreateDummy()
$ContextMenu = GUICtrlCreateContextMenu($DummyMenu)

$SubMenu = GUICtrlCreateMenu("Submenu", $ContextMenu)
$SB_Item = GUICtrlCreateMenuItem("Item in submenu", $SubMenu)

GUICtrlCreateMenuItem("", $ContextMenu)

$Some_Item = GUICtrlCreateMenuItem("Some Item", $ContextMenu)
$Exit_Item = GUICtrlCreateMenuItem("Exit", $ContextMenu)

$idTreeview = GUICtrlCreateTreeView(5, 25,150 , 300)
 _GUICtrlTreeView_Add($idTreeview, 0, "Element 1" )
_GUICtrlTreeView_Add($idTreeview, 0, "Element 2" )


GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    $nMsg = GUIGetMsg()

    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $SB_Item To $Exit_Item
            Local $sMenu_Item_Text = GUICtrlRead($nMsg, 1)
            Local $sLV_Item_Text = _GUICtrlListView_GetItemTextString($idTreeview, $iLast_LV_Index)

            MsgBox(64, 'Context Menu', _
                StringFormat('Menu Item Clicked: %s\nLV Item Index: %s\nLV Item Text: %s', _
                    $sMenu_Item_Text, $iLast_LV_Index, $sLV_Item_Text), 0, $Form1)

            If $nMsg = $Exit_Item Then Exit
    EndSwitch
WEnd

; Show a menu in a given GUI window which belongs to a given GUI ctrl
Func ShowMenu($hWnd, $nContextID, $nContextControlID, $iMouse=0)
    Local $hMenu = GUICtrlGetHandle($nContextID)
    Local $iCtrlPos = ControlGetPos($hWnd, "", $nContextControlID)

    Local $X = $iCtrlPos[0]
    Local $Y = $iCtrlPos[1] + $iCtrlPos[3]

    ClientToScreen($hWnd, $X, $Y)

    If $iMouse Then
        $X = MouseGetPos(0)
        $Y = MouseGetPos(1)
    EndIf

    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $X, "int", $Y, "hwnd", $hWnd, "ptr", 0)
EndFunc

; Convert the client (GUI) coordinates to screen (desktop) coordinates
Func ClientToScreen($hWnd, ByRef $x, ByRef $y)
    Local $stPoint = DllStructCreate("int;int")

    DllStructSetData($stPoint, 1, $x)
    DllStructSetData($stPoint, 2, $y)

    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPoint))

    $x = DllStructGetData($stPoint, 1)
    $y = DllStructGetData($stPoint, 2)
    ; release Struct not really needed as it is a local
    $stPoint = 0
EndFunc

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom,  $iCode, $tNMHDR, $hWndListView, $tInfo

    $hWndListView = $idTreeview
    If Not IsHWnd($idTreeview) Then $hWndListView = GUICtrlGetHandle($idTreeview)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_RCLICK ; Sent by a list-view control when the user clicks an item with the right mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                    $iIndex = DllStructGetData($tInfo, "Index")

                    If $iIndex <> -1 Then
                        $iLast_LV_Index = $iIndex
                        ShowMenu($hWnd, $ContextMenu, $idTreeview, 1)
                    EndIf
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc
 

joiner

Модератор
Локальный модератор
Сообщения
2 992
Репутация
501
внизу страницы есть похожие темы. первая строка прямо твой вариант
 
Верх