Jump to content

Select list item with right click?


VAN0
 Share

Recommended Posts

Hello.

Me again with endless questions about lists...

How can I make so it would select an item in list box with right click as well as left click?

I have the following example:

#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include <GUIListBox.au3>

$hGUI = GUICreate("Test", 500, 500)

$hList = GUICtrlCreateList("", 10, 10, 480, 300)
GUICtrlSetData($hList, "Line 1|Line 2|Line 3")
$hListContext = GUICtrlCreateContextMenu($hList)
$hListContextItem = GUICtrlCreateMenuItem("Menu item", $hListContext)
$hListContextUnselect = GUICtrlCreateMenuItem("Unselect me", $hListContext)
GUICtrlCreateEdit("some text", 10, 310, 480, 180)

$WM_CONTEXTMENU = 123
GUIRegisterMsg($WM_CONTEXTMENU, "_WM_CONTEXTMENU") ;display context menu only when list is in focus and selected an item

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hListContextItem
            MsgBox(0,'',GUICtrlRead($hList, getSelectedItem()))
        Case $hListContextUnselect
            _GUICtrlListBox_SetCurSel($hList, -1)
    EndSwitch

WEnd

Func getSelectedItem()
    Local $index = -1
    If (_WinAPI_GetFocus() = GUICtrlGetHandle($hList)) And GUICtrlRead($hList) Then
        $index = _GUICtrlListBox_GetCurSel($hList)
    EndIf
    Return $index
EndFunc   ;==>getSelectedFile

Func _WM_CONTEXTMENU($hwnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    If $wParam <> GUICtrlGetHandle($hList) Then Return $GUI_RUNDEFMSG
    Local $index = _GUICtrlListBox_GetCurSel($hList)
    If $index <> -1 Then
        Return $GUI_RUNDEFMSG
    EndIf
    Return 0
EndFunc   ;==>_WM_CONTEXTMENU

Currently it shows context menu only when right click over list box and only when an item selected. If nothing selected it doesn't show any context menu. I'd like it to automatically select an item under the cursor with right click and then display context menu.

Any suggestions?

Thank you.

Link to comment
Share on other sites

Maybe it's not the best solution, but I got it working with _GUICtrlListBox_ItemFromPoint()

#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include <GUIListBox.au3>

$hGUI = GUICreate("Test", 500, 500)

Global $hListX = 10, $hListY = 10

$hList = GUICtrlCreateList("", $hListX, $hListY, 480, 300)
GUICtrlSetData($hList, "Line 1|Line 2|Line 3")
$hListContext = GUICtrlCreateContextMenu($hList)
$hListContextItem = GUICtrlCreateMenuItem("Menu item", $hListContext)
$hListContextUnselect = GUICtrlCreateMenuItem("Unselect me", $hListContext)
GUICtrlCreateEdit("some text", 10, 310, 480, 180)

$WM_CONTEXTMENU = 123
GUIRegisterMsg($WM_CONTEXTMENU, "_WM_CONTEXTMENU") ;display context menu only when list is in focus and selected an item

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hListContextItem
            MsgBox(0,'',GUICtrlRead($hList, getSelectedItem()))
        Case $hListContextUnselect
            _GUICtrlListBox_SetCurSel($hList, -1)
    EndSwitch

WEnd

Func getSelectedItem()
    Local $index = -1
    If (_WinAPI_GetFocus() = GUICtrlGetHandle($hList)) And GUICtrlRead($hList) Then
        $index = _GUICtrlListBox_GetCurSel($hList)
    EndIf
    Return $index
EndFunc   ;==>getSelectedFile

Func _WM_CONTEXTMENU($hwnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    If $wParam <> GUICtrlGetHandle($hList) Then Return $GUI_RUNDEFMSG
    Local $cursor = GUIGetCursorInfo($hGUI)
    Local $index = _GUICtrlListBox_ItemFromPoint($hList, $cursor[0] - $hListX - 2, $cursor[1] - $hListY - 2)
    If $index == -1 Then Return 0
    _GUICtrlListBox_SetCurSel($hList, $index)
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_CONTEXTMENU

Maybe there is a better way?

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