CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    [RESOLVED] How to make sure that only text could be dragged into a rich edit?

    I've found many examples on how to make rich edit control to accept bitmaps but I can't seem to find any way how to limit everything that can be dragged into a rich edit control to a simple text?

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: How to make sure that only text could be dragged into a rich edit?

    I cut this directly from working source code in a project of mine. The first snippit here overrides OnKeyDown to check for CTRL-V. In my project, mouse editing is disabled so you'll have to deal with that yourself. I've snipped a bunch of stuff from this that deals with syntax highlighting so you may want to combine both in one override:

    Code:
    void CRichSyntaxCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
      if(nChar==0x56){  //"V" key
        if (GetKeyState(VK_CONTROL) & 0x8000){  //see if it's CTRL-V
          //Intercept CTRL-V so we can paste only CF_TEXT or CF_UNICODETEXT
          SetRedraw(0);
          EditPaste();
          SetRedraw(1);
          return;
        }
      }
      CRichEditCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
    }
    This function pastes only UNICODE or TEXT on the clipboard and ignores anything else:

    Code:
    int CRichSyntaxCtrl::EditPaste()
    {
    	COleDataObject dataObject;
    	dataObject.AttachClipboard();
      HGLOBAL hmem;
      wchar_t *p;
      char *p1;
      CString cs;
    
      if (dataObject.IsDataAvailable(CF_UNICODETEXT))
    	{
    	  // get file refering to clipboard data
        hmem=dataObject.GetGlobalData(CF_UNICODETEXT);
        p=(wchar_t *)GlobalLock(hmem);
        if(p){
          ReplaceSel(p,1);
        }
    
        GlobalUnlock(hmem);
      }else if (dataObject.IsDataAvailable(CF_TEXT))
    	{
    	  // get file refering to clipboard data
        hmem=dataObject.GetGlobalData(CF_TEXT);
        p1=(char *)GlobalLock(hmem);
        if(p1){
          cs=p1;
          ReplaceSel(cs,1);
        }
        GlobalUnlock(hmem);
      }
      return 1;
    }

  3. #3
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: How to make sure that only text could be dragged into a rich edit?

    Interesting, thanks. The way I was able to overcome the pasting of the formatted text was by setting up and accelerator for Ctrl+V and then calling pRichEdit->PasteSpecial(CF_TEXT) when it fired.

    But what about actually a situation when someone drags a formatted text into the rich edit?

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: How to make sure that only text could be dragged into a rich edit?

    Darn it! I thought I had all my bases covered. I just tried that with my control and it pastes formatted text. Now I've got to plug that hole too.

    I'll let you know what I come up with.

  5. #5
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: How to make sure that only text could be dragged into a rich edit?

    Hey, I'm glad that you and I are in this together. I'm at work now but I'll PM you what I came up with so far. Try it your way first and if you don't come up with something better we'll use my method. Also let me shatter your world a little bit more -- Shift + Ins also works as Paste so your method above may not work with it either. In my case, with the use of accelerators and PasteSpecial it's easy to fix though.

  6. #6
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: How to make sure that only text could be dragged into a rich edit?

    Hey, guys, I did some research and compiled a class that can allow this. Take a look at the attached project. There are couple of issues with it though:

    1. I cannot seem to find the way to remove the rich edit shortcut keys:
    http://msdn.microsoft.com/en-us/libr..._shortcut_keys

    2. There are some issues with non-text objects being dragged into a rich edit control, namely displaying the right mouse cursor for them.

    So, if someone finds out more about it, or if I messed up something in the code, please post an update.


    To use this class do the following:

    a. Add the MyRichEditOLECallback.h and MyRichEditOLECallback.cpp to your MFC project

    b. Include the MyRichEditOLECallback.h file:
    Code:
    #include "MyRichEditOLECallback.h"
    c. Subclass the rich edit control (if dialog based):
    Code:
    CRichEditCtrl m_pRE;
    d. Create a member of the MyRichEditOLECallback class for a scope of the rich edit control's existence (or place it into the same location as 'm_pRE'):
    Code:
    MyRichEditOLECallback mRECB;
    e. Call the following method when the rich edit control is created or initialized:
    Code:
    BOOL CTestRichEditExDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    	MY_RICH_EDIT_DATA mred_RE;
    	mred_RE.m_pRichEdifCtrl = &m_pRE;				//RichEdit
    	mred_RE.ndwContextMenuResID = IDR_MENU_CONTEXT;			//Context menu resource ID
    //	mred_RE.nFlags = 0;						//Flags (see MyRichEditOLECallback.h for info)
    	VERIFY(mRECB.AttachRichEditCtrl(&mred_RE));
     }
    Attached Files Attached Files
    Last edited by ahmd; September 9th, 2009 at 07:17 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured