Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Title # Comments Views Activity
iSeries DB2 Query 2 100
twoTwo  challenge 35 118
Is there a simple front-end menu system. 9 141
Example code 13 34
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 …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

740 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