Sign in to follow this  
Followers 0
Tec

Dynamic ContextMenu from ListView

2 posts in this topic

Hi

I want to create a dynamic ContextMenu from ListView. But i dont find a solution here.

Sample Code:

#NoTrayIcon
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>

Opt("MustDeclareVars", 1)
Global $hGUI, $hListView, $hContextMenu, $Call, $hmsg
$hGUI = GUICreate("Phonebook", 210, 300)
GUISetIcon(@SystemDir & "\SHELL32.dll", 310, $hGUI)
$hListView = GUICtrlCreateListView("Name                        |Number", 5, 5, 200, 290)
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
$hContextMenu = GUICtrlCreateContextMenu($hListView)
$Call = GUICtrlCreateMenuItem("Call", $hContextMenu)
For $a = 100 To 110
    GUICtrlCreateListViewItem("Name" & $a & "|" & $a, $hListView)
Next
GUISetState()

While 1
    $hmsg = GUIGetMsg()
    Switch $hmsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Call
            _select()
    EndSwitch
    
    Sleep(50)
WEnd

Func _select()
    Local $avSel, $sItemTxt = "", $TestCall
    $avSel = _GUICtrlListView_GetSelectedIndices($hListView, 1)
    If IsArray($avSel) And $avSel[0] > 0 Then
        $sItemTxt = _GUICtrlListView_GetItemText($hListView, $avSel[1], 1)
        ConsoleWrite($sItemTxt & @CRLF)
    EndIf
EndFunc   ;==>_select

On right click I want to create a Context Menu with the option Call <Number> in the row from the second column where I click (Call 108 or Call 106 ...).

I think I need to create the context menu dynamic on right click but dont know how. Have someone a example code for this ?

Share this post


Link to post
Share on other sites



Found a solution.

#NoTrayIcon
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>

GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

Global $hGUI, $hListView, $hContextMenu, $Call, $hmsg
$hGUI = GUICreate("Phonebook", 210, 300)
GUISetIcon(@SystemDir & "\SHELL32.dll", 310, $hGUI)
$hListView = GUICtrlCreateListView("Name                        |Number", 5, 5, 200, 290)
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)

$HelpContext = GUICtrlCreateContextMenu($hListView)
$HelpWWW = GUICtrlCreateMenuItem("Website", $HelpContext)


For $a = 100 To 110
    GUICtrlCreateListViewItem("Name" & $a & "|" & $a, $hListView)
Next
GUISetState()

While 1
    $hmsg = GUIGetMsg()
    Switch $hmsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $HelpWWW
            MsgBox(0,"","TEST")
    EndSwitch
    
    Sleep(50)
WEnd


Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tagNMHDR, $event, $hwndFrom, $code
    $tagNMHDR = DllStructCreate("int;int;int", $lParam)
    If @error Then Return
    $event = DllStructGetData($tagNMHDR, 3)
    Select
        Case $wParam = $hlistview
            Select
                Case $event = $NM_RCLICK
                    $avSel = _GUICtrlListView_GetSelectedIndices($hListView, 1)
                    If IsArray($avSel) And $avSel[0] > 0 Then
                        $sItemTxt = _GUICtrlListView_GetItemText($hListView, $avSel[1], 1)
                        $infoitem = GUICtrlSetData($HelpWWW, "Call " & $sItemTxt)
                    EndIf                   
            EndSelect
    EndSelect
    $tagNMHDR = 0
    $event = 0
    $lParam = 0
EndFunc   ;==>WM_Notify_Events
1 person likes this

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  
Followers 0