Cricheditctrl hypertext editing FAQ

Source: Internet
Author: User
Tags ole knowledge base
Cricheditctrl hypertext editing

I. FAQs
A. It can be compiled and cannot be executed.

Afxinitrichedit ();

B. Upgrade the default riched version (some bugs exist by default)
Can be added to initinstance
Loadlibrary ("richedw.dll ")
Last Note: freelibrary

If the base class of cricheditview is available
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_str 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 is added to the property.

F. Sometimes you do not want to paste formatted data. You can choose pastespecial to 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, 100,0 );

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

E. Three types of content are available.
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 (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 );
Reading can be similar, setwindowtext, em_settextex, streamin

F. Search for strings
Findtextex ft;
Ft. chrg. CPMin = 0;
Ft. chrg. cpmax =-1;
Ft. lpstrtext = "| ";
Long LPOS = findtext (0, & ft );

Modify CPMin if you want to continue searching, 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. Save 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

Steps:

1. Create a dialog box-based MFC project named showlink.

2. Drag the RichEdit control into the dialog box, open the cshowlinkapp class, and add afxinitrichedit () to initinstance ().

3. Use classwizard to create a cricheditctrl derived class cmyedit and add two functions:

Void cmyedit: setlink ()
{
Charformat2 CF2;
Zeromemory (& CF2, sizeof (charformat2 ));//
Cf2.cbsize = sizeof (charformat2 );
Cf2.dwmask = cfm_link;

Cf2.dweffects | = cfe_link;
Sendmessage (em_setcharformat, scf_selection, (lparam) & CF2 );

}

Void cmyedit: onurlclick (nmhdr * pnmhdr, lresult * presult)
{
Tchar linkchar [512];
Enlink * penlink = (enlink *) pnmhdr;
If (penlink-> MSG = wm_lbuttonup)
{
Setsel (penlink-> chrg );
Long res = getseltext (char *) linkchar );
Linkchar [res] = 0;
ShellExecute (null, "open", linkchar, null, null, sw_shownormal );
}
Presult = false;

}

4. Add a sentence in begin_message_map (cmyedit, cricheditctrl) of myedit. cpp: on_policy_reflect (en_link, onurlclick)

5. Add # include "myedit. H" to showlinkdlg. h and add a member variable cmyedit m_crich to the class.

6. Add two sentences in oninitdialog of cshowlinkdlg:

M_crich.seteventmask (enm_link );
M_crich.sendmessage (em_autourldetect, (wparam) True, 0 );

Compile it, enter a URL, and click it to open it.

 

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 last insert, and use an hglobal as the parameter. The data in hglobal is a metafilepict structure that contains the image provided by you.

Use cricheditview: insertfileasobject to insert an image. 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

// Uses QQ's Image Processing Control
# Import "imageole. dll" named_guids

