Jump to content

WM_COMMAND and ListView/TreeView Items


Affe
 Share

Recommended Posts

Trying to use WM_COMMAND and WM_CONTEXTMENU in my GUI, and I'm having issue with the fact that WM_COMMAND doesn't fire when the mouse is over a listview or treeview.

You can try to simulate my issue with the example code below. The WM_COMMAND will fire whenever the menu is created over the GUI background, but not when the menu is over the listview.

#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)
    $hlistview = GUICtrlCreateListView("", 10, 10, 150, 280)
    GUISetState()

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

    ; 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

    $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

Can anyone help me out with this?

Thanks!

[center][/center]

Link to comment
Share on other sites

Try it using WM_NOTIFY for the listview.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I currently use WM_NOTIFY to set global elements to tell the WM_CONTEXTMENU which menu to build.

I wish I could attach my actual code, but it contains elements that are security sensitive (login information) and simply eliminating that information will yield a brick of code.

Here is my WM_NOTIFY (global variables set are $menu, $menuitem, and $menuitem_key):

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview, $hWndListView
    $hWndTreeview = $hTreeView
    If Not IsHWnd($hTreeView) Then $hWndTreeview = GUICtrlGetHandle($hTreeView)
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            $menu = "listview"
            Switch $iCode
                Case $LVN_COLUMNCLICK
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    Local $ColumnIndex = DllStructGetData($tInfo, "SubItem")
                    _ListView_Sort($ColumnIndex)
                    Return 0
                Case $NM_CLICK ; The user has clicked the right mouse button within the control
                    Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                    Local $text = _GUICtrlListView_GetItemText($hWndListView, DllStructGetData($tInfo, "Index"))
                    Local $hItem = _GUICtrlListView_GetItem($hWndListView, DllStructGetData($tInfo, "Index"))
                    $menu = "file"
                    $menuitem = $text
                    $menuitem_key = $_MF_File_Tree[_GUICtrlListView_GetItemParam($hWndListView, DllStructGetData($tInfo, "Index"))][0]
                    Return 0
                Case $NM_RCLICK ; The user has clicked the right mouse button within the control
                    Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                    Local $text = _GUICtrlListView_GetItemText($hWndListView, DllStructGetData($tInfo, "Index"))
                    Local $hItem = _GUICtrlListView_GetItem($hWndListView, DllStructGetData($tInfo, "Index"))
                    $menu = "file"
                    $menuitem = $text
                    $menuitem_key = $_MF_File_Tree[_GUICtrlListView_GetItemParam($hWndListView, DllStructGetData($tInfo, "Index"))][0]
                    Return 0
                EndSwitch

        Case $hWndTreeview
            $menu = "folder"
            Switch $iCode
                Case $NM_CLICK ; The user has clicked the left mouse button within the control
                    _DebugPrint("$NM_CLICK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                            "-->Code:" & @TAB & $iCode)
;~                Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
                Case $NM_DBLCLK ; The user has double-clicked the left mouse button within the control
                    _DebugPrint("$NM_DBLCLK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                            "-->Code:" & @TAB & $iCode)
;~                Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
                Case $NM_RCLICK ; The user has clicked the right mouse button within the control
                    _DebugPrint("$NM_RCLICK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                            "-->Code:" & @TAB & $iCode)
                    Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)

;~                Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
                Case $NM_RDBLCLK ; The user has clicked the right mouse button within the control
                    _DebugPrint("$NM_RDBLCLK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                            "-->Code:" & @TAB & $iCode)
;~                Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
                Case $TVN_SELCHANGEDW
;~                     Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                    $treeview_change = True
                    Return 0
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

So I suppose my question then is, how do I get WM_NOTIFY to realize that I clicked on the corresponding menu item?

Here is my WM_CONTEXTMENU in case it is needed for reference:

Func WM_CONTEXTMENU($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $ilParam
    Local $hMenu

    If $menu == "" or $menu == "none" Then Return True

    $hMenu = _GUICtrlMenu_CreatePopup(32)
    If $hMenu = 0 Then MsgBox(0, "Menu", "Error")

    Select
        Case $menu == "file"
            If $menuitem == "" Then
                _GUICtrlMenu_DestroyMenu($hMenu)
                Return True
            EndIf
            _GUICtrlMenu_InsertMenuItem($hMenu, 0, 'Preview "' & $menuitem & '"', $idPreview)
            _GUICtrlMenu_InsertMenuItem($hMenu, 1, 'Download "' & $menuitem & '"', $idDownload)
            _GUICtrlMenu_InsertMenuItem($hMenu, 2, "Edit Description", $idEditDescription)
            _GUICtrlMenu_InsertMenuItem($hMenu, 3, "Edit Tags", $idEditTags)
            _GUICtrlMenu_InsertMenuItem($hMenu, 4, "", 0)
            _GUICtrlMenu_InsertMenuItem($hMenu, 5, "Rename", $idRename)
            _GUICtrlMenu_InsertMenuItem($hMenu, 6, "Delete key " & $menuitem_key, $idDelete)
        Case $menu == "folder"
            _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Rename", $idRename)
            _GUICtrlMenu_InsertMenuItem($hMenu, 1, "Delete", $idDelete)
            _GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
            _GUICtrlMenu_InsertMenuItem($hMenu, 3, "Info", $idInfo)
        Case $menu == "listview"
            _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Details", $idViewDetails)
            _GUICtrlMenu_InsertMenuItem($hMenu, 1, "Large Icon", $idViewLgIcon)
            _GUICtrlMenu_InsertMenuItem($hMenu, 2, "List", $idViewList)
            _GUICtrlMenu_InsertMenuItem($hMenu, 3, "Small Icon", $idViewSmIcon)
            _GUICtrlMenu_InsertMenuItem($hMenu, 4, "Tile", $idViewTile)
        Case Else

    EndSelect
    _GUICtrlMenu_TrackPopupMenu($hMenu, $iwParam)
    _GUICtrlMenu_DestroyMenu($hMenu)
    Return True
EndFunc   ;==>WM_CONTEXTMENU
Edited by Affe

[center][/center]

Link to comment
Share on other sites

I always use WM_NOTIFY to monitor the right click id.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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