Richedit usage Overview

Source: Internet
Author: User
Tags ole

I. FAQs
A. It can be compiled and cannot be executed.
AfxInitRichEdit ();
B. Upgrade the default Riched version (some bugs exist by default), such
Can be added to InitInstance
LoadLibrary ("richedw.dll ")
NOTE: If FreeLibrary is available for the CRichEditView base class
BOOL CXXXXXXView: PreCreateWindow (CREATESTRUCT & cs)
{
// Load rich edit version 2.0
If (LoadLibraryA ("richedw.dll") = NULL)
{
AfxMessageBox (_ T ("Fail to load \" riched20.dll \ "."), MB_ OK | MB_ICONERROR );
PostMessage (WM_QUIT, 0, 0 );
Return FALSE;
} M_strClass = RICHEDIT_CLASSA; // for 2.0 class return CRichEditView: PreCreateWindow (cs );
}
C. Last append row
Richeditctrl. SetSel (-1,-1 );
Richeditctrl. ReplaceSel (LPCTSTR) str );
D. word limit
CRichEditCtrl: LimitText (long nChars)
E. line feed Switching
The following two sentences are added to the OnInitialUpdate () function of CRichEditView:
M_nWordWrap = WrapNone;
WrapChanged ();
WrapChanged is actually called
Ctrl. SetTargetDevice (NULL, 1); // m_nWordWrap = WrapNone
Ctrl. SetTargetDevice (NULL, 0); // m_nWordWrap = WrapToWindow
M_nWordWrap = WrapToTargetDevice
Ctrl. SetTargetDevice (m_dcTarget, GetPrintWidth ());
If it is in Dialog, you can use SetTargetDevice. Note that want return f is added to the property. Sometimes you do not want to paste formatted data. You can use PasteSpecial to selectively paste the data.
PmyRichEditCtrl->; PasteSpecial (CF_TEXT );
G. scroll to the last line as the input goes along with the automatic scroll bar
Int nFirstVisible = pmyRichEditCtrl-> GetFirstVisibleLine ();
If (nFirstVisible> 0)
{
PmyRichEditCtrl-> LineScroll (-nFirstVisible, 0 );
}
Or
M_cRichEdit.PostMessage (WM_VSCROLL, SB_BOTTOM, 0 );
H. Set the number of UNDO times (only available in RICHED20 or above, which is not supported by default and must be upgraded)
SendMessage (EM_SETTEXTMODE, TM_MULTILEVELUNDO, 0 );
TM_MULTILEVELUNDO supports multi-cancel (default). You can set the maximum number of times through EM_SETUNDOLIMIT.
SendMessage (EM_SETUNDOLIMIT,); I. Response OnChange
EM_SETEVENTMASK sets ENM_CHANGE
Long lMask = GetEventMask ();
LMask | = ENM_CHANGE;
LMask & = ~ ENM_PROTECTED;
SetEventMask (lMask); j. Set read-only
CRichEditCtrl: SetReadOnly (BOOL bReadOnly = TRUE );
Set PROTECTED to read-only the selected text. For more information, see
Http://www.codeguru.com/Cpp/controls/richedit/article.php/c2401/
Ii. function applications
A. Set the font (mainly through SetSelectionCharFormat)
CHARFORMAT cf;
ZeroMemory (& cf, sizeof (CHARFORMAT ));
Cf. cbSize = sizeof (CHARFORMAT );
Cf. dwMask | = CFM_BOLD;
Cf. dwEffects | = CFE_BOLD; // set bold. Cancel using cf. dwEffects & = ~ CFE_BOLD;
Cf. dwMask | = CFM_ITALIC;
Cf. dwEffects | = CFE_ITALIC; // set italic. deselect cf. dwEffects & = ~ CFE_ITALIC;
Cf. dwMask | = CFM_UNDERLINE;
Cf. dwEffects | = CFE_UNDERLINE; // set italic, cancel using cf. dwEffects & = ~ CFE_UNDERLINE;
Cf. dwMask | = CFM_COLOR;
Cf. crTextColor = RGB (, 0); // set the color
Cf. dwMask | = CFM_SIZE;
Cf. yHeight = 200; // set the height.
Cf. dwMask | = CFM_FACE;
Strcpy (cf. szFaceName, _ T (""); // set the font
Rich. SetSelectionCharFormat (cf );
B. Set the row spacing of the font.
Use richedit2.0 or later
Try
PARAFORMAT2 pf;
Pf. cbSize = sizeof (PARAFORMAT2 );
Pf. dwMask = PFM_NUMBERING | PFM_OFFSET;
Pf. wNumbering = PFN_BULLET; // pay attention to PFM_NUMBERING.
Pf. dxOffset = 10;
VERIFY (SetParaFormat (pf ));
Frequently Used dwMask has
The PFM_NUMBERING member wNumbering takes effect. The project symbol is used by default.
2 use Arabic numerals (1, 2, 3 ,...).
3. Use lowercase letters (a, B, c ,...).
4. Use uppercase letters (A, B, C ,...).
5 Use lowercase roman numerals (I, ii, iii ,...).
6. Use uppercase roman numerals (I, II, III ,...).
7. For custom characters, see wNumberingStart.
The PFM_OFFSET member dxOffset takes effect. indent, unit: twips
The PFM_STARTINDENT member dxStartIndent takes effect, and the first line is indented.
PFM_SPACEAFTER member dySpaceAfter takes effect, segment spacing
PFM_LINESPACING member dyLineSpacing takes effect, line spacing
C. Set CRichEditCtrl (2.0) to transparent background
Long style =: GetWindowLong (GetSafeHwnd (), GWL_EXSTYLE );
Style & = WS_EX_TRANSPARENT;
: SetWindowLong (GetSafeHwnd (), GWL_EXSTYLE, style );
Or CreateEx, and then add the WS_EX_TRANSPARENT style to the e.
1) GetWindowText
2) use EM_GETTEXTEX
GETTEXTEX gt;
Gt. cb = 200;
Gt. flags = GT_DEFAULT;
Gt. codepage = CP_ACP;
Gt. lpDefaultChar = NULL;
Gt. lpUsedDefChar = NULL;
SendMessage (EM_GETTEXTEX, (WPARAM)>, (LPARAM) text );
3) StreamOut (mainly used for output in RTF and other formats)
Static DWORD CALLBACK
MyStreamOutCallback (DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG * pcb)
{
CFile * pFile = (CFile *) dwCookie; pFile-> Write (pbBuff, cb );
* Pcb = cb; return 0;
}
CFile cFile (TEXT ("myfile. rtf"), CFile: modeCreate | CFile: modeWrite );
EDITSTREAM es;
Es. dwCookie = (DWORD) & cFile; // set the use case parameters for callback function calls
Es. pfnCallback = MyStreamOutCallback;
PmyRichEditCtrl-> StreamOut (SF_RTF, es );
Read and so on. SetWindowText, EM_SETTEXTEX, and StreamIn f. query strings.
FINDTEXTEX ft;
Ft. chrg. cpMin = 0;
Ft. chrg. cpMax =-1;
Ft. lpstrText = "| ";
Long lPos = FindText (0, & ft); if you want to continue searching, modify cpMin, as shown in figure
Int nCount = 0;
Do
{
Long lPos = GetRichEditCtrl (). FindText (0, & ft );
If (-1 = lPos) break;
Ft. chrg. cpMin = lPos + strlen (ft. lpstrText );
++ NCount;
} While (TRUE); g. Saved in Html Format
Currently, you can convert it to the RTF format first, and then use the RTF-to-HTML Converter
Http://www.codeguru.com/Cpp/controls/richedit/conversions/article.php/c5377/
H. Reload the OnProtected function to obtain the corresponding message, such as pasting.
Void CMYichEditorView: OnProtected (NMHDR * pNMHDR, LRESULT * pResult)
{
ENPROTECTED * pEP = (ENPROTECTED *) pNMHDR; switch (pEP-> msg ){
Case WM_KEYDOWN: // press the key to determine pEP-> wParam
Case WM_PASTE: // Paste
Case WM_CUT: // cut
Case EM_SETCHARFORMAT:
Default:
Break;
};
* PResult = FALSE;
}
Iii. Common chat
A. LINK Function
1. LoadLibrary (_ T ("richedw.dll "));
2. Create the RichEdit2.0 control.
CreateEx (0, _ T ("RichEdit20A"), NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_TABSTOP
| ES_READONLY | ES_WANTRETURN | ES_MULTILINE,
Rect. left, rect. top, cx, cy,
PParentWnd-> m_hWnd, (HMENU) nID, NULL );
3. Set the selected text to link display
CHARFORMAT2 cf2;
ZeroMemory (& cf2, sizeof (CHARFORMAT2 ));//
Cf2.cbSize = sizeof (CHARFORMAT2 );
Cf2.dwMask = CFM_LINK;
Cf2.dwEffects | = CFE_LINK;
M_cRichEdit.SendMessage (EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) & cf2 );
4. Support for Link click response
M_cRichEdit.SetEventMask (ENM_LINK );
5. Click EN_LINK BEGIN_MESSAGE_MAP (CMyRichEdit, CRichEditCtrl) in the response link)
On_policy_reflect (EN_LINK, OnURL)
END_MESSAGE_MAP ()
... Void CMyRichEdit: OnURLClick (NMHDR * pNmhdr, LRESULT * pResult)
{
TCHAR LinkChar [512];
ENLINK * pLink = (ENLINK *) pNmhdr;
If (pLink-> msg = WM_LBUTTONUP)
{
SetSel (penLink-> chrg); // This is the text range of The Link
Long Res = GetSelText (char *) LinkChar); // This is the link text
// Your processing process is following
......
}
} B. Insert a bitmap.
Http://www.codeguru.com/Cpp/controls/richedit/article.php/c2417/
Http://www.codeguru.com/Cpp/controls/richedit/article.php/c5383/
Customize the icon for inserting objects in RichEdit
Http://www.blogcn.com/user3/jiangsheng/blog/1319738.html
The method is basically the same as the Knowledge Base Article Q220844 HOWTO: Insert a Bitmap Into an RTF Document Using the RichEdit Control
Just call IOleCache: SetData before the final insertion, and use an HGLOBAL as the parameter. The data in HGLOBAL is a METAFILEPICT structure, containing the image you provided using CRichEditView :: insertFileAsObject can insert images. VC ++ has an example WordPad.
For more information, see Insert any HBITMAP (Bitmap) in your RichEdit Control (http://www.codeguru.com/richedit/richeditrc.html ).
C. Display GIF Animation
Commonly used is through qq's imageole. dll (also useful for Gif89.dll)
Http://www.xiaozhou.net/cooldog/blogview.asp? LogID = 82
Http://www.codeproject.com/richedit/AnimatedEmoticon.asp
Insert a dynamic GIF (Native C ++) into the richedit control)
Http://blog.joycode.com/jiangsheng/archive/2004/12/15/41209.aspx
D. Use of IRichEditOleCallback
Http: // 61.186.252.131/Expert/topic/905/905844 .xml? Temp =. 8379022.
Create a message sending box similar to MSN (on)
Http://www.vckbase.com/document/viewdoc? Id = 1087
Content includes: Right-click menu, insert image, read/write custom CRichEditCtrl control in RTF Format String
Http://www.vckbase.com/document/viewdoc? Id = 328
Content includes: Right-click message, message ing, font conversion PS. after the richedit control is upgraded to 2.0, the font is set to the primary body, and there is no problem in Inputting Chinese characters. However, when a letter is entered, it is automatically redirected to the Arial font, but 1.0 does not have this question, still show letters with the letter body
Is a Specialized Design Dual-font, Smart font apply, see http: // 61.186.252.131/Expert/topic/913/913807 .xml? Temp =. 3753778.

