0

I am currently trying to subclass an Edit Control, in particular the subject of en Email in the Outlook Client. This control is of class RichEdit20WPT.

I get a wndProc by using the following WINAPI methods.

<DllImport("ComCtl32.dll", CharSet:=CharSet.Auto)> _
    Public Shared Function SetWindowSubclass(hWnd As IntPtr, newProc As Win32SubClassProc, uIdSubclass As IntPtr, dwRefData As IntPtr) As Integer
    End Function

 <DllImport("comctl32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Shared Function DefSubclassProc(ByVal hWnd As IntPtr, ByVal uMsg As IntPtr, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
    End Function

    Public Delegate Function Win32SubClassProc(hWnd As IntPtr, Msg As IntPtr, wParam As IntPtr, lParam As IntPtr, uIdSubclass As IntPtr, dwRefData As IntPtr) As Integer

And would then have a wndProc like this;

Private WM_CONTEXTMENU As IntPtr = &H7B

Private Function SubClassProc(hWnd As IntPtr, Msg As IntPtr, wParam As IntPtr, lParam As IntPtr, uIdSubclass As IntPtr, dwRefData As IntPtr) As Integer

        Select Case Msg

            Case WM_DESTROY                   

            Case WM_NCDESTROY

            Case WM_LBUTTONDOWN

            Case WM_CONTEXTMENU   'NEVER HAPPENS

            Case WM_RBUTTONDOWN

        End Select

        Return DefSubclassProc(hWnd, Msg, wParam, lParam)
    End Function

I get the L and R button down and up messages but no WM_CONTEXTMENU. My current intention is to add a menu item to the context menu so as an alternative I am using the WM_RBUTTONDOWN message.

Is this control special and known to not show the WM_CONTEXTMENU message?

I also have a challenge to find the messages that occur after choosing something on the context menu. My understanding is that the messages of the menu item clicked in the context menu are given to the parent which in this case is this RichEdit20WPT window. Is this correct? Note I am not making my own context menu I am appending to the existing one so I am not changing the owner of the context menu or anything like that.

darbid
  • 2,545
  • 23
  • 55
  • 1
    You bet there is something special about it! This is not a standard Windows control. The standard rich text box control has the class name `RichEdit20`. The `WPT` suffix is something added by the Outlook team to denote their custom implementation. – Cody Gray - on strike Dec 08 '16 at 12:56
  • My assumption then will be to say that a context menu will appear after a right mouse click message. Other than the fact that it is not a WM_CONTEXTMENU message this assumption seems ok for me. – darbid Dec 08 '16 at 13:32
  • Does your code execute in the process that created the window. – David Heffernan Dec 08 '16 at 13:37
  • Yes - for the SetWindowSubclass / wndProc to work you must be in the same process. – darbid Dec 08 '16 at 13:43
  • So how are you executing VB.net code inside the Outlook client? – David Heffernan Dec 08 '16 at 14:03
  • 3
    `WM_CONTEXTMENU` is one of those backwards-compatibility window messages that is not part of the standard window lifetime, but rather is generated by `DefWindowProc()` if you give it certain messages. In this case, `WM_CONTEXTMENU` is only generated if `WM_RBUTTONDOWN` is given to `DefWindowProc()`, and it's likely that the Outlook authors never pass that to `DefWindowProc()` and instead handle context menu logic themselves in `WM_RBUTTONDOWN`. – andlabs Dec 08 '16 at 14:33
  • David - From a VSTO Addin. andlabs - I am assuming this too. – darbid Dec 08 '16 at 15:04
  • 1
    "*My understanding is that the messages of the menu item clicked in the context menu are given to the parent which in this case is this RichEdit20WPT window. Is this correct?*" - When an app displays a popup menu, it specifies an `HWND` to receive menu messages. That IS NOT required to be the RichEdit itself. For instance, some 3rd party frameworks use dedicated message windows instead. We don't know what Outlook uses. Easiest way to know if the RichEdit receives menu messages is to check whether your subclass is receiving them. If not, you may have to hook `TrackPopupMenu/Ex()` directly. – Remy Lebeau Dec 08 '16 at 17:35

1 Answers1

0

Thank you to all the comments which helped me to at least keep searching for answers or in this case messages. For anyone coming here and wanting to add to the context menu of an Outlook Menu.

First here are two good links which explain generally what to do. How to disable copy/paste commands in the Windows edit control context menu? Modify right-click context menu in standard controls Anyone reading them can assume for a standard edit control such as a text box on a windows form application that the messages will be sent.

For Outlook (At least 2007 / 2010) this is what I have found;

  1. The text box you need to find for both an Explorer and Inspector is RichEdit20WPT
  2. This window however does not get two of the key messages required. (a) it does not get WM_INITMENUPOPUP to know before the context menu is being shown and secondly (b) it does not get a message when you choose something in the context menu which is a WM_COMMAND in this case.
  3. Inorder to amend the context menu you need to subclass the subject text box's parent which is a window of class #32770.
  4. As the parent is subclassed there are a few challenges. To know when our target text box as had a right click from the #32770 window you need to look for the WM_SETCURSOR.

Something like this where the wParam will be the text box's Hwnd and the HiWord will be the mouse message;

Case NativeMethodsEX.WM_SETCURSOR
    If wParam = subjectHwnd Then
        Dim pMap As New NativeMethodsEX.LParamMap(lParam)
    If pMap.hiword = NativeMethodsEX.WM_RBUTTONUP Then
        rightClickOnSubject = True
    Else
        rightClickOnSubject = False
    End If
    End If

Then shortly after there will be this message

Case NativeMethodsEX.WM_INITMENUPOPUP
    If rightClickOnSubject Then
        'check here if you want to display something.
    End If

Once you know this, you can implement the ideas from the other forum answers.

i_saw_drones
  • 3,486
  • 1
  • 31
  • 50
darbid
  • 2,545
  • 23
  • 55