Jump to content
Sign in to follow this  
qwert

Possible to adjust the default context menu?

Recommended Posts

qwert

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.

post-29172-0-33787200-1409680725_thumb.p

Share this post


Link to post
Share on other sites
JLogan3o13

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

√-1 2^3 ∑ π, and it was delicious!

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
qwert

 

what is it you're trying to accomplish?

Simplicity for the end user.  There's not one entry in the highlighted area they would ever use.

Share this post


Link to post
Share on other sites
LarsJ

It's possible:

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

Opt( "MustDeclareVars", 1 )

Example()

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()
      Case $GUI_EVENT_SECONDARYDOWN
        Local $hMessageHandler = DllCallbackRegister( "MessageHandler", "long", "int;wparam;lparam" )
        Local $hMessageHook = _WinAPI_SetWindowsHookEx( $WH_MSGFILTER, DllCallbackGetPtr( $hMessageHandler ), 0, _WinAPI_GetCurrentThreadId() )

      Case $GUI_EVENT_SECONDARYUP
        _WinAPI_UnhookWindowsHookEx( $hMessageHook )
        DllCallbackFree( $hMessageHandler )

      Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
  WEnd

  GUIDelete()
EndFunc

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 )
    Next
  EndIf
EndFunc

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
  WEnd
  If $i = 3 Then Return 0
  Local $hContext = _SendMessage( $hChild, $MN_GETHMENU )
  If $hContext > 0 Then Return $hContext
  Return 0
EndFunc

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.

Share this post


Link to post
Share on other sites
qwert

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. 

post-29172-0-32218800-1409765002_thumb.p

Share this post


Link to post
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
Sign in to follow this  

×