Jump to content

ListView ContextMenu


unknown12
 Share

Recommended Posts

I know it is possible to have a context menu on the listview control, but it seems to be "global" (By that I mean it's not item specific, you stilll get it if you right-click in whitespace).

Is there any way to get a context menu that *is* item specific, so an item can be right-clicked on and deleted?

Thanks.

Link to comment
Share on other sites

I know it is possible to have a context menu on the listview control, but it seems to be "global" (By that I mean it's not item specific, you stilll get it if you right-click in whitespace).

Is there any way to get a context menu that *is* item specific, so an item can be right-clicked on and deleted?

Thanks.

Maybe this helps

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

$Debug_LV = False; Check ClassName being passed to ListView functions, set to True and use a handle to another control to see it work

Global $B_DESCENDING

Local $btnInstall, $btnAdd

$iniFile = @ScriptDir & "\InstaProLauncher.ini"

$hGUI = GUICreate("InstaPro Launcher", 875, 600)

$cListView = GUICtrlCreateListView("", 100, 40, 750, 500, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
$hListView = GUICtrlGetHandle($cListView)


$dummy = GUICtrlCreateDummy()
$lvcm = GUICtrlCreateContextMenu($dummy)
$menuRow = GUICtrlCreateMenuItem("aaa", $lvcm)
$menuCol = GUICtrlCreateMenuItem("aaa",$lvcm)
;Added Code
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
;Added Code End

_GUICtrlListView_InsertColumn($hListView, 0, "Column 1", 100)
_GUICtrlListView_InsertColumn($hListView, 1, "Column 2", 100)
_GUICtrlListView_InsertColumn($hListView, 2, "Column 3", 100)
_GUICtrlListView_InsertColumn($hListView, 3, "Column 4", 100)
_GUICtrlListView_InsertColumn($hListView, 4, "Column 5", 100)

_GUICtrlListView_SetColumnWidth($hListView, 0, 150)
_GUICtrlListView_SetColumnWidth($hListView, 1, 296)
_GUICtrlListView_SetColumnWidth($hListView, 2, 100)
_GUICtrlListView_SetColumnWidth($hListView, 3, 100)
_GUICtrlListView_SetColumnWidth($hListView, 4, 100)
    
GUIRegisterMsg($WM_NOTIFY,"WM_NOTIFY")
Global $setcm = false
Dim $varAI[3][5]
; Add items
For $i = 0 To UBound($varAI) - 1  
    $varAI[$i][0] = $i & "One"
    $varAI[$i][1] = $i & "two"
    $varAI[$i][2] = $i & "three"
    $varAI[$i][3] = $i & "four"
    $varAI[$i][4] = $i & "five"
Next
_GUICtrlListView_AddArray($hListView, $varAI)



$btnInstall = GUICtrlCreateButton("Install", 750, 550, 100)

GUISetState()

Global $B_DESCENDING[_GUICtrlListView_GetColumnCount($hListView)]

Do
    $msg = GUIGetMsg()
        
    if $setcm Then
        $setcm = False
        TrackPopupMenu($hGui,GuiCtrlGetHandle($lvcm),MouseGetPos(0),MouseGetPos(1))
    EndIf
Until $msg = $GUI_EVENT_CLOSE

Exit



Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)
    
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hListView
            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, $lParam)
                    _DebugPrint("$NM_RCLICK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                            "-->Code:" & @TAB & $iCode & @LF & _
                            "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @LF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @LF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @LF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @LF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @LF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @LF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @LF & _
                            "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @LF & _
                            "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
                            
                          ;set context menu text
                            GuiCtrlSetData($menuRow,"-->Index:" & @TAB & DllStructGetData($tInfo, "Index"))
                GuiCtrlSetData($menuCol,"-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem"))
                if DllStructGetData($tInfo, "Index") <>  then  $setcm = true
              ;Return 1; not to allow the default processing
                
                    Return 0; allow the default processing
                

            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc;==>WM_NOTIFY



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


Func TrackPopupMenu($hWnd, $hMenu, $x, $y)
    ConsoleWrite("x,y = " & $hmenu & ',' & $hwnd & @CRLF)
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
EndFunc ;==>TrackPopupMenu

EDIT: Added condition so only a menu when clicking over an item.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

unknown12

Another way:

#Include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>

$hGUI = GUICreate("Context menu demo", 300, 200)

$hListView = GUICtrlCreateListView("Items|SubItems", 10, 10, 280, 180)

For $i = 1 To 10
    GUICtrlCreateListViewItem("Item " & $i & "|SubItem " & $i, $hListView)
Next

$hContextMenu = GUICtrlCreateContextMenu($hListView)

$Open_MenuItem = GUICtrlCreateMenuItem("Open", $hContextMenu)
$Del_MenuItem1 = GUICtrlCreateMenuItem("Delete", $hContextMenu)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $hWnd_ListView, $tNMHDR, $hWndFrom, $iCode
    
    $hWnd_ListView = $hListView
    If Not IsHWnd($hWnd_ListView) Then $hWnd_ListView = GUICtrlGetHandle($hListView)
    
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    
    Switch $hWndFrom
        Case $hWnd_ListView
            Switch $iCode
                Case $NM_RCLICK
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                    Local $iItem = DllStructGetData($tInfo, "Item")
                    
                    If $iItem = -1 Then
                        GUICtrlSetState($Open_MenuItem, $GUI_DISABLE)
                        GUICtrlSetState($Del_MenuItem1, $GUI_DISABLE)
                    Else
                        GUICtrlSetState($Open_MenuItem, $GUI_ENABLE)
                        GUICtrlSetState($Del_MenuItem1, $GUI_ENABLE)
                    EndIf
            EndSwitch
    EndSwitch
    
    Return $GUI_RUNDEFMSG
EndFunc

:P

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...