Solved

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

Posted on 2002-07-08
4
267 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
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

840 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