Jump to content

create a context menu to listView


Recommended Posts


Hello all
I have a question please
this  a question is about the context menu
When i create a list box  Or combo box
if i create a context menu for this list
When i press the application key or the shortcut shift + f10
the menu options is appear normally
but When i create a List view
if i create a context menu for this list
When i press the application key or the shortcut shift + f10
the menu options isn't appear
But the right mouse button works normally
I need a solution please because I deal with the screen readers users
Unfortunately They can not use the mouse to navigate
So I have to find a solution to activate keyboard shortcuts
am waiting  for your help

Edited by nacerbaaziz
Link to comment
Share on other sites

  • Moderators

@nacerbaaziz you know what comes next, you need to provide code that shows the issue you are encountering.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

hello again
this is my example
When i press the application key or the shortcut shift + f10
the menu options isn't appear
But the right mouse button works normally


#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <GuiListView.au3>
main()
func main()
local $GUI = GUICreate("listView context menu", 800, 400, @DesktopWidth / 2 - 192, @DesktopHeight / 2 - 235, -1, $WS_EX_ACCEPTFILES + $WS_EX_TOPMOST)
global $list = GUICtrlCreateListView("", 0, 10, 700, 150, bitOr($LVS_SINGLESEL,$LVS_SORTASCENDING))

    _GUICtrlListView_AddColumn($list, "Name", 150, 0)
    _GUICtrlListView_AddColumn($list, "version", 50, 0)
    _GUICtrlListView_AddColumn($list, "Publisher", 150, 0)
    _GUICtrlListView_AddColumn($list, "Uninstall file path", 400, 0)
local $menu = GUICtrlCreateContextMenu($list)
local $Ref = GUICtrlCreateMenuItem("refresh", $menu)
local $Unins = GUICtrlCreateMenuItem("Uninstall", $menu)
Unins_refresh()
GUISetState(@sw_Show, $GUI)
while 1
Switch GUIGetMSG()
case $GUI_Event_Close
exit
case $Ref
Unins_refresh()
case $Unins
            $selecteditem = _GUICtrlListView_GetItemTextString($list, Int(_GUICtrlListView_GetSelectedIndices($list)))
            $selecteditemarray = StringSplit($selecteditem, "|")

Dim $iMsgBoxAnswer
$iMsgBoxAnswer = MsgBox(4132,"uninstall " &             $selecteditemarray[1] & "?","are you sure want to uninstall " &             $selecteditemarray[1] & " version" &             $selecteditemarray[2] & "?")
Select
   Case $iMsgBoxAnswer = 6 ;Yes

run($selecteditemarray[4])
EndSelect

EndSwitch
Wend
endFunc
Func Unins_refresh()
    _GUICtrlListView_DeleteAllItems($list)
    ProgressOn("Uninstall Manager", "Loading...", "0%")

local $r_var, $r_hash = 0, $UninsName = "", $UnInsPath = "", $UninsVersion = "", $UninsPublisher = "", $SystemComponent
local $RegKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    While 1
        $r_hash += 1
        $r_var = RegEnumKey($regKey, $r_hash)
        If @error <> 0 Then
$r_var = ""
$r_hash = 0
$UninsName = ""
$UnInsPath = ""
$UninsVersion = ""
$UninsPublisher = ""
            ExitLoop

        Else
        $r_var = $Regkey & "\" &         $r_var
$UNinsName = RegRead($r_var, "DisplayName")
if @error then $UNinsName = ""
$UNinsVersion = RegRead($r_var, "DisplayVersion")
if @error then $UNinsVersion = ""
$UninsPublisher = RegRead($r_var, "Publisher")
if @error then $UninsPublisher = ""
$UNinsPath = RegRead($r_var, "UninstallString")
if @error then $UNinsPath = ""
$SystemComponent = RegRead($r_var, "SystemComponent")
if @error then $SystemComponent = 0
if not ($UninsPath = "") and not ($UninsName = "") and not ($SystemComponent = 1) then
            GUICtrlCreateListViewItem($UninsName & "|" & $UninsVersion & "|" & $UninsPublisher & "|" & $UninsPath, $list)
endIf
        EndIf
    WEnd
