NET RichTextBox Controls how you can insert images

Source: Internet
Author: User

This article Describes. NET RichTextBox Controls how you can insert images, controls, and ActiveX objects by using OLE methods, as explained IN. unfortunately, It covers only a sample of C source code, so I need to implement a similar solution in managed code (C #).
There is a RichTextBox insert picture and other relevant terms of OLE object, but they are using RTF code, I need a more suitable chat using specialized control and provide one?? A way to insert emoticons, progress bars and images, and finally, restore them to get their OLE processing or any Object's Properties. Special thanks
Khendys Gordon's article: "." Background
To implement the solution, I need to use the P/invoke platform invoke Method. I'm from a lot of Information.
The first step in inserting a RichTextBox OLE object is to get its iricheditole interface. It can be done by sending the message em_getoleinterface control: This. IRichEditOle = SendMessage message (richedithandle,em_getoleinterface,0);
With this interface, you can insert objects through the Reobject structure. It is important to note that you can specify an insertion point, an aspect dwuser variable to store the flags of this object or any related information, so you can update it at any time.//-----------------------reobject reoobject = New Reobject (); REOOBJECT.CP = This._richedit.textlength;reoobject.clsid = GUID;REOOBJECT.PSTG = pStorage; Reoobject.poleobj = Marshal.getiunknownforobject (control group); reoobject.polesite = Poleclientsite;reoobject.dvaspect = (uint) (dvaspect.dvaspect_content); reoobject.dwflags = (uint) (reoobjectflags.reo_belowbaseline); Reoobject.dwuser = 1; This. Iriceditole.insertobject (reoobject);//-----------------------
To insert an image, you need to implement the interface idataobject, in which case I name it mydataobject. This class is an OLE callback object that uses the FORMATETC and stgmedium structures to show the image, telling the OLE container that this object is a GDI bitmap in the Clipboard format (cf_bitmap) (tymed_gdi) public class Mydataobject:i Dataobject's. {private bitmap mbitmap; formatetc mpformatetc of the public; # IDataObject Congressman of the region private constant UINT = 0; Private constant UINT e_pointer = 0x80004003; Private constant UINT E_ Notimpl = 0x80004001; E_fail of private constant UINT = 0x80004005; ; GetData of public uint (formatetc pformatetc of the literature 160; Document Stgmedium pmedium) {intptr hdst = mbitmap.gethbitmap (); pmedium.tymed = (INT) tymed.tymed_gdi; Pmedium.unionmember = hdst; Pmedium.punkforrelease = intptr.zero; (UINT) returns s_ok; } ... ... #endregion public mydataobject () {mbitmap = new Bitmap (16,16); mpformatetc = new formatetc ();} public invalid setimage (string Strfilename) {try & nbsp;; {mbitmap = (bitmap) bitmap.fromfile (strfilename,true);  //clipboard format = cf_bitmap & #160; Mpformatetc.cfformat = Clipform At. cf_bitmap; //target device = Screen 0; MPFORMATETC.PTD = intptr.zero; & #160; //level of detail = Full content  ; Mpformatetc.dwaspect = Dvaspect. dvaspect_content; //index = Applicaple & #160; Mpformatetc.lindex =-1; 0; //hbitmap processing of storage media 60; mpformatetc.tymed = Tymed. tymed_gdi; Capture {}} Public Invalid setimage (image Image) {try 0; {mbitmap = new Bitmap (picture);//clipboard format = Cf_bitmap Mpformatetc.cfformat = clipformat. cf_bitmap; 0; //target device = Screen 160;MPFORMATETC.PTD = intptr.zero;  ; //verbosity = Full content 60;mpformatetc.dwaspect = Dvaspect. dvaspect_content; 0; //index = Applicaple  ; Mpformatetc.lindex =-1; 160; //hbitmap processing & #160 of storage media; mpformatetc.tymed = Tymed. tymed_gdi; 0; } Capture {0;}}}
Look at a member method setimage how to create a Bitmap object that takes advantage of the getdata that it calls when it is Processed.
now, The object here is how to create a shared global memory and pointer (istorage) and use iricheditole.public invalid Insertmydataobject (mydataobject MDO) Oleclientsite interface inserted into Richeditbox{(MDO = = NULL) 60; return;//-----------------------ilockbytes plockbytes; INT SC = CreateILockBytesOnHGlobal (intptr.zero,  ; true, out of plockbytes); IStorage pstorage; SC = stgcreatedocfileonilockbytes (plockbytes, (UINT) (stgm.stgm_share_exclusive | Stgm. Stgm_create | 160; Stgm. Stgm_readwrite) 0,pstorage); The IOleClientSite poleclientsite; 60; This. Iricheditole.getclientsite (out of poleclientsite); 0; -----------------------guid (guid = Marshal.generateguidfortype mdo. GetType ()); GUID iid_ioleobject = new GUID ("{00000112-0000-0000-c000-000000000046}"); GUID Iid_idataobject = 160; New GUID ("{0000010e-0000-0000-c000-000000000046}"); GUID IID_IUnknown = new GUID ("{00000000-0000-0000-c000-000000000046}"); Object poleobject; INT HR = Olecreatestaticfromdata (MDO, Property iid_ioleobject (UINT) olerender.olerender_format, Literature mdo.mpformatetc Poleclientsite,pstoragE Out of poleobject); if (poleobject = = NULL) returns; -----------------------//-----------------------olesetcontainedobject (poleobject,true); 160; Reobject reoobject = new Reobject (); REOOBJECT.CP = this._richedit.textlength; Reoobject.clsid = GUID; REOOBJECT.PSTG = pstorage; Reoobject.poleobj = Marshal.getiunknownforobject (poleobject); 60; Reoobject.polesite = poleclientsite; Reoobject.dvaspect = (UINT) (dvaspect.dvaspect_content); Reoobject.dwflags = (UINT) (reoobjectflags.reo_belowbaseline); 160;reoobject.dwuser = 0; This. Iricheditole.insertobject (reoobject); -----------------------160;//-----------------------marshal.releasecomobject (plockbytes); Marshal.ReleaseComObject (poleclientsite); Marshal.ReleaseComObject (pstorage); Marshal.ReleaseComObject (poleobject); //-----------------------
There are other ways to insert controls and ActiveX objects, and they look very similar to the above method, so please review the source Code. Points of interest
finally, How do I control updates?
This is the trick that you need to use a timer and call the method Updateobjects. This method performs a search for all objects in richtextbox, and if they are special tags (in my case, i use the Dwuser variable), they will be updated: public invalid updateobjects () {K = This. Iricheditole.getobjectcount (); for (int i = 0; i lt; K,i) {& #160; Reobject reoobject = new Reobject (); This. Iricheditole.getobject (i,reoobject; getobjectopt?? IONS. reo_getobj_all_interfaces); (reoobject.dwuser = = 1) 160; {point PT = this._richedit.getpositionfromcharindex (reoobject.cp); 0; Rectangle rect = new Rectangle (pt,reoobject.sizel); this._ Richedit.invalidate (rect,false); //redraw}}}
There are many optimizations for this control, but it is now necessary to work on any suggestions to express Appreciation. Using code
To use the code, simply add a reference to the control, put in the normal RichTextBox form, and then replace the Myextrichtextbox type: myextrichtextbox.myextrichtextbox richTextBox1; Tips
I create an array of controls (buttons and progress Bars) and add a timer form, and then call such a method updateobjects update the object as: private void Timer1_Tick (object sender, send System.eventargs) {for (int i = 0; I lt; ar.   Count; I) {itimer; ; If (itimer's GT 100) 60; Itimer = 0; Object obj = AR [i]; 60; If (obj is a button) {& #160; The BT = button, obj; 0; (bt. Text = "clickedquot;" & #160; Bt. Text = "button quot; i.ToString () 160; "-" Itimer. ToString (); } Other & #160; {progressbar PB = (progress bar) OBJ; if (1 pb). Value GT (+) pb. Value = 0; Pb. Value = Pb. Value 1; }} richtextbox1.updateobjects ();} Historical version 1.0-1/November 2005

NET RichTextBox Controls how you can insert images

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.