Jump to content
Sign in to follow this  
AustrianOak

context menu creation on listview

Recommended Posts

AustrianOak

Switch $nWndFrom
        Case $nWndListView
            Switch $iCode
                Case $NM_RCLICK
                    Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    Local $iIndex = DllStructGetData($tInfo, "Index")
                    If $iIndex <> -1 Then _ContextMenu($iIndex)

My code looks like this and what it does is it calls a function that creates a a context menu that says 'whatever'

What if I want to add another context menu when I right click? Would it be like this?

Switch $nWndFrom
        Case $nWndListView
            Switch $iCode
                Case $NM_RCLICK
                    Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    Local $iIndex = DllStructGetData($tInfo, "Index")
                    If $iIndex <> -1 Then _ContextMenu($iIndex)

                    Local $iInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    Local $zIndex = DllStructGetData($iInfo, "Index")
                    If $zIndex <> -1 Then _ContextMenu2($zIndex)

I tried this above and the second context menu only appears if I right click twice. Any advice?

Share this post


Link to post
Share on other sites
Airwolf

Have you taken a look at the GUICtrlCreateContextMenu() function? I'm not sure if you're dealing with an AutoIt GUI or not, but if you are then you can create GUI or control specific context menus with this built-in function.


Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt

Share this post


Link to post
Share on other sites
AustrianOak

I know about that function but that's not exactly what I'm looking for...

I'm just having a general problem with a context menu being in some sort of loop i guess..

Share this post


Link to post
Share on other sites
rasim

nowagain

Trick example from GaryFrost

#include <GuiMenu.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>

; ========================================================================
; Global variables
; ========================================================================

Global Enum $idOpen = 1000, $idSave, $idInfo

; ========================================================================
; Main
; ========================================================================

$hGUI = GUICreate('Context Menu Demo (Right Click)', 400, 300)
$hListView = _GUICtrlListView_Create($hGUI, "", 2, 2, 394, 268)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

; Load images
$hImage = _GUIImageList_Create()
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0xFF0000, 16, 16))
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0x00FF00, 16, 16))
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0x0000FF, 16, 16))
_GUICtrlListView_SetImageList($hListView, $hImage, 1)

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

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

GUISetState()

; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func ListView_RClick()
    Local $aHit

    $aHit = _GUICtrlListView_SubItemHitTest($hListView)
    If ($aHit[0] <> -1) Then
        ; Create a standard popup menu
        ; -------------------- To Do --------------------
        $hMenu = _GUICtrlMenu_CreatePopup()
        _GUICtrlMenu_AddMenuItem($hMenu, "Open", $idOpen)
        _GUICtrlMenu_AddMenuItem($hMenu, "Save", $idSave)
        _GUICtrlMenu_AddMenuItem($hMenu, "Info", $idInfo)
        ; ========================================================================
        ; Shows how to capture the context menu selections
        ; ========================================================================
        Switch _GUICtrlMenu_TrackPopupMenu($hMenu, $hListView, -1, -1, 1, 1, 2)
            Case $idOpen
                _DebugPrint("Open: " & StringFormat("Item, SubItem [%d, %d]", $aHit[0], $aHit[1]))
            Case $idSave
                _DebugPrint("Save: " & StringFormat("Item, SubItem [%d, %d]", $aHit[0], $aHit[1]))
            Case $idInfo
                _DebugPrint("Info: " & StringFormat("Item, SubItem [%d, %d]", $aHit[0], $aHit[1]))
        EndSwitch
        _GUICtrlMenu_DestroyMenu($hMenu)
    EndIf
EndFunc   ;==>ListView_RClick


Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    ;$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $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 left mouse button
                    ListView_RClick()
                    Return 0
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func _DebugPrint($s_text, $line = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @LF & _
            "+======================================================" & @LF & _
            "-->Line(" & StringFormat("%04d", $line) & "):" & @TAB & $s_text & @LF & _
            "+======================================================" & @LF)
EndFunc   ;==>_DebugPrint

Share this post


Link to post
Share on other sites
AustrianOak

Thank you rasim, I solved it a few hours ago :)

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  

×