Jump to content
Sign in to follow this  
ChrisL

_GUICtrlTreeView_Create and WM_CONTEXTMENU WM_COMMAND won't work together

Recommended Posts

ChrisL
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
#include <GuiMenu.au3>

Global $g_hTreeView
Global Enum $e_idOpen = 1000, $e_idSave, $e_idInfo

Example()

Func Example()
    Local $hGUI, $hItem
    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
    $hGUI = GUICreate("(UDF Created) TreeView Create", 400, 300)

    $g_hTreeView = _GUICtrlTreeView_Create($hGUI, 2, 2, 396, 268, $iStyle, $WS_EX_CLIENTEDGE)
    GUISetState(@SW_SHOW)

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")
    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

    _GUICtrlTreeView_BeginUpdate($g_hTreeView)
    For $x = 1 To Random(2, 10, 1)
        $hItem = _GUICtrlTreeView_Add($g_hTreeView, 0, StringFormat("[%02d] New Item", $x))
        For $y = 1 To Random(2, 10, 1)
            _GUICtrlTreeView_AddChild($g_hTreeView, $hItem, StringFormat("[%02d] New Child", $y))
        Next
    Next
    _GUICtrlTreeView_EndUpdate($g_hTreeView)

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>Example

Func WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam

    ;ConsoleWrite($GUI_RUNDEFMSG & @CRLF)
    ;Switch $wParam

    ;   Case $hTreeView

            Local $hMenu

            $hMenu = _GUICtrlMenu_CreatePopup()
            _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Open", $e_idOpen)
            _GUICtrlMenu_InsertMenuItem($hMenu, 1, "Save", $e_idSave)
            _GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
            _GUICtrlMenu_InsertMenuItem($hMenu, 3, "Info", $e_idInfo)
            _GUICtrlMenu_TrackPopupMenu($hMenu, $wParam)
            _GUICtrlMenu_DestroyMenu($hMenu)
            Return True

    ;EndSwitch

    Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_CONTEXTMENU

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam
    Switch $wParam
        Case $e_idOpen
             _DebugPrint("WM_COMMAND " & $wParam & " Open")
        Case $e_idSave
             _DebugPrint("WM_COMMAND " & $wParam & " Save")
        Case $e_idInfo
             _DebugPrint("WM_COMMAND " & $wParam & " Info")
    EndSwitch

    Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_COMMAND


Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
    $hWndTreeview = $g_hTreeView
    If Not IsHWnd($g_hTreeView) Then $hWndTreeview = GUICtrlGetHandle($g_hTreeView)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndTreeview
            Switch $iCode
                Case $NM_CLICK ; The user has clicked the left mouse button within the control
                    _DebugPrint("$NM_CLICK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->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" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func _DebugPrint($s_Text, $sLine = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @CRLF & _
            "+======================================================" & @CRLF & _
            "-->Line(" & StringFormat("%04d", $sLine) & "):" & @TAB & $s_Text & @CRLF & _
            "+======================================================" & @CRLF)
EndFunc   ;==>_DebugPrint

In the example code I have a treeview and a context/popup menu, I can't use a conventional context menu because I need to use the treeview UDF functions.

The WM_COMMAND never fires from the context/popup menu selection while there is a treeview using  _GuiCtrlTreeview_Create

If you comment out _GuiCtrlTreeview_Create line you will then see the right click selection works as expected and the debug is written to the console on selection.

Autoit Ver 3.3.14.2

Any ideas why this combination won't work together?

Thanks

 

Share this post


Link to post
Share on other sites
LarsJ

You may have noticed that WM_COMMAND messages are still handled correctly when you right-click in the small empty area below the TreeView. Only when you right-click in the TreeView area the messages are not handled correctly.

When you right-click in a window WM_COMMAND messages are always send to the same window. If you right-click in the GUI WM_COMMAND messages are send to the GUI. If you right-click in the TreeView WM_COMMAND messages are to send to the TreeView.

But GUIRegisterMsg is only able to catch messages which are send to the GUI. GUIRegisterMsg cannot catch messages which are send to the TreeView.

The solution is to subclass the TreeView. Details in the codebox. I've added 10 code lines.

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

#include <WinAPIShellEx.au3>

Global $g_hTreeView
Global Enum $e_idOpen = 1000, $e_idSave, $e_idInfo

Example()

Func Example()
    Local $hGUI, $hItem
    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
    $hGUI = GUICreate("(UDF Created) TreeView Create", 400, 300)

    $g_hTreeView = _GUICtrlTreeView_Create($hGUI, 2, 2, 396, 268, $iStyle, $WS_EX_CLIENTEDGE)
    GUISetState(@SW_SHOW)

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")
    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

    Local $pTreeViewMsgHandler = DllCallbackGetPtr( DllCallbackRegister( "TreeViewMsgHandler", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr" ) )
    _WinAPI_SetWindowSubclass( $g_hTreeView, $pTreeViewMsgHandler, 0, 0 ) ; $iSubclassId = 0, $pData = 0

    _GUICtrlTreeView_BeginUpdate($g_hTreeView)
    For $x = 1 To Random(2, 10, 1)
        $hItem = _GUICtrlTreeView_Add($g_hTreeView, 0, StringFormat("[%02d] New Item", $x))
        For $y = 1 To Random(2, 10, 1)
            _GUICtrlTreeView_AddChild($g_hTreeView, $hItem, StringFormat("[%02d] New Child", $y))
        Next
    Next
    _GUICtrlTreeView_EndUpdate($g_hTreeView)

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    _WinAPI_RemoveWindowSubclass( $g_hTreeView, $pTreeViewMsgHandler, 0 )
    GUIDelete()
EndFunc   ;==>Example

Func TreeViewMsgHandler( $hWnd, $iMsg, $wParam, $lParam, $iSubclassId, $pData )
  Switch $iMsg
    Case $WM_COMMAND
      Switch $wParam
          Case $e_idOpen
               _DebugPrint("WM_COMMAND " & $wParam & " Open")
          Case $e_idSave
               _DebugPrint("WM_COMMAND " & $wParam & " Save")
          Case $e_idInfo
               _DebugPrint("WM_COMMAND " & $wParam & " Info")
      EndSwitch
  EndSwitch
  ; Call next function in subclass chain (this forwards Windows messages to main GUI)
  Return DllCall( "comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam )[0]
EndFunc

Func WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam

    ;ConsoleWrite($GUI_RUNDEFMSG & @CRLF)
    ;Switch $wParam

    ;   Case $hTreeView

            Local $hMenu

            $hMenu = _GUICtrlMenu_CreatePopup()
            _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Open", $e_idOpen)
            _GUICtrlMenu_InsertMenuItem($hMenu, 1, "Save", $e_idSave)
            _GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
            _GUICtrlMenu_InsertMenuItem($hMenu, 3, "Info", $e_idInfo)
            _GUICtrlMenu_TrackPopupMenu($hMenu, $wParam)
            _GUICtrlMenu_DestroyMenu($hMenu)
            Return True

    ;EndSwitch

    Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_CONTEXTMENU

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam
    Switch $wParam
        Case $e_idOpen
             _DebugPrint("WM_COMMAND " & $wParam & " Open")
        Case $e_idSave
             _DebugPrint("WM_COMMAND " & $wParam & " Save")
        Case $e_idInfo
             _DebugPrint("WM_COMMAND " & $wParam & " Info")
    EndSwitch

    Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_COMMAND


Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
    $hWndTreeview = $g_hTreeView
    If Not IsHWnd($g_hTreeView) Then $hWndTreeview = GUICtrlGetHandle($g_hTreeView)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndTreeview
            Switch $iCode
                Case $NM_CLICK ; The user has clicked the left mouse button within the control
                    _DebugPrint("$NM_CLICK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->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" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func _DebugPrint($s_Text, $sLine = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @CRLF & _
            "+======================================================" & @CRLF & _
            "-->Line(" & StringFormat("%04d", $sLine) & "):" & @TAB & $s_Text & @CRLF & _
            "+======================================================" & @CRLF)
EndFunc   ;==>_DebugPrint

 

  • Like 2

Share this post


Link to post
Share on other sites
ChrisL

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  

  • Similar Content

    • Dalex
      By Dalex
      It has been a while since I last installed and used Autoit, I just installed the latest version along with Scite.  For some reason, I do not have the option to create new .au3 files when right clicking. I have tried rebooting, and running the installation as administrator as mentioned in other threads. no joy...
       
      OS = Windows 10 Enterprise version 1806
       
      Thanks in advance!

    • RooperGee
      By RooperGee
      I had written a GUI with several buttons and I wanted one of the buttons to show its context menu regardless of the user right or left clicking the button.  The whole purpose of the button was to show the user a menu of options and a context menu fit the need just fine, but I needed it to display with either click option.  I came up with the code below to accomplish this but I feel like I might be missing some very obvious easier way to do this and I feel it's a little lame to be forcing a right click mouse action on the button as a result of the user doing a left click, but it works!
      Anyone have a suggestion of a better way to do this?  Otherwise, if this idea helps you, here you go.
      #include <GUIConstantsEx.au3> LeftClickContextTest() Func LeftClickContextTest() Local $hGUI = GUICreate("My GUI", 200, 100) Local $button = GUICtrlCreateButton("Show Context", 15, 40, 85, 25) Local $close = GUICtrlCreateButton("Close", 110, 40, 85, 25) Local $context = GUICtrlCreateContextMenu($button) GUICtrlCreateMenuItem("Test Context Item 1", $context) GUICtrlCreateMenuItem("Test Context Item 2", $context) GUICtrlCreateMenuItem("Test Context Item 3", $context) GUISetState(@SW_SHOW, $hGUI) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $close ExitLoop Case $button MouseClick("right", MouseGetPos(0), MouseGetPos(1), 1, 0) EndSwitch WEnd EndFunc  
    • Luigi
      By Luigi
      Greetings forum,
      Hi develop this script, to use context menu with listview.
      It's work fine, run and click with left or right mouse button, you see on Console the id from item.
      If you click none, the id is -1, like this:
      $NM_RCLICK[-1] $NM_RCLICK[-1] $NM_RCLICK[2] $NM_RCLICK[1] $NM_RCLICK[0] $NM_RCLICK[1] That is correct.
      But if you uncoment the line:
      GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")
      You have to click with left button (over item or none) and after click with right button.
      LeftButton is to update $HOSTS_INDEX.
      RightButton is to open ContextMenu.
       
      Exist another way to update $HOSTS_INDEX?
      Example:
      Call WM_NOTIFY every time WM_CONTEXTMENU is called?
      I ask this, becouse i cant understanding why $HOSTS_INDEX is not updated when WM_CONTEXTMENU is uncomented.
      Someone can explain or help me?
       
      Best regards.
      ;~ #AutoIt3Wrapper_AU3Check_Parameters= -q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7 ;~ #Tidy_Parameters=/sf #include-once #include-once #include <Array.au3> #include <GUIConstantsEx.au3> #include <GuiListView.au3> #include <GuiMenu.au3> #include <WindowsConstants.au3> #include <WinAPI.au3> #include <SendMessage.au3> #include <Timers.au3> #include <AutoItConstants.au3> #include <EditConstants.au3> #include <File.au3> #include <FontConstants.au3> #include <GuiComboBoxEx.au3> #include <GuiImageList.au3> #include <GuiTreeView.au3> #include <ListViewConstants.au3> #include <StaticConstants.au3> #include <TreeViewConstants.au3> #include <String.au3> OnAutoItExitRegister("OnExit") Opt("GUIOnEventMode", 1) Opt("GUIEventOptions", 1) Opt("MustDeclareVars", 1) Global Enum $eCREATE = 1000, $eUPDATE, $eDELETE, $eICON_TABLE, $eDEFAULT, $eEXPORT_HTML Global Const $EMPTY = -1 Global $HOSTS_INDEX = -1 Global $HOST_Host Global $aGuiSize[2] = [800, 600] Global $sGuiTitle = "GuiTitle" Global $hGui Global $iList, $hList $hGui = GUICreate($sGuiTitle, $aGuiSize[0], $aGuiSize[1]) GUISetOnEvent($GUI_EVENT_CLOSE, "Quit") $iList = GUICtrlCreateListView("nome", 20, 50, 240, 490) $hList = GUICtrlGetHandle($iList) _GUICtrlListView_SetColumnWidth($hList, 0, 236) Populate() GUISetState(@SW_SHOW, $hGui) GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") ;~ GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU") While Sleep(25) WEnd Func OnExit() GUISetState($hGui, @SW_HIDE) GUIDelete($hGui) EndFunc ;==>OnExit Func Quit() Exit EndFunc ;==>Quit Func Populate() Local $arr[4] = [3, "nome 1", "nome 2", "nome 3"] _GUICtrlListView_BeginUpdate($hList) _GUICtrlListView_DeleteAllItems($hList) For $ii = 1 To $arr[0] _GUICtrlListView_AddItem($hList, $arr[$ii]) Next _GUICtrlListView_SetItemSelected($hList, 0, True, True) _GUICtrlListView_EndUpdate($hList) EndFunc ;==>Populate Func WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg, $wParam, $lParam ConsoleWrite("WM_CONTEXTMENU..( $hWnd=" & $hWnd & ", $iMsg=" & $iMsg & ", $wParam=" & $wParam & ", $lParam=" & $lParam & " )" & @LF) Local $exec = 0 Local $hMenu Switch $wParam Case $hList Local $TRY_ID = _GUICtrlListView_GetHotItem($hList) If Not ($TRY_ID = $HOSTS_INDEX) Then $HOSTS_INDEX = $TRY_ID Local $aOrigin = _GUICtrlListView_GetOrigin($hList) ConsoleWrite("WM_CONTEXTMENU $HOSTS_INDEX[" & $HOSTS_INDEX & "] $aOrigin[" & _GUICtrlListView_GetOriginX($iList) & "]" & @LF) $hMenu = _GUICtrlMenu_CreatePopup() If $HOSTS_INDEX = $EMPTY Then _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Add", $eCREATE) Else _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Rename", $eUPDATE) _GUICtrlMenu_InsertMenuItem($hMenu, 1, "Del", $eDELETE) EndIf _GUICtrlMenu_SetMenu($hGui, $hMenu) $exec = _GUICtrlMenu_TrackPopupMenu($hMenu, $wParam, -1, -1, 1, 1, 2, 1) _GUICtrlMenu_DestroyMenu($hMenu) EndSwitch Return True EndFunc ;==>WM_CONTEXTMENU Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg, $wParam, $lParam Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo $tNMHDR = DllStructCreate($tagNMHDR, $lParam) $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") $iCode = DllStructGetData($tNMHDR, "Code") Local $TRY_ID Switch $hWndFrom Case $hList $TRY_ID = _GUICtrlListView_GetHotItem($hList) If Not ($TRY_ID = $HOSTS_INDEX) Then $HOSTS_INDEX = $TRY_ID Switch $iCode Case $NM_CLICK ConsoleWrite("$NM_CLICK[" & $TRY_ID & "]" & @LF) If $HOSTS_INDEX = $EMPTY Or $HOST_Host Then ;~ HOSTS_Clear() ;~ HOSTS_ListView_ItemCancel() ;~ GUICtrlSetState($HOSTS_ITEM_DEL, $GUI_DISABLE) ;~ GUICtrlSetState($HOSTS_ITEM_UPD, $GUI_DISABLE) Else Local $name = _GUICtrlListView_GetItemText($hList, $HOSTS_INDEX) ;~ HOSTS_FieldLoad($name) ;~ GUICtrlSetState($HOSTS_ITEM_DEL, $GUI_ENABLE) ;~ GUICtrlSetState($HOSTS_ITEM_UPD, $GUI_ENABLE) EndIf Return 0 ; allow the default processing Case $NM_RCLICK ConsoleWrite("$NM_RCLICK[" & $TRY_ID & "]" & @LF) Return 0 ; allow the default processing EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_NOTIFY  
×