$RegKey = "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    While 1
        $r_hash += 1
        $r_var = RegEnumKey($regKey, $r_hash)
        If @error <> 0 Then
$r_var = ""
$r_hash = 0
$UninsName = ""
$UnInsPath = ""
$UninsVersion = ""
$UninsPublisher = ""
            ExitLoop

        Else
        $r_var = $Regkey & "\" &         $r_var
$UNinsName = RegRead($r_var, "DisplayName")
if @error then $UNinsName = ""
$UNinsVersion = RegRead($r_var, "DisplayVersion")
if @error then $UNinsVersion = ""
$UninsPublisher = RegRead($r_var, "Publisher")
if @error then $UninsPublisher = ""
$UNinsPath = RegRead($r_var, "UninstallString")
if @error then $UNinsPath = ""
$SystemComponent = RegRead($r_var, "SystemComponent")
if @error then $SystemComponent = 0
if not ($UninsPath = "") and not ($UninsName = "") and not ($SystemComponent = 1) then
            GUICtrlCreateListViewItem($UninsName & "|" & $UninsVersion & "|" & $UninsPublisher & "|" & $UninsPath, $list)
endIf
        EndIf
WEnd
###
if @OSArch = "x64" then
$RegKey = "HKLM64\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    While 1
        $r_hash += 1
        $r_var = RegEnumKey($regKey, $r_hash)
        If @error <> 0 Then
$r_var = ""
$r_hash = 0
$UninsName = ""
$UnInsPath = ""
$UninsVersion = ""
$UninsPublisher = ""
            ExitLoop

        Else
        $r_var = $Regkey & "\" &         $r_var
$UNinsName = RegRead($r_var, "DisplayName")
if @error then $UNinsName = ""
$UNinsVersion = RegRead($r_var, "DisplayVersion")
if @error then $UNinsVersion = ""
$UninsPublisher = RegRead($r_var, "Publisher")
if @error then $UninsPublisher = ""
$UNinsPath = RegRead($r_var, "UninstallString")
if @error then $UNinsPath = ""
$SystemComponent = RegRead($r_var, "SystemComponent")
if @error then $SystemComponent = 0
if not ($UninsPath = "") and not ($UninsName = "") and not ($SystemComponent = 1) then
            GUICtrlCreateListViewItem($UninsName & "|" & $UninsVersion & "|" & $UninsPublisher & "|" & $UninsPath, $list)
endIf
        EndIf
WEnd
$RegKey = "HKCU64\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    While 1
        $r_hash += 1
        $r_var = RegEnumKey($regKey, $r_hash)
        If @error <> 0 Then
$r_var = ""
$r_hash = 0
$UninsName = ""
$UnInsPath = ""
$UninsVersion = ""
$UninsPublisher = ""
            ExitLoop

        Else
        $r_var = $Regkey & "\" &         $r_var
$UNinsName = RegRead($r_var, "DisplayName")
if @error then $UNinsName = ""
$UNinsVersion = RegRead($r_var, "DisplayVersion")
if @error then $UNinsVersion = ""
$UninsPublisher = RegRead($r_var, "Publisher")
if @error then $UninsPublisher = ""
$UNinsPath = RegRead($r_var, "UninstallString")
if @error then $UNinsPath = ""
$SystemComponent = RegRead($r_var, "SystemComponent")
if @error then $SystemComponent = 0
if not ($UninsPath = "") and not ($UninsName = "") and not ($SystemComponent = 1) then

            GUICtrlCreateListViewItem($UninsName & "|" & $UninsVersion & "|" & $UninsPublisher & "|" & $UninsPath, $list)
endIf
        EndIf
WEnd
endIf
endFunc


:)
Please help me

Edited by nacerbaaziz
Link to comment
Share on other sites

I recently encountered a similar issue with trying to allow update on arrow up and down in a list view

same thing should help you as well

Global $g_hList1 = GUICtrlCreateListView("#|x|y", 5, 24, 161, 201, $LVS_SHOWSELALWAYS Or $LVS_SINGLESEL)
Global $g_hList1_LVN = GUICtrlCreateDummy() ;listview notifications

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

;Event loop etc....
If GuiGetMsg() == $g_hList1_LVN Then
            ;Your Code
