I am trying to create a context menu that pops up when you right click a RichEdit. I know how to create the context menu, but I don't know how to check for when the user right-clicks RichEdit.

This is what I have:

case WM_CREATE:
             {
                 // Create RichEdit Control
                 // SetEventMask to ENM_MOUSEEVENTS
             }
             break;
        case WM_NOTIFY:
             {
                 if(((LPNMHDR)lParam)->code == EN_MSGFILTER && ((LPNMHDR)lParam)->idFrom == ID_CTRL_RTB)
                 {
                     if(((MSGFILTER*)LOWORD(lParam))->msg == WM_RBUTTONDOWN) // Im tried NM_RDOWN, but MSDN says it's not supported
                     {
                         MsgBox("You right clicked it", "info", MB_OK); // replace with context menu
                     }
                 }
             }
             break;

The thing is that I can't get the WPARAM and LPARAM from the MSGFILTER structure from EN_MSGFILTER. Is there a more simple way to track mouse clicks?

I have done the similar thing in MFC ,
we can specify the right click option while tool tip
creation only .

OnContextMenu(CWnd* pWnd, CPoint point)
{
    VERIFY(mMainMenu.CreatePopupMenu());
    // Adding menu item
    mMainMenu.AppendMenu(MF_ENABLED,IDD_UPDATE,"UPDATE");
    .
    .
    .
    mMainMenu.TrackPopupMenu(TPM_RIGHTBUTTON ,point.x,point.y,this);
    mMainMenu.DestroyMenu();
}

What we can do is to get the Crect [ cpoint ] of richedit box on the dialog,
compare with the acutal piont then display the menu.
But i havent worked with win32 this is its done in MFC, hope this will help u.

The following should work ...

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  switch (uMsg)
  {
  case WM_CREATE:

  // ... create the richedit window here -> hWndRichEdit  
  // set event mask ...
  SendMessage(hWndRichEdit, EM_SETEVENTMASK, (WPARAM)0 (LPARAM)ENM_MOUSEEVENTS);
  break;

  case WM_NOTIFY:
  {
    const MSGFILTER * pF = (MSGFILTER *)lParam;

    if(pF->nmhdr.hWndFrom == hWndRichEdit)
    {
      if(pF->msg == WM_RBUTTONDOWN)
      {
        // got WM_RBUTTONDOWN from the richedit window
      }
    }
  }
  break;

  }

  return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

The following should work ...

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  switch (uMsg)
  {
  case WM_CREATE:

  // ... create the richedit window here -> hWndRichEdit  
  // set event mask ...
  SendMessage(hWndRichEdit, EM_SETEVENTMASK, (WPARAM)0 (LPARAM)ENM_MOUSEEVENTS);
  break;

  case WM_NOTIFY:
  {
    const MSGFILTER * pF = (MSGFILTER *)lParam;

    if(pF->nmhdr.hWndFrom == hWndRichEdit)
    {
      if(pF->msg == WM_RBUTTONDOWN)
      {
        // got WM_RBUTTONDOWN from the richedit window
      }
    }
  }
  break;

  }

  return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

Thank you, thank you, thank you, thank you, THANK YOU!!! I have been busting my hump trying to figure this out. Thanks for the help. :)

This article has been dead for over six months. Start a new discussion instead.