GifOleControl 可插入图片,包括BMP、jpg、png等,图片大小不限,重点是可以在richedit中插入gif动画,暂不支持插入SWF格式。同时可以在插入的图片、gif中直接显示tooltip,方便应用。
GifOleControl 定义的接口如下
interface IGifCtrl : IDispatch{
[propput, bindable, requestedit, id(DISPID_BORDERCOLOR)]
HRESULT BorderColor([in]OLE_COLOR clr);
[propget, bindable, requestedit, id(DISPID_BORDERCOLOR)]
HRESULT BorderColor([out, retval]OLE_COLOR* pclr);
[propget, bindable, requestedit, id(DISPID_HWND)]
HRESULT HWND([out, retval]LONG_PTR* pHWND);
[id(1), helpstring("方法LoadFromFile")] HRESULT LoadFromFile([in] BSTR bstrFileName);
[id(2), helpstring("方法LoadFromFileSized")] HRESULT LoadFromFileSized([in] BSTR bstrFileName, [in] INT nHeight);
[id(3)] HRESULT GetRECT( [out] RECT *rc );
[id(4)] HRESULT ShowHint( );
[id(5)] HRESULT SetHostWindow( [in] long hwndHostWindow, [in] INT nNotyfyMode );
[id(6)] HRESULT LoadFlash( [in] BSTR bstrFileName, [in] BSTR bstrFlashVars, [in] INT nWidth, [in] INT nHeight );
[id(7)] HRESULT SetTooltip( [in] BSTR bstrHint);
[id(8)] HRESULT GetTooltip( [out, retval] BSTR * bstrHint);
[id(9)] HRESULT GetFilePath( [out, retval] BSTR * bstPath);
};
通过导入ActiveX后生成的类:
///////////////////////////////////////////////////////////////////////////// // CGifCtrl wrapper class class CGifCtrl : public CWnd { protected: DECLARE_DYNCREATE(CGifCtrl) public: CLSID const& GetClsid() { static CLSID const clsid = { 0xc44ec05f, 0x86a8, 0x410c, { 0xba, 0xcd, 0xf8, 0xdd, 0x18, 0x6c, 0xfc, 0x46 } }; return clsid; } virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL) { return CreateControl(GetClsid(), lpszWindowName, dwStyle, rect, pParentWnd, nID); } BOOL Create(LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CFile* pPersist = NULL, BOOL bStorage = FALSE, BSTR bstrLicKey = NULL) { return CreateControl(GetClsid(), lpszWindowName, dwStyle, rect, pParentWnd, nID, pPersist, bStorage, bstrLicKey); } // Attributes public: // Operations public: void SetBorderColor(unsigned long newValue); unsigned long GetBorderColor(); long GetHwnd(); void LoadFromFile(LPCTSTR bstrFileName); void LoadFromFileSized(LPCTSTR bstrFileName, long nHeight); // method 'GetRECT' not emitted because of invalid return type or parameter type // method 'OnMsg' not emitted because of invalid return type or parameter type void ShowHint(); void SetHostWindow(long hwndHostWindow, long nNotyfyMode); void LoadFlash(LPCTSTR bstrFileName, LPCTSTR bstrFlashVars, long nWidth, long nHeight); void SetTooltip(LPCTSTR bstrHint); CString GetTooltip(); CString GetFilePath(); };
通过该COM组件,demo如下:
下面介绍控件的使用方法,包括两种形式:1、在对话框中使用;2、在RichEdit中使用。
1、在对话框中使用:
首先注册控件GifOleControl .dll,将GifOleControl .dll拷贝到系统目录下,然后使用命令regsvr32 GifOleControl .dll注册控件。
在对话框资源编辑器中,右击并选择“插入ActiveX控件”,选择“Gifctrl class”,插入本控件。然后在插入的ActiveX控件上右击,并选择“添加变量”,生成对应的包装类和对应的实例,这样就可以调用对象的方法了。
2、 在RichEdit中使用
在RichEdit中使用的方法和上篇文章中的方法基本相同,只需替换调对应的类和函数调用即可。但是在这里,可以直接使用接口和接口GUID的定义。具体的代码如下
IRichEditOle *lpRichEditOle = m_rich.GetIRichEditOle(); if (NULL == lpRichEditOle) { return ; } IStorage* lpStorage = NULL; IOleObject* lpOleObject = NULL; LPLOCKBYTES lpLockBytes = NULL; IOleClientSite* lpOleClientSite = NULL; IGifCtrl *lpDynamicGif = NULL; CLSID clsid; REOBJECT reobject; HRESULT hr; hr = ::CoCreateInstance(CLSID_GifCtrl,0,CLSCTX_INPROC, IID_IGifCtrl, (LPVOID*)&lpDynamicGif); int n = GetLastError(); if( lpDynamicGif == NULL ) { return ; } if (FAILED(hr)) { return ; } CString szPath = m_path;//("c://21.gif"); BSTR path = szPath.AllocSysString(); try { try { lpDynamicGif->LoadFromFile(path); BSTR p= lpDynamicGif->GetFilePath(); } catch (...) { AfxThrowOleException(hr); } if (FAILED(hr)) { AfxThrowOleException(hr); } //获取OLE对象接口 hr = lpDynamicGif->QueryInterface(IID_IOleObject,(void**)&lpOleObject); if (FAILED(hr)) { AfxThrowOleException(hr); } OleSetContainedObject(lpOleObject,TRUE); //获取CLSID hr = lpOleObject->GetUserClassID(&clsid); if (FAILED(hr)) { AfxThrowOleException(hr); } hr = ::CreateILockBytesOnHGlobal(NULL,TRUE,&lpLockBytes); //创建LOCKBYTE对象 if (FAILED(hr)) { AfxThrowOleException(hr); } ASSERT(lpLockBytes != NULL); hr = ::StgCreateDocfileOnILockBytes(lpLockBytes, STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE, 0,&lpStorage); if (FAILED(hr)) { VERIFY(lpLockBytes->Release() == 0); lpLockBytes = NULL; AfxThrowOleException(hr); } OleSetContainedObject(lpOleObject,TRUE); lpRichEditOle->GetClientSite(&lpOleClientSite); ZeroMemory(&reobject,sizeof(REOBJECT)); //初始化一对象 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; reobject.dwUser = (DWORD)lpDynamicGif; lpOleObject->SetClientSite(lpOleClientSite); lpDynamicGif->SetHostWindow((long)m_rich.GetSafeHwnd(), 1); lpOleObject->DoVerb(OLEIVERB_SHOW, NULL, lpOleClientSite, -1, m_hWnd, NULL); CString szPath2("22222222222"); BSTR path2 = szPath2.AllocSysString(); lpDynamicGif->SetTooltip(path2); // 设置GIF的toolt lpDynamicGif->ShowHint(); szPath2.AllocSysString(); hr = lpRichEditOle->InsertObject(&reobject); if (FAILED(hr)) { AfxThrowOleException(hr); } RedrawWindow(); //刷新窗体 } catch (CException* e) { e->Delete(); } lpRichEditOle->Release(); lpRichEditOle = NULL;
DEMO下载:http://sv004d.mofile.com/7125120180791962/RGlzazEvNTQvNTQyNzI4MTQyMC83Lzc0OTQ0NjQwNjA4NjQ1Nw../TestGif.rar