EndIf
;;;.................................

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    Static Local $hWndList1 = GUICtrlGetHandle($g_hList1)
    If Not IsHWnd($hWndList1) Then $hWndList1 = GUICtrlGetHandle($g_hList1)
    If @error Then Return $GUI_RUNDEFMSG
    If $wParam = $g_hList1 Then
        Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)

        Switch DllStructGetData($tNMHDR, "Code")

            Case $LVN_KEYDOWN, $NM_CLICK
                GUICtrlSendToDummy($g_hList1_LVN, $lParam)
        EndSwitch
    EndIf

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
































































;

To see it in full context check here:

You also don't really need the dummy control but I wanted to receive an even in my event loop and handle it there rather than in the WM_Notify callback

Noticed I saved the listview handle as well; It seems that sometimes it takes forever for the GUICtrlGetHandle() function to return...

Edited by Bilgus
Made the code post Long so you can use the popup button to get it in a separate window
Link to comment
Share on other sites

Basically you don't get notifications for keypress in the list view so you register a callback for WM_NOTIFY and look for the handle of the listview

This calls the dummy control when it gets a key even or mouse click

in the event for the dummy control you check for a mouse right click or keyboard key and pop your menu

If you want more code than that follow the link and you can see the code in action for the arrow keys

I put the code for DrawPathPoints in a file for you in the example code post..

Edited by Bilgus
Link to comment
Share on other sites

  • Moderators

You have all that code, but don't understand 18 lines that were provided to you? Are you just copying your code from somewhere, without actually understanding what how it works?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • 3 weeks later...

hiI understand the code,
but let's say I have more than one list view in the same GUI
so how can I do each list view its own context menu
This is the code that I do, please help me to adjust it.
I apologize for my difficulty in understanding.

#include <Misc.au3>
#include <ListViewConstants.au3>
#include <GUIConstants.au3>
Global $GUI = GUICreate("test")
Global $g_hList1 = GUICtrlCreateListView("#|x|y", 5, 24, 161, 70, $LVS_SHOWSELALWAYS Or $LVS_SINGLESEL)
Global $g_hList1_LVN = GUICtrlCreateDummy()
Global $context = GUICtrlCreateContextMenu($g_hList1_LVN)
GUICtrlCreateMenuItem("1", $context)
GUICtrlCreateMenuItem("2", $context)

Global $g_hList2 = GUICtrlCreateListView("#|x|y", 5, 100, 161, 70, $LVS_SHOWSELALWAYS Or $LVS_SINGLESEL)
Global $g_hList2_LVN = GUICtrlCreateDummy()
Global $context2 = GUICtrlCreateContextMenu($g_hList2_LVN)
GUICtrlCreateMenuItem("3", $context2)
GUICtrlCreateMenuItem("4", $context2)

GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
        Case $g_hList1_LVN
            ShowMenu($GUI, GUIGetMsg(), $context)
    EndSwitch
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    Static Local $hWndList1 = GUICtrlGetHandle($g_hList1)
    If Not IsHWnd($hWndList1) Then $hWndList1 = GUICtrlGetHandle($g_hList1)
    If @error Then Return $GUI_RUNDEFMSG
    If $wParam = $g_hList1 Then
        Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
        Switch DllStructGetData($tNMHDR, "Code")
            Case $LVN_KEYDOWN, $NM_CLICK
                If _IsPressed("02") Or _IsPressed("5D") Then
                    GUICtrlSendToDummy($g_hList1_LVN, $lParam)
                EndIf
        EndSwitch
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Func ShowMenu($hWnd, $idCtrl, $idContext)
    Local $aPos, $x, $y
    Local $hMenu = GUICtrlGetHandle($idContext)

    $aPos = ControlGetPos($hWnd, "", $idCtrl)

    $x = $aPos[0]
    $y = $aPos[1] + $aPos[3]

    ClientToScreen($hWnd, $x, $y)
    TrackPopupMenu($hWnd, $hMenu, $x, $y)
EndFunc   ;==>ShowMenu

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

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

    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "struct*", $tPoint)

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

; Show at the given coordinates (x, y) the popup menu (hMenu) which belongs to a given GUI window (hWnd)
Func TrackPopupMenu($hWnd, $hMenu, $x, $y)
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
EndFunc   ;==>TrackPopupMenu

