Sometimes we need to insert a variety of different objects in a container. For example, in the QQ chat dialog box can insert a variety of static and dynamic pictures, or even insert Flash files, these are implemented by OLE technology. OLE technology is based on CoM, while COM is very complicated, wants to learn COM spend a lot of time, "COM technology insider", "INSIDE OLE 2" is very good COM and OLE books, but they have to finish these books, understand, and even for a few months. Learning COM is a depressed, long process, although the progress is very slow, but persistent will be very interesting. If we are just using someone else's components and insert others into their own containers, it is not very complicated. The following is an example. The components created in MSDN's ATL Tutorial are used as objects we have to insert (see MSDN for the creation of the component). 1. Create a COM client based on the dialog, named the client, and set up the project by default option. 2. Add a Richedit control in the main dialog, use classwizard to add a variable indicating the control, M_RE, remember the type set to Control. Add a button for the main dialog, ID is set to IDC_INSERTPOLYGON, and CAPTION is set to INSERTPOLYGON. 3, in the cclientapp :: InitInstance () function, add the following code before creating the dialog: if (AFXInitricHeDit () == false) {return false;} If the above code is not added, the application will not be able to run. 4, add header files in client.cpp "../polygon.h" // DLL module header file #include "../polygon_i.c" // interface definition #include "richole.h" 5 , is added to the private dialog class member functions BOOL InsertPolygon (IRichEditOle * lpRichEditOle); this function is defined as follows: BOOL CClientDlg :: InsertPolygon (IRichEditOle * lpRichEditOle) {IStorage * lpStorage = NULL; // storage interface IOleObject * lpOleObject = NULL; // OLE object lplockbytes lplockbytes = null; // Lockbyte IoleClientSite * lpoleclientsite = null; ipolyctl * lppolyctl = null; // control clsid clsid; Reobject Reobject; HRESULT HR;
if (lpRichEditOle == NULL) return FALSE; // Create PolyCtl object and obtaining interface hr = :: CoCreateInstance (CLSID_PolyCtl, NULL, CLSCTX_INPROC, IID_IPolyCtl, (LPVOID *) & lpPolyCtl); if (lpPolyCtl == NULL) {return FALSE; } // USES_CONVERSION; BOOL bRet = TRUE; try {hr = lpPolyCtl-> QueryInterface (& lpOleObject); // get the data object interface if (hr = S_OK!) AfxThrowOleException (hr); hr = lpOleObject-> GetUserClassID (& clsid); if (hr = S_OK!) AfxThrowOleException (hr); hr = :: CreateILockBytesOnHGlobal (NULL, TRUE, & lpLockBytes); // create LOCKBYTE objects if (hr = S_OK!) AfxThrowOleException (hr); ASSERT (! lpLockBytes = NULL); hr = :: StgCreateDocfileOnILockBytes (lpLockBytes, // create compound documents STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE, 0, & lpStorage); if (! hr = S_OK) {VERIFY (lpLockBytes-> Release () == 0); lpLockBytes = NULL; AfxThrowOleException (hr);} lpRichEditOle-> GetClientSite (& lpOleClientSite); ZeroMemory (& reobject, sizeof (REOBJECT)); // initialize an object reobject.cbStruct = sizeof (REOBJECT); reobject.clsid = clsid; reobject. cp = REO_CP_SELECTION; reobject.dvaspect = DVASPECT_CONTENT; reobject.dwFlags = REO_BELOWBASELINE; reobject.poleobj = lpOleObject; reobject.polesite = lpOleClientSite; reobject.pstg = lpStorage;
HR = lprichditole-> INSERTOBJECT (& Reobject); if (hr! = s_ok) AFXTHROWOLEEXCEPTION (HR); OLESETCONTAINEDOBJECT (LPOLEOBJECT, TRUE);}
Catch (ColeException * e) {trace ("OleException Code:% D"), E-> M_SC); E-> delete (); BRET = false;}
// release the interface if (lpPolyCtl = NULL!) LpPolyCtl-> Release (); if (! LpOleObject = NULL) lpOleObject-> Release (); if (! LpOleClientSite = NULL) lpOleClientSite-> Release (); if (lpStorage ! = NULL) lpStorage-> Release (); return bRet;} 6, add button InsertPolygon response function is defined as follows: void CClientDlg :: OnInsertpolygon () {IRichEditOle * lpRichEditOle = NULL; lpRichEditOle = m_re.GetIRichEditOle (); if (lprichditole! = null) {INSERTPOLYGON (LPRICHEDITOLE); lprichditole-> release ();}} 7, running renderings are as follows: If you have your own control, you can insert it as described above.