Jump to content

Possible to adjust the default context menu?


Recommended Posts

I’ve been on the search for how to simplify the default context menu for edit controls (see example below).  I've read dozens of posts, which address all sorts of special cases.  Indeed, there's a good one that shows how to implement a completely custom menu:

But all I want to do is drop some of the standard choices.  So, short of the full custom approach, is there a way to disable some of the standard context menu entries?


(BTW, I've never noticed these "standard entries" on any other application.  Are the standard only for AU3?)


Thanks in advance for any advice.


Link to comment
Share on other sites

  • Moderators

My first question would be, why do you need to remove them, rather than just not using them if you don't need them? In the end, what is it you're trying to accomplish?

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

It's possible:

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

Opt( "MustDeclareVars", 1 )


Func Example()
  Local $hGui = GUICreate( "Context menu in edit control" )
  GUISetState( @SW_SHOW )

  Local $idEdit = GUICtrlCreateEdit( "", 176, 32, 121, 97, $ES_AUTOVSCROLL + $WS_VSCROLL )
  GUICtrlSetData( $idEdit, "First line" & @CRLF )
  Send( "{END}" )
  GUICtrlSetData( $idEdit, "Second line", 1 )

  While 1
    Switch GUIGetMsg()
        Local $hMessageHandler = DllCallbackRegister( "MessageHandler", "long", "int;wparam;lparam" )
        Local $hMessageHook = _WinAPI_SetWindowsHookEx( $WH_MSGFILTER, DllCallbackGetPtr( $hMessageHandler ), 0, _WinAPI_GetCurrentThreadId() )

        _WinAPI_UnhookWindowsHookEx( $hMessageHook )
        DllCallbackFree( $hMessageHandler )



Func MessageHandler( $nCode, $wParam, $lParam )
  Local Static $hContextPrev = 0
  Local $hContext = GetContextMenuHandle()
  If $hContext And $hContextPrev <> $hContext Then
    $hContextPrev = $hContext
    For $i = 14 To 8 Step -1
      ;_GUICtrlMenu_DeleteMenu( $hContext, $i )
      _GUICtrlMenu_SetItemGrayed( $hContext, $i )

Func GetContextMenuHandle()
  Local $hDesktop = _WinAPI_GetDesktopWindow(), $i = 0
  Local $hChild = _WinAPI_GetWindow( $hDesktop, $GW_CHILD )
  While $hChild And $i < 3
    If _WinAPI_GetClassName( $hChild ) = "#32768" Then ExitLoop
    $hChild = _WinAPI_GetWindow( $hChild, $GW_HWNDNEXT )
    $i += 1
  If $i = 3 Then Return 0
  Local $hContext = _SendMessage( $hChild, $MN_GETHMENU )
  If $hContext > 0 Then Return $hContext
  Return 0

The AutoIt edit control is based on a MicroSoft edit control. All other applications using the MicroSoft edit control, will also get this context menu on right click.

Link to comment
Share on other sites

LarsJ, thanks for your solution.  Although it's more involved than I hoped was needed, it provides the right result.  The only drawback is that the menu downsizing is visible for an instant ... at least on my Win7 test PC.

Regarding its effect on other applications, I wasn't able to produce an example of that.  The search field in AutoIt Help, for example, continues to show all entries while the test script operates.

Given this as a viable alternative, I'll have to weigh against using a custom menu.  The downside of custom is having to locally handle the available features.

I also want to mention that your script is a clear and concise example of DllCallback and MessageHook processing, which is too often shown surrounded by other calls and variable settings which tend to disguise the fundamental operations.

I appreciate your help with this. 


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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...