ZZ: cedit & crichedit tips

Source: Internet
Author: User
ZZ from: http://www.my1984.com.cn/article.asp? Id = 830 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

~~~~~~~~~~~~~~~~~~~~~~~~~~

Reference content: cedit: getsel

DWORD getsel () const;
Void getsel (Int & nstartchar, Int & nendchar) const;

Return Value:
In this version, a dual character is returned. The low character is the starting position, and the high character is the position of the first unselected character.

Parameter: nstartchar points to the first character position of the selected part, expressed as an integer.
Nendchar points to the position of the first unselected character, expressed as an integer.

Note:
Call this function to obtain the start and end positions of the currently selected part (if any) in the editing control. Available parameters can also return values.
For more information, see em_getsel in Win32.

See cedit: setsel

------------------------

Cedit: setsel

Void setsel (DWORD dwselection, bool bnoscroll = false );
Void setsel (INT nstartchar, int nendchar, bool bnoscroll = false );

Parameter: dwselection indicates the starting position of the low-level word, and the high-level word indicates the Ending position. If the low position is 0 and the high position is-1, all text in the editing control is selected. If the low position is-1, any currently selected content is removed from the selected status.
Bnoscroll indicates whether to display delimiters in scroll mode. If the value is false, it is displayed. If the value is true, it is not displayed.
Nstartchar indicates the starting position of the selected part. If nstartchar = 0 and nendchar =-1, the text of the edit control is all selected. If nstartchar =-1, any currently selected content is removed from the selected status.
Nendchar indicates the end position.

Note:
Call this member function to select a certain range of characters in an editing control.
For more information, see em_setsel in Win32.

See: cedit: getsel, cedit: replacesel

-----------------------------------

Cedit: replacesel

Void replacesel (lpctstr lpsznewtext, bool bcanundo = false );

Parameter: lpsznewtext points to an empty replacement string.
Bcanundo if this replacement can be undone, set this parameter to true. The default value is false.

Note:
Call this function to replace the currently selected part of the editing control with the text specified by lpsznewtext. Only part of the text in the editing control is replaced. To replace all text, use the cwnd: setwin-dow text member function. If no text is selected, the text is inserted into the current cursor position. For more information, see em_replacesel in Win32.

See: cwnd: setwindowtext

Left ()

Function to obtain the specified number of characters at the left of the string.
Syntax left (string, n)

String: string type. It specifies the string N: long type to be extracted from a substring. It indicates the return value of string for the length of the substring. If the function is successfully executed, n characters on the left of the string are returned. If an error occurs, an empty string ("") is returned (""). If the value of any parameter is null, the left () function returns NULL. If the value of N is greater than the length of the string, the left () function returns the entire string, but does not add other characters.

Cstring: reversefind

Int reversefind (tchar ch) const;

Return Value:
Returns the index of the last character that matches the required character in the cstring object. If the required character is not found,-1 is returned.

Parameter: the string to be searched.

Note:
This member function searches for the last character that matches a substring in this cstring object. This function is similar to the runtime function strrchr.

Example: // cstring: reversefind example:
Cstring S ("abcabc ");
Assert (S. reversefind ('B') = 4 );

See: cstring: Find, cstring: findoneof

Cedit: linelength

Int linelength (INT nline =-1) const;

Return Value:
When a function is called by a multi-row editing control, the length of the row specified by nline is returned (expressed in bytes). When a function is called by a single-row editing control, returns the length (in bytes) of the text in the editing control ).

Parameter: nline specifies the character index of the row to be obtained. If the parameter value is-1, it indicates the current line (that is, the line containing the delimiters), excluding the length of the selected part of the text in this line. Linelength is ignored when a single-line editing control is called.

Note:
Call this member function to obtain the length of a line in the editing control.
Call this function to obtain the character index of a row with a specified row number in the multiline editing control.
For more information, see em_linelength in Win32.

See: cedit: lineindex

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.