// Only BMP void colericheditctrl: insertbitmap (hbitmap) {stgmedium stgm; stgm. tymed = tymed_gdi; // storage medium = hbitmap handlestgm. hbitmap = hbitmap; stgm. punkforrelease = NULL; // use releasestgmediumformatetc FM; FM. cfformat = cf_bitmap; // clipboard format = cf_bitmapfm.ptd = NULL; // target device = screenfm. dwaspect = dvaspect_content; // level of detail = Full contentfm. lindex =-1 ;// Index = Not applicaplefm. tymed = tymed_gdi; // create input data source istorage * pstorage; // allocate memory lplockbytes = NULL; scode SC =: createilockbytesonhglobal (null, true, & lplockbytes ); if (SC! = S_ OK) afxthrowoleexception (SC); Assert (lplockbytes! = NULL); SC =: stgcreatedocfileonilockbytes (lplockbytes, stgm_1__exclusive | stgm_create | stgm_readwrite, 0, & pstorage); If (SC! = S_ OK) {verify (lplockbytes-> release () = 0); lplockbytes = NULL; afxthrowoleexception (SC);} assert (pstorage! = NULL); coledatasource * pdatasource = new coledatasource; pdatasource-> cachedata (cf_bitmap, & stgm); lpdataobject = (lpdataobject) pdatasource-> getinterface (& iid_idataobject ); /// obtain RichEdit's oleclientsitelpoleclientsite lpclientsite; this-> getiricheditole ()-> getclientsite (& lpclientsite); // create an OLE object ioleobject * poleobject; SC = olecreatestaticfromdata (lpdataobject, iid_ioleobject, olerender_forma T, & FM, lpclientsite, pstorage, (void **) & poleobject); If (SC! = S_ OK) afxthrowoleexception (SC); // Insert the OLE object reobject; zeromemory (& reobject, sizeof (reobject); reobject. cbstruct = sizeof (reobject); CLSID; SC = poleobject-> getuserclassid (& CLSID); If (SC! = S_ OK) afxthrowoleexception (SC); reobject. CLSID = CLSID; reobject. CP = reo_cp_selection; reobject. dvaspect = dvaspect_content; reobject. poleobj = poleobject; reobject. polesite = maid; reobject. pstg = pstorage; hresult hR = This-> getiricheditole ()-> insertobject (& reobject); Delete pdatasource;} // load images such as GIF and BMP void colericheditctrl :: insertgif (cstring strfilepath) // path of the strfilepath GIF file {lplockbytes lplo Ckbytes = NULL; scode SC; hresult hr; // print to RichEdit's iclientsite lpoleclientsite m_lpclientsite; // a smart point to ianimator imageolelib: igifanimatorptr m_lpanimator; // PTR 2 Storage lpstorage m_lpstorage; // The object 2 B insert 2 lpoleobject m_lpobject; // create lockbytes SC =: createilockbytesonhglobal (null, true, & lplockbytes ); if (SC! = S_ OK) afxthrowoleexception (SC); Assert (lplockbytes! = NULL); // use lockbytes to create storage SC =: stgcreatedocfileonilockbytes (lplockbytes, stgm_1__exclusive | stgm_create | stgm_readwrite, 0, & m_lpstorage); If (SC! = S_ OK) {verify (lplockbytes-> release () = 0); lplockbytes = NULL; afxthrowoleexception (SC);} assert (m_lpstorage! = NULL); // get the clientsite of the very richeditctrl getiricheditole ()-> getclientsite (& m_lpclientsite); Assert (m_lpclientsite! = NULL); try {// initlize COM interface hR =: coinitialize (null); // (null, coinit_apartmentthreaded); If (failed (HR )) _ com_issue_error (HR); // get gifanimator object // here, I used a smart point, so I do not need to free it hR = m_lpanimator.createinstance (imageolelib: clsid_gifanimator ); if (failed (HR) _ com_issue_error (HR); // com operation need BSTR, so get a BSTR Path = strfilepath. allo Csysstring (); // load the GIF hR = m_lpanimator-> loadfromfile (PATH); If (failed (HR) _ com_issue_error (HR ); // trace0 (m_lpanimator-> getfilepath (); // get the ioleobject hR = m_lpanimator.queryinterface (iid_ioleobject, (void **) & m_lpobject); If (failed (HR )) _ com_issue_error (HR); // set it 2 B inserted olesetcontainedobject (m_lpobject, true); // 2 insert in 2 RichEdit, you need a struct of reobject REO Bject reobject; zeromemory (& reobject, sizeof (reobject); reobject. cbstruct = sizeof (reobject); CLSID; SC = m_lpobject-> getuserclassid (& CLSID); If (SC! = S_ OK) afxthrowoleexception (SC); // set CLSID reobject. CLSID = CLSID; // can be selected reobject. CP = reo_cp_selection; // content, but not static reobject. dvaspect = dvaspect_content; // goes in the same line of text line reobject. dwflags = reo_belowbaseline; // reo_resizable | reobject. dwuser = 0; // The very object reobject. poleobj = m_lpobject; // client site contain the object reobject. polesite = m_lpclientsite; // The storage reobject. pstg = m_lpstorage; sizel. cx = sizel. cy = 0; reobject. sizel = sizel; hwnd hwndrt = This-> m_hwnd; // sel all text //: sendmessage (hwndrt, em_setsel, 0,-1); // DWORD dwstart, dwend; //: sendmessage (hwndrt, em_getsel, (wparam) & dwstart, (lparam) & dwend); //: sendmessage (hwndrt, em_setsel, dwend + 1, dwend + 1); // Insert after the line of text getiricheditole ()-> insertobject (& reobject);: sendmessage (hwndrt, em_scrollcaret, (wparam) 0, (lparam) 0); variant_bool ret; // do frame changing ret = m_lpanimator-> triggerframechange (); // show it m_lpobject-> doverb (oleiverb_uiactivate, null, m_lpclientsite, 0, m_hwnd, null); m_lpobject-> doverb (oleiverb_show, null, m_lpclientsite, 0, m_hwnd, null); // redraw the window to show animation redrawwindow (); If (m_lpclientsite) {m_lpclientsite-> release (); m_lpclientsite = NULL;} If (m_lpobject) {m_lpobject-> release (); m_lpobject = NULL;} If (m_lpstorage) {m_lpstorage-> release (); m_lpstorage = NULL;} sysfreestring (PATH);} catch (_ com_error e) {afxmessagebox (E. errormessage ();: couninitialize ();}}

 

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 RTF Format String

Custom cricheditctrl Control
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.

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.