Note:
M_edit1 indicates the control type variable of the CEdit control whose ID is IDC_EDIT1.
M_richedit1 indicates the control type variable of the CRichEditCtrl control whose ID is IDC_RICHEDIT1
--------------------------------------------------------------------------------
1. Set the edit read-only attribute
Method 1:
M_edit1.SetReadOnly (TRUE );
Method 2:
: SendMessage (m_edit1.m_hWnd, EM_SETREADONLY, TRUE, 0 );
--------------------------------------------------------------------------------
2. Determine the cursor status in edit and obtain the selected content (richedit is also applicable)
Int nStart, nEnd;
CString strTemp;
M_edit1.GetSel (nStart, nEnd );
If (nStart = nEnd)
{
StrTemp. Format (_ T ("cursor in % d"), nStart );
AfxMessageBox (strTemp );
}
Else
{
// Obtain the selected content of edit.
M_edit1.GetWindowText (strTemp );
StrTemp = strTemp. Mid (nStart)-strTemp. Mid (nEnd );
AfxMessageBox (strTemp );
}
Note: After GetSel, if nStart and nEnd indicate that the cursor is in a certain position (intuitively, the cursor is flashing );
If nStart and nEnd are not equal, the user selects a piece of content in edit.
--------------------------------------------------------------------------------
3. Add a string at the end of edit.
CString str;
M_edit1.SetSel (-1,-1 );
M_edit1.ReplaceSel (str );
--------------------------------------------------------------------------------
4. Automatically scroll to the last line as input (richedit also applies)
Method 1: (from msdn)
// The pointer to my edit.
Extern CEdit * pmyEdit;
Int nFirstVisible = pmyEdit-> GetFirstVisibleLine ();
// Scroll the edit control so that the first visible line
// Is the first line of text.
If (nFirstVisible> 0)
{
PmyEdit-> LineScroll (-nFirstVisible, 0 );
}
Method 2:
M_richedit.PostMessage (WM_VSCROLL, SB_BOTTOM, 0 );
--------------------------------------------------------------------------------
5. How to restrict the specified characters in edit
A class can be derived from CEdit to add the WM_CHAR message ing. The following example provides a limited input of hexadecimal characters.
Void CMyHexEdit: OnChar (UINT nChar, UINT nRepCnt, UINT nFlags)
{
If (nChar> = '0' & nChar <= '9') |
(NChar> = 'A' & nChar <= 'F') |
(NChar> = 'A' & nChar <= 'F') |
NChar = VK_BACK |
NChar = VK_DELETE) // msdn virtual key
{
CEdit: OnChar (nChar, nRepCnt, nFlags );
}
}
--------------------------------------------------------------------------------
6. How to Use richedit
Add AfxInitRichEdit ();
CxxxApp: InitInstance ()
{
AfxInitRichEdit ();
.............
}
AfxInitRichEdit (): loads RichEdit 1.0 Control (RICHED32.DLL ).
--------------------------------------------------------------------------------
7. How to Use richedit2.0 or richedit3.0
Usage reason: because RichEdit2.0A is automatically a wide character (WideChar), it can solve Chinese garbled characters and some Chinese Character problems.
Method 1: (the method on msdn is applicable to projects created using VC. NET and later)
To update rich edit controls in existing Visual C ++ applications to version 2.0,
Open the. RC file as text, change the class name of each rich edit control from "RICHEDIT" to "RichEdit20a ".
Then replace the call to AfxInitRichEdit with AfxInitRichEdit2.
Method 2: Take the dialog box as an example:
(1) Add a global variable HMODULE hMod;
(2) Add an hMod = LoadLibrary (_ T ("richeddll DLL") in CxxxApp: InitInstance "));
Add FreeLibrary (hMod) to CxxxApp: ExitInstance );
(3) Put A richedit in the dialog box. Open the. rc file in text mode and modify the richedit control class name "RICHEDIT" to "RichEdit20a ".
(4) add CRichEditCtrl m_richedit to the header file of the dialog box;
Add m_richedit.SubclassDlgItem (IDC_RICHEDIT1, this) in OnInitDialog );
--------------------------------------------------------------------------------
8. Change the color and font of the specified richedit Area
CHARFORMAT cf;
ZeroMemory (& cf, sizeof (CHARFORMAT ));
Cf. cbSize = sizeof (CHARFORMAT );
Cf. dwMask = CFM_BOLD | CFM_COLOR | CFM_FACE |
CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE;
Cf. dwEffects = 0;
Cf. yHeight = 12*12; // text height
Cf. crTextColor = RGB (200,100,255); // text color
Strcpy (cf. szFaceName, _ T (""); // set the font
M_richedit1.SetSel (1, 5); // you can specify the processing area.
M_richedit1.SetSelectionCharFormat (cf );
--------------------------------------------------------------------------------
9. Set the row spacing (only applicable to richedit2.0)
PARAFORMAT2 pf;
Pf2.cbSize = sizeof (PARAFORMAT2 );
Pf2.dwMask = PFM_LINESPACING | PFM_SPACEAFTER;
Pf2.dyLineSpacing = 200;
Pf2.bLineSpacingRule = 4;
M_richedit.SetParaFormat (pf2 );
--------------------------------------------------------------------------------
10. Insert a bitmap into richedit
Q220844: How to insert a bitmap into an RTF document using the RichEdit control in Visual C ++ 6.0
Http://support.microsoft.com/default.aspx? Scid = kb; en-us; 220844
Http://www.codeguru.com/Cpp/controls/richedit/article.php/c2417/
Http://www.codeguru.com/Cpp/controls/richedit/article.php/c5383/
--------------------------------------------------------------------------------
11. Insert GIF animation into richedit
Http://www.codeproject.com/richedit/AnimatedEmoticon.asp
--------------------------------------------------------------------------------
12. Insert richedit into ole object
Http://support.microsoft.com/kb/141549/en-us
--------------------------------------------------------------------------------
13. Read-only the selected richedit content
Http://www.codeguru.com/cpp/controls/richedit/article.php/c2401/
--------------------------------------------------------------------------------
14. Print richedit
Http://www.protext.com/MFC/RichEdit3.htm
--------------------------------------------------------------------------------
15. richeidt is used for chat message window
Http://www.vckbase.com/document/viewdoc? Id = 1087
Http://www.codeproject.com/richedit/chatrichedit.asp
Http://www.codeguru.com/Cpp/controls/richedit/article.php/c2395/
--------------------------------------------------------------------------------
16. fixed the problem of no response to EN_SETFOCUS and EN_KILLFOCUS of richedit.
Http://support.microsoft.com/kb/181664/en-us
--------------------------------------------------------------------------------
17. richedit spelling check
Http://www.codeproject.com/com/AutoSpellCheck.asp
--------------------------------------------------------------------------------
18. Change the edit background color.
Q117778: How to change the background color of an MFC edit control
Http://support.microsoft.com/kb/117778/en-us
--------------------------------------------------------------------------------
19. When the parent window attribute of the edit Control is WS_CAPTION with the title bar and WS_CHILD, the focus SetFocus cannot be set.
Q230587: PRB: Can't Set Focus to an Edit Control When its Parent Is an Inactive Captioned Child Window
Http://support.microsoft.com/kb/230587/en-us
--------------------------------------------------------------------------------
20. When you press enter in Edit, the dialog box will exit.
Select the Edit style Want Return.
The MSDN explanation is as follows:
ES_WANTRETURN Specifies that a carriage return be inserted when the user presses the ENTER key while entering text into a multiple-line edit control in a dialog box. without this style, pressing the ENTER key has the same effect as pressing the dialog box's default pushbutton. this style has no effect on a single-line edit control.
--------------------------------------------------------------------------------
21. No borders for dynamically created edit
M_edit.Create (....);
M_edit.ModifyStyleEx (0, WS_EX_CLIENTEDGE, SWP_DRAWFRAME );
--------------------------------------------------------------------------------
22. An example of how to display RTF, ole (including gif, wmv, excel, and ppt)
Http://www.codeproject.com/richedit/COleRichEditCtrl.asp

Re: http://bbs.chinavideo.org/viewthread.php? Tid = 1806

3. Personal things (instances)

Initialization: AfxInitRichEdit2 ();

The two RichEditCtrl values in the window correspond:

CRichEditCtrl m_rtdRecv; // Receiving Window
CRichEditCtrl m_rtdSend; // sending window

// Set richEdit window properties
Void CDemoDlgMsg: SetRichEditStyle ()
{
/* Set some properties of related controls */
M_rtdRecv.SetAutoURLDetect (TRUE );
M_rtdRecv.SetEventMask (ENM_CHANGE );
USES_CONVERSION;
/* Set the font attribute of the chat edit box and information display box */
// M_cfDefault.cbSize = sizeof (CHARFORMAT );
// M_cfDefault.dwMask = CFM_BOLD | CFM_COLOR | CFM_FACE | CFM_ITALIC | CFM_UNDERLINE;
// M_cfDefault.dwEffects = CFE_BOLD | CFE_ITALIC | CFE_UNDERLINE;
// M_cfDefault.yHeight = 20*1440/96;
// M_cfDefault.yOffset = 0;
// M_cfDefault.crTextColor = RGB (255, 0, 0 );
// M_cfDefault.bCharSet = 0;
// M_cfDefault.bPitchAndFamily = 0;
// Strcpy (W2A (m_cfDefault.szFaceName), "");
M_cfDefault.cbSize = sizeof (CHARFORMAT );
M_rtdrecv.getdefacharcharformat (m_cfDefault );
M_cfDefault.yHeight = 200;
M_cfDefault.dwEffects = 0;
M_cfDefault.crTextColor = RGB (0, 0,255 );
M_rtdrecv.setdefacharcharformat (m_cfDefault );
M_rtdRecv.LimitText (1024 );
M_rtdsend.setdefacharcharformat (m_cfDefault );
M_rtdSend.LimitText (1024 );
}
// Display the message in the Receiving Window
Void CDemoDlgMsg: ShowRecvText (CString str)
{
/* Line feed display */
M_rtdRecv.ReplaceSel (str );
M_rtdRecv.SetSel (-1,-1 );
M_rtdRecv.ReplaceSel (L "\ r \ n ");
}

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.