Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 106
Separate files macro - Errors on larger data set 18 56
lucky13 challenge 11 112
fizzArray  challenge 1 47
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now