Jump to content

Rich edit context menu?


Go to solution Solved by Mat,

Recommended Posts

I found this example on an edit control:

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

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

$hGUI = GUICreate("Test", 300, 200)

$Edit1 = GUICtrlCreateEdit("", 10, 10, 280, 150, BitOR($WS_HSCROLL, $WS_VSCROLL, $ES_MULTILINE))

$hMenu = _GUICtrlMenu_CreatePopup()

_GUICtrlMenu_AddMenuItem($hMenu, "Open", $idOpen)
_GUICtrlMenu_AddMenuItem($hMenu, "Save", $idSave)
_GUICtrlMenu_AddMenuItem($hMenu, "Info", $idInfo)

GUISetState()

$wProcHandle = DllCallbackRegister("_WindowProc", "ptr", "hwnd;uint;wparam;lparam")

$wProcOld = _WinAPI_SetWindowLong(GUICtrlGetHandle($Edit1), $GWL_WNDPROC, DllCallbackGetPtr($wProcHandle))

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

GUIDelete($hGui)
DllCallbackFree($wProcHandle)

Func _WindowProc($hWnd, $Msg, $wParam, $lParam)
    Switch $hWnd
        Case GUICtrlGetHandle($Edit1)
            Switch $Msg
                Case $WM_CONTEXTMENU
                    _GUICtrlMenu_TrackPopupMenu($hMenu, $wParam)
                    Return 0
                Case $WM_COMMAND
                    Switch $wParam
                        Case $idOpen
                            ConsoleWrite("-> Open" & @LF)
                        Case $idSave
                            ConsoleWrite("-> Save" & @LF)
                        Case $idInfo
                            ConsoleWrite("-> Info" & @LF)
                    EndSwitch
            EndSwitch
    EndSwitch
    
    Local $aRet = DllCall("user32.dll", "int", "CallWindowProc", "ptr", $wProcOld, _
                          "hwnd", $hWnd, "uint", $Msg, "wparam", $wParam, "lparam", $lParam)
    Return $aRet[0]
EndFunc

How could the above be done for a richedit control?

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Link to comment
Share on other sites

  • Solution

Easy, 99.9% of the code is the same. Change the edit to a rich edit, change anything using GUICtrlGetHandle($Edit1) to just use the handle, change the message handler to use WM_RBUTTONUP because richedits don't send WM_CONTEXTMENU messages I seem to remember. Because you change the message you can't use $wParam like you are above, you probably want $hWnd instead.

Give this a try:

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

#include <GUIRichEdit.au3>

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

$hGUI = GUICreate("Test", 300, 200)

$Edit1 = _GUICtrlRichEdit_Create($hGUI, "", 10, 10, 280, 150, BitOR($WS_HSCROLL, $WS_VSCROLL, $ES_MULTILINE))

$hMenu = _GUICtrlMenu_CreatePopup()

_GUICtrlMenu_AddMenuItem($hMenu, "Open", $idOpen)
_GUICtrlMenu_AddMenuItem($hMenu, "Save", $idSave)
_GUICtrlMenu_AddMenuItem($hMenu, "Info", $idInfo)

GUISetState()

$wProcHandle = DllCallbackRegister("_WindowProc", "ptr", "hwnd;uint;wparam;lparam")

$wProcOld = _WinAPI_SetWindowLong($Edit1, $GWL_WNDPROC, DllCallbackGetPtr($wProcHandle))

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd


GUIDelete($hGUI)
DllCallbackFree($wProcHandle)


Func _WindowProc($hWnd, $Msg, $wParam, $lParam)
    Switch $hWnd
        Case $Edit1
            Switch $Msg
                Case $WM_RBUTTONUP
                    _GUICtrlMenu_TrackPopupMenu($hMenu, $hWnd)
                    Return 0
                Case $WM_COMMAND
                    Switch $wParam
                        Case $idOpen
                            ConsoleWrite("-> Open" & @LF)
                        Case $idSave
                            ConsoleWrite("-> Save" & @LF)
                        Case $idInfo
                            ConsoleWrite("-> Info" & @LF)
                    EndSwitch
            EndSwitch
    EndSwitch

    Local $aRet = DllCall("user32.dll", "int", "CallWindowProc", "ptr", $wProcOld, _
            "hwnd", $hWnd, "uint", $Msg, "wparam", $wParam, "lparam", $lParam)

    Return $aRet[0]
EndFunc   ;==>_WindowProc
Link to comment
Share on other sites

Thanks Mat and James!(Or should it be James and Mat?)

Perfect, exactly what I was looking for.

Now I just need to tackle the HTML formatted text to rich edit and back...

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Link to comment
Share on other sites

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

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