Jump to content

WM_CONTEXTMENU custom for controls does not work


Recommended Posts

Greetings,

I try work with WM_CONTEXT, I take from help's example, and modify to generate a custom context menu to each control (ListView, TreeView or another control).

If you leave with default/generic it work.

If you custom for a specific control, does not work, what is my erro?

When I say work, I want call the Switch...Case from WM_COMMAND.

This is the code:

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

OnAutoItExitRegister("OnExit")

Opt("GUIOnEventMode", 1)
Opt("GUIEventOptions", 1)
Opt("MustDeclareVars", 1)

Global Enum $eOPEN = 1000, $eSAVE, $eINFO

Global $hGui = GUICreate("Menu", 400, 300)
ConsoleWrite("$hGui........[" & $hGui & "]" & @LF)
Global $iListView1 = GUICtrlCreateListView("", 10, 10, 80, 120)
Global $hListView1 = GUICtrlGetHandle($iListView1)
ConsoleWrite("$iTreeView1..[" & $iListView1 & "] $hListView1..[" & $hListView1 & "]" & @LF)
Global $iListView2 = GUICtrlCreateListView("", 100, 10, 80, 120)
Global $hListView2 = GUICtrlGetHandle($iListView2)
ConsoleWrite("$iTreeView1..[" & $iListView2 & "] $hListView2..[" & $hListView2 & "]" & @LF)
Global $iTreeView1 = GUICtrlCreateTreeView(190, 10, 80, 120)
Global $hTreeView1 = GUICtrlGetHandle($iTreeView1)
ConsoleWrite("$iTreeView1..[" & $iTreeView1 & "] $hTreeView1..[" & $hTreeView1 & "]" & @LF)

GUISetOnEvent($GUI_EVENT_CLOSE, "Quit", $hGui)
GUISetState(@SW_SHOW, $hGui)

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

While Sleep(25)
WEnd

Func Quit()
    Exit
EndFunc   ;==>Quit

Func OnExit()
    GUIDelete($hGui)
EndFunc   ;==>OnExit

; Handle WM_COMMAND messages
Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    ConsoleWrite("WM_CONTEXTMENU( $hWnd=" & $hWnd & ", $iMsg=" & $iMsg & ", $wParam=" & $wParam & ", $lParam=" & $lParam & " )" & @LF)
    Switch $wParam
        Case $eOPEN
            ConsoleWrite("Open" & @LF)
        Case $eSAVE
            ConsoleWrite("Save" & @LF)
        Case $eINFO
            ConsoleWrite("Info" & @LF)
    EndSwitch
EndFunc   ;==>WM_COMMAND

; Handle WM_CONTEXTMENU messages
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 $hMenu = _GUICtrlMenu_CreatePopup()
    _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Generic Open", $eOPEN)
    _GUICtrlMenu_InsertMenuItem($hMenu, 1, "Generic Save", $eSAVE)
    _GUICtrlMenu_InsertMenuItem($hMenu, 2, "Generic Info", $eINFO)
    _GUICtrlMenu_TrackPopupMenu($hMenu, $wParam)
    _GUICtrlMenu_DestroyMenu($hMenu)
    Return True


    ; does not work
    #cs
        #forceref $hWnd, $iMsg, $wParam, $lParam
        ConsoleWrite("WM_CONTEXTMENU( $hWnd=" & $hWnd & ", $iMsg=" & $iMsg & ", $wParam=" & $wParam & ", $lParam=" & $lParam & " )" & @LF)
        Local $hMenu = _GUICtrlMenu_CreatePopup()
        Switch $wParam
        Case $hListView1
        _GUICtrlMenu_InsertMenuItem($hMenu, 0, "$hListView1 Open", $eOPEN)
        _GUICtrlMenu_InsertMenuItem($hMenu, 1, "$hListView1 Save", $eSAVE)
        _GUICtrlMenu_InsertMenuItem($hMenu, 2, "$hListView1 Info", $eINFO)
        Case $hListView2
        _GUICtrlMenu_InsertMenuItem($hMenu, 0, "$hListView2 Open", $eOPEN)
        _GUICtrlMenu_InsertMenuItem($hMenu, 1, "$hListView2 Save", $eSAVE)
        _GUICtrlMenu_InsertMenuItem($hMenu, 2, "$hListView2 Info", $eINFO)
        Case $hTreeView1
        _GUICtrlMenu_InsertMenuItem($hMenu, 0, "$hTreeView1 Open", $eOPEN)
        _GUICtrlMenu_InsertMenuItem($hMenu, 1, "$hTreeView1 Save", $eSAVE)
        _GUICtrlMenu_InsertMenuItem($hMenu, 2, "$hTreeView1 Info", $eINFO)
        EndSwitch

        _GUICtrlMenu_TrackPopupMenu($hMenu, $wParam)
        _GUICtrlMenu_DestroyMenu($hMenu)
        Return True
    #ce
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")
EndFunc   ;==>WM_NOTIFY

 

Edited by Luigi

Visit my repository

Link to comment
Share on other sites

I found this way... Is the better?

It not fire WM_COMMAND or WM_MENUCOMMAND, it use only WM_CONTEXTMENU

 

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

