VC in the program to Richeditview write text

Source: Internet
Author: User

When you set up an SDI or MDI program, if you choose Richeditview, you can take advantage of the rich format provided by RichEdit, but it is a problem to write text in Richeditview by program. The following is a description of how to implement a program to write text in Richeditview.

First, write text at the end of the text

void CRichEditUseDoc::OnTextWriteafter()
{
   CString str;
   int nTextLength;
   CRichEditView *pView = GetView();
   CRichEditCtrl &edit = pView->GetRichEditCtrl();
  
   //要写的内容为ABCDEFG
   str = "ABCDEFG";
   //加入换行
   str += "\r\n";
   //获得文字的长度
   nTextLength = edit.GetWindowTextLength();
   //将光标放在文本最末
   edit.SetSel(nTextLength, nTextLength);
   //写入文本
   edit.ReplaceSel(str);
}

Second, insert text after the cursor, you can use the CHARRANGE structure

void CRichEditUseDoc::OnTextWriteCur()
{
   CString str;
  
   CHARRANGE crPos;
   CRichEditView *pView = GetView();
   CRichEditCtrl &edit = pView->GetRichEditCtrl();
  
   //用CHARRANGE结构体获得选择的文本位置
   edit.GetSel(crPos);
   //要写的内容为12345
   str = "12345";
   //如果没有选中文本,就直接写在光标后
   //如果选中了文本,就替代选中的文本
   if (crPos.cpMin != crPos.cpMax)
   {
     edit.SetSel(crPos.cpMin, crPos.cpMax);
   }
   else
   {
     edit.SetSel(crPos.cpMax, crPos.cpMax);
   }
  
   edit.ReplaceSel(str);  
}

Third, all selected text

void CRichEditUseDoc::OnTextSelectall()
{
   CRichEditView *pView = GetView();
   CRichEditCtrl &edit = pView->GetRichEditCtrl();
   edit.SetSel(0, -1);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.