Jump to content

Recommended Posts

Posted

Hi,

Just a quick question.

I have a contextmenu on my listview, where i can select options for my items, works all fine.

But it also pops up if there are 0 items.

I only want it to popup when there are 1 or more items in the listview.

How?

Is there something like an opening event for the contextmenu? :/

  • Moderators
Posted

MadaraUchiha,

There might well be a better way - but this seems to work quite well: :D

#include <GUIConstantsEx.au3>
#include <Constants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

Global $mCM_1 = 0, $mCM_2 = 0, $fFlag_1 = False, $fFlag_2 = False, $mItem_1 = 9999, $mItem_2 = 9999

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

$cLV_1 = GUICtrlCreateListView("          ", 10, 10, 230, 240)
GUICtrlCreateListViewItem("Item 1", $cLV_1)

$cLV_2 = GUICtrlCreateListView("          ", 260, 10, 230, 240)

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $mItem_1
            MsgBox($MB_SYSTEMMODAL, "Menu", "ListView 1 context menu actioned")
        Case $mItem_2
            MsgBox($MB_SYSTEMMODAL, "Menu", "ListView 2 context menu actioned")
    EndSwitch

    ; Look for flags
    If $fFlag_1 Then
        ; Clear flag
        $fFlag_1 = False
        ; Right click on LV_1
        ControlClick($hGUI, "", $cLV_1, "right")
    EndIf
    If $fFlag_2 Then
        $fFlag_2 = False
        ControlClick($hGUI, "", $cLV_2, "right")
    EndIf

WEnd

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    #forceref $hWnd, $iMsg, $wParam

    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    Local $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    Local $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $iCode
        Case $NM_RCLICK
            ; Check if a ListView has sent the message
            Switch $hWndFrom
                Case GUICtrlGetHandle($cLV_1)
                    ; Get item count
                    If _GUICtrlListView_GetItemCount($cLV_1) Then
                        If $mCM_1 = 0 Then
                            ; Create menu if required
                            $mCM_1 = GUICtrlCreateContextMenu( $cLV_1)
                            $mItem_1 = GUICtrlCreateMenuItem("Test 1", $mCM_1)
                            ; Set flag
                            $fFlag_1 = True
                        EndIf
                    Else
                        ; Destroy any existing menu
                        GUICtrlDelete($mCM_1)
                        ; Placeholders
                        $mCM_1 = 0
                        $mItem_1 = 9999
                    EndIf

                Case GUICtrlGetHandle($cLV_2)
                    If _GUICtrlListView_GetItemCount($cLV_2) Then
                        If $mCM_2 = 0 Then
                            $mCM_2 = GUICtrlCreateContextMenu( $cLV_2)
                            $mItem_2 = GUICtrlCreateMenuItem("Test 1", $mCM_2)
                            GUICtrlCreateMenuItem("text", $mItem_2)
                            $fFlag_2 = True
                        EndIf
                    Else
                        GUICtrlDelete($mCM_2)
                        $mCM_2 = 0
                        $mItem_2 = 9999
                    EndIf
            EndSwitch

    EndSwitch
    Return $GUI_RUNDEFMSG

EndFunc
Please ask if you have any questions. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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
×
×
  • Create New...