Link to home
Start Free TrialLog in
Avatar of jjjkkklll
jjjkkklll

asked on

Moving cursor to location of right-click popup menu...

Hi,
I have a rich edit control and I set up a right-click menu for editing.  My question is: how can I code it so when the user right-clicks in the middle of a paragraph, the cursor moves to that location to do the pasting.  In other words, in, say, MS Word, when the user right-clicks in a paragraph, the insertion point moves to the location clicked on.  As my code works now, the menu pops up at the mouse location but when the user clicks on "Paste" it pastes it wherever the cursor was.  How can I put the cursor where the user right-clicks?  Thanks.

BOOL CCharacterPad::PreTranslateMessage(MSG* pMsg)
{
     if(pMsg->hwnd != NULL && pMsg->message == WM_RBUTTONDOWN)
     {
          HWND hWnd = pMsg->hwnd;
          TCHAR szClassName[6];
          ::GetClassName(hWnd, szClassName, 6);
          if (_tcsicmp(szClassName, _T("RICHE")) == 0)
          {
          ::SetFocus(pMsg->hwnd);
               CMenu menu;
               CPoint point = pMsg->pt;
               menu.CreatePopupMenu();
               menu.AppendMenu(MF_STRING, ID_EDIT_CUT, _T("Cu&t"));
               menu.AppendMenu(MF_STRING, ID_EDIT_COPY, _T("&Copy"));
               menu.AppendMenu(MF_STRING, ID_EDIT_PASTE, _T("&Paste"));
               menu.AppendMenu(MF_STRING, ID_EDIT_CLEAR, _T("&Delete"));
               menu.AppendMenu(MF_SEPARATOR);
               menu.AppendMenu(MF_STRING, ID_EDIT_SELECTALL, _T("Select &All"));
               //ClientToScreen( & point );
               menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON, point.x, point.y, this);
               menu.DestroyMenu();
          }
     }
     return CDialog::PreTranslateMessage(pMsg);
}
Avatar of mblat
mblat

It is probably because there is diffrence between caret and cursor. Text is inserted at caret position ( if I am not mistaken)
Try to use SetCaretPos and SetCursorPos and see what happend.


Hope it helps..
That indeed is the reason. To get the pasting done at the position of mouse click (cursor) you need to set the caret position using ::SetCaretPos() API (assuming the window in which you have clicked already owns the caret). If you are using edit box then you don't need to bother about caret ownig and releasing since it is automatically handled by the system.

Else you need to Create and Destroy the caret yourself according to the window having keyboard focus.

  See CreateCaret()
      SetCaretPos()
      ShowCaret()
      DestroyCaret()

SetCaretPos moves the caret even if it is hidden.

[Shahzad]

-------------------------------------------------
Shahzad Akhtar          Tech Lead
Mindfire Solutions      www.mindfiresolutions.com
--Reliable Off-shore Software development--
C/C++, Win32/MFC, VC++, CBuilder, CodeWarrior
ASKER CERTIFIED SOLUTION
Avatar of migel
migel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jjjkkklll

ASKER

That works with some minor tweaks.  When the user selects a block of text, the program has to differentiate between clicking on the block of text and clicking outside the block of text.  Say if the user needs to copy a block of text, without my modifications, the program selects the cursor position under the mouse click and eliminates the block, making it impossible to copy a block of text.  here's my final code in the SetRichEditCaret function.  Thanks.

BOOL CCharacterPad::SetRichEditCaret(CPoint point)
{
 m_wndEdit.ScreenToClient(&point);
 POINTL ptl = {point.x, point.y};
LONG lCharacter = m_wndEdit.SendMessage(EM_CHARFROMPOS, 0, (LPARAM)&ptl);

LONG start, end;
m_ wndEdit.GetSel(start, end);
if(0 == (start - end))
     m_ wndEdit.SetSel(lCharacter, lCharacter);
else if (lCharacter <= start || lCharacter >= end)
     m_ wndEdit.SetSel(lCharacter, lCharacter);
}