Jump to content

[SOLVED] WM_CONTEXTMENU and ListView


Recommended Posts

Here's the code I have, pulled from the examples and a couple things added. I've been trying to implement something like this in one of my scripts with no luck. So I went to the basics and decided to just get this part working.

#include <GuiMenu.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global Enum $idOpen = 1000, $idSave, $idInfo

_Main()

Func _Main()
; Create GUI
GUICreate("Menu", 400, 300)
GUISetState()

; Register message handlers
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")
Global $listview = GUICtrlCreateListView( "test1|test2|test3|test4", 5, 5, 390, 290 )
Global $row1 = GUICtrlCreateListViewItem( "item1|item2|item3|item4", $listview )
Global $row2 = GUICtrlCreateListViewItem( "test1|test2|test3|test4", $listview )
; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc ;==>_Main

; Handle WM_COMMAND messages
Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $ilParam
Switch $iwParam
Case $idOpen
_WinAPI_ShowMsg("Open")
Case $idSave
_WinAPI_ShowMsg("Save")
Case $idInfo
_WinAPI_ShowMsg("Info")
EndSwitch
EndFunc ;==>WM_COMMAND

; Handle WM_CONTEXTMENU messages
Func WM_CONTEXTMENU($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $ilParam
Local $hMenu
Local $selected = GUICtrlRead( GUICtrlRead( $listview ) )
If $selected = 0 Then Return 1
$hMenu = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($hMenu, 0, "Open", $idOpen)
_GUICtrlMenu_InsertMenuItem($hMenu, 1, "Save", $idSave)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "Info", $idInfo)
_GUICtrlMenu_TrackPopupMenu($hMenu, $iwParam)
_GUICtrlMenu_DestroyMenu($hMenu)
Return True
EndFunc ;==>WM_CONTEXTMENU

I added this to the main function:

Global $listview = GUICtrlCreateListView( "test1|test2|test3|test4", 5, 5, 390, 290 )
Global $row1 = GUICtrlCreateListViewItem( "item1|item2|item3|item4", $listview )
Global $row2 = GUICtrlCreateListViewItem( "test1|test2|test3|test4", $listview )

and added this to wm_contextmenu:

Local $selected = GUICtrlRead( GUICtrlRead( $listview ) )
If $selected = 0 Then Return 1

The rest is pulled straight from the example script for "_GUICtrlMenu_CreatePopup()"

Basically I only want the menu to show up if something is selected inside the listview.

The problem is that it won't show up at all when I have the coded added to WM_CONTEXTMENU. I checked $selected with a traytip and it shows the info correctly.. and if nothing is selected it always displays 0. I'm not sure how to do this.

Edited by caleb41610
Link to comment
Share on other sites

Hi,

Maybe this? :huh:

#include <GuiMenu.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global Enum $idOpen = 1000, $idSave, $idInfo

Global $GUI, $listview, $hMenu

_Main()

Func _Main()
    ; Create GUI
    $GUI = GUICreate("Menu", 400, 300)

    $listview = GUICtrlCreateListView("test1|test2|test3|test4", 5, 5, 390, 290)
    GUICtrlCreateListViewItem("item1|item2|item3|item4", $listview)
    GUICtrlCreateListViewItem("test1|test2|test3|test4", $listview)

    $hMenu = _GUICtrlMenu_CreatePopup()
    _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Open", $idOpen)
    _GUICtrlMenu_InsertMenuItem($hMenu, 1, "Save", $idSave)
    _GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
    _GUICtrlMenu_InsertMenuItem($hMenu, 3, "Info", $idInfo)

    ; Register message handlers
    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

    GUISetState()

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc   ;==>_Main

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

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $iIDFrom
        Case $listview
            Switch $iCode
                Case $NM_CLICK, $NM_RCLICK
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)

                    If DllStructGetData($tInfo, "Item") > -1 Then
                        _GUICtrlMenu_TrackPopupMenu($hMenu, $GUI)
                    EndIf
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    Switch $iwParam
        Case $idOpen
            _WinAPI_ShowMsg("Open")
        Case $idSave
            _WinAPI_ShowMsg("Save")
        Case $idInfo
            _WinAPI_ShowMsg("Info")
    EndSwitch
EndFunc   ;==>WM_COMMAND

Br, FireFox.

Edited by FireFox
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...