Jump to content

Making the context menu only on the listbox items, but not the whitespace.


 Share

Recommended Posts

When I make a listbox with a context menu, I see that the context menu pops up even if I haven't selected anything in the list. Any way to make it only pop up when I rightclick an actual item?

Screenshot:

Posted Image

#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)
$gui = GUICreate("test", 182, 262, 489, 365)
GUISetOnEvent($GUI_EVENT_CLOSE, "closeme")
$listbox = GUICtrlCreateList("", 10, 10, 161, 240)
GUICtrlSetData($listbox, "Item 1")
GUICtrlSetData($listbox, "Item 2")
GUICtrlSetData($listbox, "Item 3")
GUICtrlSetData($listbox, "Item 4")
$context = GUICtrlCreateContextMenu($listbox)
$option1 = GUICtrlCreateMenuitem("This", $context)
$option2 = GUICtrlCreateMenuitem("is", $context)
$option3 = GUICtrlCreateMenuitem("my", $context)
$option3 = GUICtrlCreateMenuitem("menu", $context)
GUISetState(@SW_SHOW)

While 1
    Sleep(20)
WEnd

Func closeme()
  Exit
EndFunc

I also noticed that once I select an item in the listbox, I can't de-select it and the list always remains with at least one item selected. I'd like to go back to the original state with all items de-selected by clicking on the whitespace at the bottom of the list items. If you don't know what the heck I'm talking about, don't worry about it LOL. I suck at explaining these kind of things :)

Link to comment
Share on other sites

This is a trick:

#include <GUIConstants.au3>
#include <GuiListBox.au3>
#include <GuiMenu.au3>
#include <WinAPI.au3>

Global Enum $IdNew = 1000, $IdOpen, $IdSelect

Opt("GUIOnEventMode", 1)

$gui = GUICreate("test", 182, 262, 489, 365)
GUISetOnEvent($GUI_EVENT_CLOSE, "closeme")

$listbox = GUICtrlCreateList("", 10, 10, 161, 240)

_GUICtrlListBox_AddString($listbox, "Item1")
_GUICtrlListBox_AddString($listbox, "Item2")
_GUICtrlListBox_AddString($listbox, "Item3")

$hMenu = _GUICtrlMenu_CreatePopup()

_GUICtrlMenu_InsertMenuItem($hMenu, 0, "New", $IdNew)
_GUICtrlMenu_InsertMenuItem($hMenu, 1, "Open", $IdOpen)
_GUICtrlMenu_InsertMenuItem($hMenu, 2, "Select", $IdSelect)

GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")

GUISetState(@SW_SHOW)

While 1
    Sleep(20)
WEnd

Func closeme()
    Exit
EndFunc

Func WM_CONTEXTMENU($hWnd, $Msg, $wParam, $lParam)
    Local $tPoint = _WinAPI_GetMousePos(True, GUICtrlGetHandle($listbox))
    Local $iY = DllStructGetData($tPoint, "Y")
    
    For $i = 0 To 2
        Local $aRect = _GUICtrlListBox_GetItemRect($listbox, $i)
        If ($iY >= $aRect[1]) And ($iY <= $aRect[3]) Then _ContextMenu($i)
    Next
    
    Return $GUI_RUNDEFMSG
EndFunc

Func _ContextMenu($sItem)
    Switch _GUICtrlMenu_TrackPopupMenu($hMenu, GUICtrlGetHandle($listbox), -1, -1, 1, 1, 2)
        Case $IdNew
            ConsoleWrite("Command New selected on item: " & $sItem & @LF)
        Case $IdOpen
            ConsoleWrite("Command Open selected on item: " & $sItem & @LF)
        Case $IdSelect
            ConsoleWrite("Command Select selected on item: " & $sItem & @LF)
    EndSwitch
EndFunc
:)
Link to comment
Share on other sites

Func WM_CONTEXTMENU($hWnd, $Msg, $wParam, $lParam)
    Local $tPoint = _WinAPI_GetMousePos(True, GUICtrlGetHandle($listbox))
    Local $iY = DllStructGetData($tPoint, "Y")
    Local $iX = DllStructGetData($tPoint, "X")
    For $i = 0 To 2
        Local $aRect = _GUICtrlListBox_GetItemRect($listbox, $i)
        If ($iY >= $aRect[1]) And ($iY <= $aRect[3]) and ($iX <= $aRect[2]) and ($iX >= $aRect[0])  Then 
        _GUICtrlListBox_SetCurSel($listbox,$i)
        _ContextMenu($i)
        EndIf
    Next
    Return $GUI_RUNDEFMSG
EndFunc

use this simple tweak.

:)

Edited by Linux
You can help! Donate to AutoIt! or, visit ClimatePREDICTION.netMy posts:Travian Bot Example (100+ servers) BETAHow to Host you code/app for free! (unlimited team number) (Public or Private)"Sir, we're surrounded!" "Excellent. We can attack in any direction!"
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...