@Bilgus

Edited by Jos
added codebox ... please use that in the future and tidied code
Link to comment
Share on other sites

There is the bare minimum for what you want

#include <Misc.au3>
#include <ListViewConstants.au3>
#include <GUIConstants.au3>
#include <GuiMenu.au3>

Global $GUI = GUICreate("test")
Global $g_iTemp
Global $g_hList1 = GUICtrlCreateListView("#|x|y", 5, 24, 161, 70, $LVS_SHOWSELALWAYS Or $LVS_SINGLESEL)
GUICtrlCreateListViewItem("text", $g_hList1)
Global $g_hList1_LVN = GUICtrlCreateDummy()

Global $context = GUICtrlCreateContextMenu($g_hList1)
GUICtrlCreateMenuItem("1", $context)
GUICtrlCreateMenuItem("2", $context)

Global $g_hList2 = GUICtrlCreateListView("#|x|y", 5, 100, 161, 70, $LVS_SHOWSELALWAYS Or $LVS_SINGLESEL)
GUICtrlCreateListViewItem("text", $g_hList2)
Global $g_hList2_LVN = GUICtrlCreateDummy()
Global $context2 = GUICtrlCreateContextMenu($g_hList2)
GUICtrlCreateMenuItem("3", $context2)
GUICtrlCreateMenuItem("4", $context2)

GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
        Case $g_hList1_LVN ;This is just a dummy it only recieves events
            ConsoleWrite("LV1 EVENT" & @CRLF)
            $g_iTemp = GUICtrlRead($g_hList1_LVN) ;Retrieve the code that WM_NOTIFY SENT
            If $g_iTemp = $LVN_KEYDOWN Then
                If _IsPressed("79") And (_IsPressed("A0") Or _IsPressed("A1")) Then ;Right/ Left Shift & F10
                    ShowMenu($GUI, $g_hList1, $context)
                Else
                    ConsoleWrite("Some Other Keys" & @CRLF)
                EndIf
            Else
                ConsoleWrite("Some Other Event" & @CRLF)
            EndIf
        Case $g_hList2_LVN
            ConsoleWrite("LV2 EVENT" & @CRLF)
            $g_iTemp = GUICtrlRead($g_hList1_LVN)
            If $g_iTemp = $LVN_KEYDOWN Then
                If _IsPressed("79") And (_IsPressed("A0") Or _IsPressed("A1")) Then ;Right/ Left Shift & F10
                    ShowMenu($GUI, $g_hList2, $context2)
                EndIf
            EndIf
    EndSwitch
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    Local $tNMHDR, $iCode ;
    Switch $wParam
        Case $g_hList1
            $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
            $iCode = DllStructGetData($tNMHDR, "Code")
            Switch $iCode
                Case $LVN_KEYDOWN, $NM_CLICK
                    GUICtrlSendToDummy($g_hList1_LVN, $iCode) ;send the code back to be processed by the dummy
            EndSwitch
        Case $g_hList2
            $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
            $iCode = DllStructGetData($tNMHDR, "Code")
            Switch $iCode
                Case $LVN_KEYDOWN, $NM_CLICK
                    GUICtrlSendToDummy($g_hList2_LVN, $iCode)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func ShowMenu($hWnd, $idCtrl, $idContext)
    Local $aPos, $x, $y
    Local $hMenu = GUICtrlGetHandle($idContext)

    $aPos = ControlGetPos($hWnd, "", $idCtrl)

    $x = $aPos[0]
    $y = $aPos[1] + $aPos[3]

    ClientToScreen($hWnd, $x, $y)
    TrackPopupMenu($hWnd, $hMenu, $x, $y)
EndFunc   ;==>ShowMenu

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

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

    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "struct*", $tPoint)

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

; Show at the given coordinates (x, y) the popup menu (hMenu) which belongs to a given GUI window (hWnd)
Func TrackPopupMenu($hWnd, $hMenu, $x, $y)
    ;DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
    _GUICtrlMenu_TrackPopupMenu($hMenu, $hWnd, $x, $y)
EndFunc   ;==>TrackPopupMenu

 

Edited by Bilgus
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

×
×
  • Create New...