[Original] cedit & crichedit usage skills
Author: lixiaosan
Date: 04/07/2006
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_wantreturnSpecifies 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