OnAutoItExitRegister("OnExit")

Opt("GUIOnEventMode", 1)
Opt("GUIEventOptions", 1)
Opt("MustDeclareVars", 1)

Global Enum $eOPEN = 1000, $eSAVE, $eINFO
Global $try
Global $hGui = GUICreate("Menu", 400, 300)
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit", $hGui)
Global $iListView = GUICtrlCreateListView("", 10, 10, 80, 120)
Global $hListView = GUICtrlGetHandle($iListView)

Global $iTreeView = GUICtrlCreateTreeView(100, 10, 80, 120)
Global $hTreeView = GUICtrlGetHandle($iTreeView)

ConsoleWrite("$hGui[" & $hGui & "] $iListView[" & $iListView & "] $hListView[" & $hListView & "] $iTreeView[" & $iTreeView & "] $hTreeView[" & $hTreeView & "]" & @LF)

;~ GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
;~ GUIRegisterMsg($WM_MENUCOMMAND, "WM_MENUCOMMAND")
GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")

GUISetState(@SW_SHOW, $hGui)

While Sleep(10)
WEnd

;~ Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
;~  #forceref $hWnd, $iMsg, $wParam, $lParam
;~  ConsoleWrite("WM_COMMAND.....( $hWnd=" & $hWnd & ", $iMsg=" & $iMsg & ", $wParam=" & $wParam & ", $lParam=" & $lParam & " )" & @LF)
;~  Switch $hWnd
;~      Case $hGui
;~          Switch $wParam
;~              Case $eOPEN
;~                  ConsoleWrite("Open" & @LF)
;~              Case $eSAVE
;~                  ConsoleWrite("Save" & @LF)
;~              Case $eINFO
;~                  ConsoleWrite("Open" & @LF)
;~          EndSwitch
;~  EndSwitch
;~  Return $GUI_RUNDEFMSG
;~ EndFunc   ;==>WM_COMMAND

;~ Func WM_MENUCOMMAND($hWnd, $iMsg, $wParam, $lParam)
;~  #forceref $hWnd, $iMsg, $wParam, $lParam
;~  ConsoleWrite("WM_MENUCOMMAND..( $hWnd=" & $hWnd & ", $iMsg=" & $iMsg & ", $wParam=" & $wParam & ", $lParam=" & $lParam & " )" & @LF)
;~  ConsoleWrite(_GUICtrlMenu_GetItemText($lParam, $wParam) & @CRLF)

;~  Switch _WinAPI_LoWord($wParam)
;~      Case $eOPEN
;~          ConsoleWrite("Open" & @LF)
;~      Case $eSAVE
;~          ConsoleWrite("Save" & @LF)
;~      Case $eINFO
;~          ConsoleWrite("Open" & @LF)
;~  EndSwitch

;~  Return $GUI_RUNDEFMSG
;~ EndFunc   ;==>WM_MENUCOMMAND

; Handle WM_CONTEXTMENU messages
Func WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    If Not ($hWnd = $hGui) Then Return
    ConsoleWrite("WM_CONTEXTMENU..( $hWnd=" & $hWnd & ", $iMsg=" & $iMsg & ", $wParam=" & $wParam & ", $lParam=" & $lParam & " )" & @LF)
    Local $hMenu = _GUICtrlMenu_CreatePopup()

    Local $label
    Switch $wParam
        Case $hListView
            $label = "ListView"
            _GUICtrlMenu_InsertMenuItem($hMenu, 0, $label & " Open", $eOPEN)
            _GUICtrlMenu_InsertMenuItem($hMenu, 1, $label & " Save", $eSAVE)
            _GUICtrlMenu_InsertMenuItem($hMenu, 2, $label & " Info", $eINFO)
        Case $hTreeView
            $label = "TreeView"
            _GUICtrlMenu_InsertMenuItem($hMenu, 0, $label & " Open", $eOPEN)
            _GUICtrlMenu_InsertMenuItem($hMenu, 1, $label & " Save", $eSAVE)
            _GUICtrlMenu_InsertMenuItem($hMenu, 2, $label & " Info", $eINFO)
    EndSwitch
    _GUICtrlMenu_SetMenu($hGui, $hMenu)

    $try = _GUICtrlMenu_TrackPopupMenu($hMenu, $wParam, -1, -1, 1, 1, 2, 1)
    ConsoleWrite("@line[" & @ScriptLineNumber & "] @error[" & @error & "] $try[" & $try & "]" & @LF)

    Switch $try
        Case $eOPEN
            ConsoleWrite("Execute " & $label & " Open" & @LF)
        Case $eSAVE
            ConsoleWrite("Execute " & $label & " Save" & @LF)
        Case $eINFO
            ConsoleWrite("Execute " & $label & " Open" & @LF)
        Case Else
            ConsoleWrite("Execute " & $label & " None" & @LF)
    EndSwitch

    _GUICtrlMenu_DestroyMenu($hMenu)
    Return True
EndFunc   ;==>WM_CONTEXTMENU

Func Quit()
    Exit
EndFunc   ;==>Quit

Func OnExit()
    GUIDelete($hGui)
EndFunc   ;==>OnExit

 

Edited by Luigi

Visit my repository

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