Sign in to follow this  
Followers 0
Achilles

Problem with GUICtrlCreateContextMenu on the move

4 posts in this topic

I have a list that six different types of icons. Depending on the icon, I want to show a different context menu. So, I decided the easiest way to do this would be to delete the menu each time and start from scratch after every right click. It doesn't work. Any ideas why the menu doesn't show?

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

Global $testMenu

$GUI = GUICreate("Menu problem", 400, 300)

$hListView = _GUICtrlListView_Create($GUI, "", 2, 2, 394, 268)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
$hListViewHdl = GUICtrlGetHandle($hListView)
GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

; Add columns
_GUICtrlListView_InsertColumn($hListView, 0, "Column 1", 100)

; Add items
_GUICtrlListView_AddItem($hListView, "Row 1: Col 1", 0)
_GUICtrlListView_AddItem($hListView, "Row 2: Col 1")
_GUICtrlListView_AddItem($hListView, "Row 3: Col 1")

; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()


Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $tInfo

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(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, $ilParam)
                    $iItem = DllStructGetData($tInfo, 'Item')
                    
                    GUICtrlDelete($testMenu)
                    $testMenu = GUICtrlCreateContextMenu($hListView)
                    GUICtrlCreateMenuItem('Testing', $testMenu)
                    
                   ;Return 1; not to allow the default processing
                    Return 0; allow the default processing

            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc  ;==>WM_NOTIFY

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites



@Ichigo

You can't create the context menu using the GUICtrlCreateContextMenu for the control created with UDF functions, e.g. _GUICtrlListView_Create

Example:

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

Global $testMenu

$GUI = GUICreate("Menu problem", 400, 300)

$hListView = _GUICtrlListView_Create($GUI, "", 2, 2, 394, 268)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))

; Add columns
_GUICtrlListView_InsertColumn($hListView, 0, "Column 1", 100)

; Add items
_GUICtrlListView_AddItem($hListView, "Row 1: Col 1", 0)
_GUICtrlListView_AddItem($hListView, "Row 2: Col 1")
_GUICtrlListView_AddItem($hListView, "Row 3: Col 1")

$cDummy = GUICtrlCreateDummy()

$testMenu = GUICtrlCreateContextMenu($cDummy)
$MenuItem = GUICtrlCreateMenuItem('Testing', $testMenu)

$hMenu = GUICtrlGetHandle($testMenu)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUISetState()

; Loop until user exits
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cDummy
            _TrackPopupMenu($hMenu, 0, MouseGetPos(0), MouseGetPos(1), $GUI)
    EndSwitch
WEnd

GUIDelete()

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $tInfo

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(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($tagNMLISTVIEW, $ilParam)
                    $iItem = DllStructGetData($tInfo, 'Item')
                    
                    If $iItem <> -1 Then
                        _GUICtrlMenu_SetItemText($hMenu, $MenuItem, _GUICtrlListView_GetItemText($hListView, $iItem), False)
                        GUICtrlSendToDummy($cDummy)
                    EndIf

                    ;Return 1; not to allow the default processing
                    Return 0; allow the default processing

            EndSwitch
    EndSwitch
    
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func _TrackPopupMenu($hMenu, $iFlags, $iX, $iY, $hWnd)
    Local $aRet = DllCall("User32.dll", "int", "TrackPopupMenu", _
                                                "hwnd", $hMenu, _
                                                "int", $iFlags, _
                                                "int", $iX, _
                                                "int", $iY, _
                                                "int", 0, _
                                                "hwnd", $hWnd, _
                                                "hwnd", 0)
EndFunc

Share this post


Link to post
Share on other sites

Another way:

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

$GUI = GUICreate("Menu problem", 400, 300)

$hListView = GUICtrlCreateListView("Items|SubItems", 2, 2, 394, 268)

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

$testMenu = GUICtrlCreateContextMenu($hListView)
$MenuItem = GUICtrlCreateMenuItem('Testing', $testMenu)

$hMenu = GUICtrlGetHandle($testMenu)

GUIRegisterMsg($WM_INITMENUPOPUP, "WM_INITMENUPOPUP")

GUISetState()

; Loop until user exits
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func WM_INITMENUPOPUP($hWnd, $Msg, $wParam, $lParam)
    Local $sText = GUICtrlRead(GUICtrlRead($hListView))
    
    If $sText = "0" Then
        GUICtrlSetData($MenuItem, "Test")
        GUICtrlSetState($MenuItem, $GUI_DISABLE)
    Else
        GUICtrlSetData($MenuItem, $sText)
        GUICtrlSetState($MenuItem, $GUI_ENABLE)
    EndIf
    
    Return $GUI_RUNDEFMSG
EndFunc

:)

Share this post


Link to post
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
Sign in to follow this  
Followers 0