This section contains information about using object linking and embedding (OLE) in rich edit controls.
What You Need to Know
Technologies
Prerequisites
- C/C++
- Windows User Interface Programming
Instructions
Use a Rich Edit Interface
Rich edit controls expose some of their functionality through Component Object Model (COM) interfaces. By obtaining an interface from a control, you gain the ability to work with other objects within the control. You can obtain this interface by sending theEM_GETOLEINTERFACEmessage. From theIRichEditOleinterface, you can then obtain interfaces used in theText Object Model.
Another interface,IRichEditOleCallback, is implemented by applications to define the behavior of the control when it interacts with objects.
Insert an Object into a Rich Edit Control
The following code example inserts a file object into a rich edit control. If a program is associated with the file type on the user's machine (for example, Microsoft Excel for an .xls file), the contents of the file display in the control; otherwise, an icon appears.
-
Get theIRichEditOleinterface.
BOOL InsertObject(HWND hRichEdit, LPCTSTR pszFileName)
{
HRESULT hr;
LPRICHEDITOLE pRichEditOle;
SendMessage(hRichEdit, EM_GETOLEINTERFACE, 0, (LPARAM)&pRichEditOle);
...
-
Create structured storage.
LPLOCKBYTES pLockBytes = NULL;
hr = CreateILockBytesOnHGlobal(NULL, TRUE, &pLockBytes);
LPSTORAGE pStorage;
hr = StgCreateDocfileOnILockBytes(pLockBytes,
STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE,
0, &pStorage);
...
-
Set up the data format.
FORMATETC formatEtc;
formatEtc.cfFormat = 0;
formatEtc.ptd = NULL;
formatEtc.dwAspect = DVASPECT_CONTENT;
formatEtc.lindex = -1;
formatEtc.tymed = TYMED_NULL;
...
-
Get a pointer to the display site.
LPOLECLIENTSITE pClientSite;
hr = pRichEditOle->GetClientSite(&pClientSite);
...
-
Create the object and retrieve itsIUnknowninterface.
LPUNKNOWN pUnk;
CLSID clsid = CLSID_NULL;
hr = OleCreateFromFile(clsid,
pszFileName,
IID_IUnknown,
OLERENDER_DRAW,
&formatEtc,
pClientSite,
pStorage,
(void**)&pUnk);
pClientSite->Release();
...
-
Get the IOleObject interface to the object.
LPOLEOBJECT pObject;
hr = pUnk->QueryInterface(IID_IOleObject, (void**)&pObject);
pUnk->Release();
...
-
To ensure that references are counted correctly, notify the object that it is contained.
OleSetContainedObject(pObject, TRUE);
...
-
Set up object info.
REOBJECT reobject = { sizeof(REOBJECT)};
hr = pObject->GetUserClassID(&clsid);
reobject.clsid = clsid;
reobject.cp = REO_CP_SELECTION;
reobject.dvaspect = DVASPECT_CONTENT;
reobject.dwFlags = REO_RESIZABLE | REO_BELOWBASELINE;
reobject.dwUser = 0;
reobject.poleobj = pObject;
reobject.polesite = pClientSite;
reobject.pstg = pStorage;
SIZEL sizel = { 0 };
reobject.sizel = sizel;
...
-
Move the caret to the end of the text and add a carriage return.
SendMessage(hRichEdit, EM_SETSEL, 0, -1);
DWORD dwStart, dwEnd;
SendMessage(hRichEdit, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
SendMessage(hRichEdit, EM_SETSEL, dwEnd+1, dwEnd+1);
SendMessage(hRichEdit, EM_REPLACESEL, TRUE, (WPARAM)L"/n");
...
-
Insert the object.
hr = pRichEditOle->InsertObject(&reobject);
...
-
Clean up.
pObject->Release();
pRichEditOle->Release();
return TRUE;
}
Using IRichEditOleCallback
Applications implement theIRichEditOleCallbackinterface to respond to OLE-related queries or actions that are performed by a rich edit control. You associate your implementation of the interface with the control by sending anEM_SETOLECALLBACKmessage. The control then calls methods on your implementation of the interface as appropriate.
For example,QueryAcceptDatais called when the user attempts to drag or paste an object into the control. If your application can accept the data, your implementation of the method returns S_OK; otherwise, it returns an error code. The method might also take some other action, such as warning the user that files of that type cannot be placed in the control.
Complete InsertObject Example Function
The following code example demonstrates the previous code snippets combined into one complete function that includes error handling.
BOOL InsertObject(HWND hRichEdit, LPCTSTR pszFileName)
{
HRESULT hr;
LPRICHEDITOLE pRichEditOle;
SendMessage(hRichEdit, EM_GETOLEINTERFACE, 0, (LPARAM)&pRichEditOle);
if (pRichEditOle == NULL)
{
return FALSE;
}
LPLOCKBYTES pLockBytes = NULL;
hr = CreateILockBytesOnHGlobal(NULL, TRUE, &pLockBytes);
if (FAILED(hr))
{
return FALSE;
}
LPSTORAGE pStorage;
hr = StgCreateDocfileOnILockBytes(pLockBytes,
STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE,
0, &pStorage);
if (FAILED(hr))
{
return FALSE;
}
FORMATETC formatEtc;
formatEtc.cfFormat = 0;
formatEtc.ptd = NULL;
formatEtc.dwAspect = DVASPECT_CONTENT;
formatEtc.lindex = -1;
formatEtc.tymed = TYMED_NULL;
LPOLECLIENTSITE pClientSite;
hr = pRichEditOle->GetClientSite(&pClientSite);
if (FAILED(hr))
{
return FALSE;
}
LPUNKNOWN pUnk;
CLSID clsid = CLSID_NULL;
hr = OleCreateFromFile(clsid, pszFileName, IID_IUnknown, OLERENDER_DRAW,
&formatEtc, pClientSite, pStorage, (void**)&pUnk);
pClientSite->Release();
if (FAILED(hr))
{
return FALSE;
}
LPOLEOBJECT pObject;
hr = pUnk->QueryInterface(IID_IOleObject, (void**)&pObject);
pUnk->Release();
if (FAILED(hr))
{
return FALSE;
}
OleSetContainedObject(pObject, TRUE);
REOBJECT reobject = { sizeof(REOBJECT)};
hr = pObject->GetUserClassID(&clsid);
if (FAILED(hr))
{
pObject->Release();
return FALSE;
}
reobject.clsid = clsid;
reobject.cp = REO_CP_SELECTION;
reobject.dvaspect = DVASPECT_CONTENT;
reobject.dwFlags = REO_RESIZABLE | REO_BELOWBASELINE;
reobject.dwUser = 0;
reobject.poleobj = pObject;
reobject.polesite = pClientSite;
reobject.pstg = pStorage;
SIZEL sizel = { 0 };
reobject.sizel = sizel;
SendMessage(hRichEdit, EM_SETSEL, 0, -1);
DWORD dwStart, dwEnd;
SendMessage(hRichEdit, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
SendMessage(hRichEdit, EM_SETSEL, dwEnd+1, dwEnd+1);
SendMessage(hRichEdit, EM_REPLACESEL, TRUE, (WPARAM)L"/n");
hr = pRichEditOle->InsertObject(&reobject);
pObject->Release();
pRichEditOle->Release();
if (FAILED(hr))
{
return FALSE;
}
return TRUE;
}
分享到:
相关推荐
mfc vc richedit 绘制ole 图片
richedit中表情的嵌入应用imageole.dll
此文件特别适合需要对RichEdit进行开发的人,可以说是一个支持RTF的全功能Demo,不同于其他仅仅支持普通RTF文本和图片的程序。 MFC下实现完美读写RTF文件,支持RTF中图片,表格,ppt,word文档,以及各种以OLe插入的...
此文件特别适合需要对RichEdit进行开发的人,可以说是一个支持RTF的全功能Demo,不同于其他仅仅支持普通RTF文本和图片的程序。 MFC下实现完美读写RTF文件,支持RTF中图片,表格,ppt,word文档,以及各种以OLe插入的...
类似聊天窗口,可以发送表情、图片,RichEdit显示图片,这只是一个例子,不过可以借鉴一下 类似聊天窗口,可以发送表情、图片,RichEdit显示图片,这只是一个例子,不过可以借鉴一下
像QQ聊天窗口一样,RichEdit显示图片的一个例子,用到的是腾讯的一个OLE,使用前记得先注册ImageOledll
There are numerous examples that demonstrate how to use/create COM/OLE/ActiveX components. But these examples typically use Microsoft Foundation Classes (MFC), .NET, C#, WTL, or at least ATL, because ...
Written by a former data management consultant and current program manager at Microsoft in the Visual Languages group, ACTIVEX CONTROLS INSIDE OUT is a comprehensive update to the successful first ...
C# Richeditbox 插入 OLE ActiveX对象
Database APIs, including ADO, OLEDB, ODBC, and DAO, and how to use them in C++ applications Relational database design principles and techniques Relational database programming and SQL ...
'This sample demostrates the use of an ole object ptr being passed to 'an external (and optionally remote) ole server. The ole server then 'periodically calls a method on this object ptr. This has the...
RichTextBox 控件不仅允许输入和编辑文本 还支持图片,Excel表格,与QQ聊天窗一样
Demonstrates the use of OLE.
A tool that allows you to query OLE DB sources查询OLE DB数据源的工具