Solved

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

Posted on 2002-07-08
4
272 Views
Last Modified: 2013-11-20
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);
}
0
Comment
Question by:jjjkkklll
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 4

Expert Comment

by:mblat
ID: 7138952
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..
0
 

Expert Comment

by:shahzadakhtar
ID: 7139590
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
0
 
LVL 12

Accepted Solution

by:
migel earned 100 total points
ID: 7139858
Hi!
you have to use special EDIT control message to manipulate edit caret
EM_SETSEL
EM_CHARFROMPOS
here is example :

assume your RichEdit attached to the CRichEdit class object m_wndEdit;
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;
               SetRichEditCaret(point);
              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);
}


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);
m_wndEdit.SetSel(lCharacter, lCharacter);
}
0
 

Author Comment

by:jjjkkklll
ID: 7143771
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);
}
0

Featured Post

 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question