这是CSDN的VC论坛上的一个老FAQ了。我在写自定义在RichEdit中插入对象的图标(http://www.blogcn.com/user3/jiangsheng/blog/1319738.html)这片文章的时候就是想用这个技术做动画GIF的,但是怎么判断一个内嵌在RichEdit的对象是GIF这个问题一直没有解决。好在QQ附带的一个控件支持动画GIF,可以插入这个对象来解决问题。
首先需要一个定时器来定时更新GIF。
public: System::Void OnLoad(System::Object^ sender, System::EventArgs^ e)
{
this->typingRichTextBox->RichTextShortcutsEnabled=false;
this->timer1->Start();
}
private: System::Void OnFormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e)
{
this->timer1->Stop();
this->frameClosing=true;
}
然后在定时器的处理函数里面通知GIF控件更新显示。
private: System::Void OnTimer1Elapsed(System::Object^ sender, System::Timers::ElapsedEventArgs^ e)
{
if(this->frameClosing==false)
UnmanagedGifTriggerFrameChange(this->contentRichTextBox->Handle.ToInt32());
}
最后的工作就是插入GIF了
private: System::Void smileToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
{
insertemotion(sender,e,"c:\\Program Files\\Tencent\\QQ\\Face\\20.gif");
}
private: System::Void insertemotion(System::Object^ sender, System::EventArgs^ e,String ^ gifPath)
{
System::Windows::Forms::RichTextBox^ ptypingRichTextBox=this->typingRichTextBox;
stdcli::language::pin_ptr< wchar_t> wch = PtrToStringChars(gifPath);
UnmanagedInsertGif(ptypingRichTextBox->Handle.ToInt32(),wch );
}
为了偷懒起见关键的代码还是用Native C++来写,所以这些函数名全部以Unmanaged开头。
//unmanagedwin32.h
//混合托管和非托管编程的话,编译的时候不能使用/clr:safe和/clr:pure开关,
#pragma once
#pragma unmanaged//用这个开关来切换托管和非托管代码
extern void UnmanagedScrollToButton(int hwndRichEdit);
extern void UnmanagedInsertGif(int hwndRichEdit,wchar_t * pFilePath);
extern void UnmanagedGifTriggerFrameChange(int hwndRichEdit);
#pragma managed
//unmanagedwin32.cc
//使用了Windows 平台SDK
//必须要使用clr:oldSyntax来避免SDK头文件和C++/CLI语法的冲突
//而且在Visual C++ 2005 Express中要添加/d1PrivateNativeTypes 开关以避免混合LNK2022错误。
#define UNICODE
#define _UNICODE
#define _WIN32_DCOM
#include "windows.h"
#include "Richedit.h"
#include "Richole.h"
#pragma comment( lib, "User32.lib" )
#include "UnmanagedWin32.h"
//纯用C编写自动化操作会死人的,幸好可以自动导入
#import "c:\\Program files\\tencent\\qq\\ImageOle.dll" named_guids
//移动光标到末尾然后调用System::Windows::Forms::RichTextBox的ScrollToCaret方法,需要拖动滚动条才可以看到最后一行之前的文字。老办法,API伺候。
void UnmanagedScrollToButton(int hwndRichEdit)
{
HWND h=(HWND)hwndRichEdit;
int line = SendMessage(h, EM_GETFIRSTVISIBLELINE, 0, 0);
int linecount = SendMessage(h, EM_GETLINECOUNT, 0, 0);
SendMessage(h, EM_LINESCROLL, 0, (linecount - line - 2));
}
下面的函数UnmanagedInsertGif(int hwndRichEdit,wchar_t * pFilePath)是插入一个ImageOle::GifAnimator对象,UnmanagedGifTriggerFrameChange(int hwndRichEdit)是枚举richedit中已经插入的对象,如果是ImageOle::GifAnimator对象,那么调用其TriggerFrameChange方法。
void UnmanagedInsertGif(int hwndRichEdit,wchar_t * pFilePath) { HWND h=(HWND)hwndRichEdit; LPRICHEDITOLE lpRichEditOle=NULL; LPOLEOBJECT lpObject=NULL; LPSTORAGE lpStorage=NULL; LPOLECLIENTSITE lpClientSite=NULL; LPLOCKBYTES lpLockBytes = NULL; REOBJECT reobject; ZeroMemory(&reobject, sizeof(REOBJECT)); reobject.cbStruct = sizeof(REOBJECT); HRESULT hr=S_OK; CLSID clsid=CLSID_NULL; do{ ::SendMessage(h, EM_GETOLEINTERFACE, 0, (LPARAM)&lpRichEditOle); if(lpRichEditOle==NULL)break; hr= ::CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes); if (hr != S_OK||lpLockBytes==NULL) break; hr= ::StgCreateDocfileOnILockBytes(lpLockBytes, STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &lpStorage); if (hr!= S_OK||lpStorage==NULL) break; hr=lpRichEditOle->GetClientSite(&lpClientSite); if (hr!= S_OK||lpClientSite==NULL) break; try { ImageOleLib::IGifAnimatorPtr lpAnimator; hr = lpAnimator.CreateInstance(ImageOleLib::CLSID_GifAnimator); if( FAILED(hr) ) _com_issue_error(hr); _bstr_t bstrPath(pFilePath); hr = lpAnimator->LoadFromFile(bstrPath); if( FAILED(hr) ) _com_issue_error(hr); hr = lpAnimator.QueryInterface(IID_IOleObject, (void**)&lpObject); if( FAILED(hr)||lpObject==NULL) _com_issue_error(hr); hr=OleSetContainedObject(lpObject, TRUE); if( FAILED(hr) ) _com_issue_error(hr); hr=lpObject->GetUserClassID(&clsid); if( FAILED(hr) ) _com_issue_error(hr); reobject.clsid = clsid; reobject.cp = REO_CP_SELECTION; reobject.dvaspect = DVASPECT_CONTENT; reobject.dwFlags = REO_BELOWBASELINE; reobject.dwUser = 0; reobject.poleobj = lpObject; reobject.polesite = lpClientSite; reobject.pstg = lpStorage; SIZEL sizel={0,0}; reobject.sizel = sizel; hr=lpRichEditOle->InsertObject(&reobject); } catch( _com_error e ) { LPCTSTR lpszErrMessage=e.ErrorMessage(); } }while(FALSE); if(lpLockBytes) lpObject->Release(); if(lpLockBytes) lpLockBytes->Release(); if(lpClientSite) lpClientSite->Release(); if(lpRichEditOle) lpRichEditOle->Release(); } void UnmanagedGifTriggerFrameChange(int hwndRichEdit) { HWND h=(HWND)hwndRichEdit; LPRICHEDITOLE lpRichEditOle=NULL; LPOLECLIENTSITE lpClientSite=NULL; LPOLECONTAINER lpContainer=NULL; LPENUMUNKNOWN lpEnumUnknown=NULL; HRESULT hr=S_OK; do{ ::SendMessage(h, EM_GETOLEINTERFACE, 0, (LPARAM)&lpRichEditOle); if(lpRichEditOle==NULL)break; hr=lpRichEditOle->GetClientSite(&lpClientSite); if (hr!= S_OK||lpClientSite==NULL) break; hr=lpClientSite->GetContainer(&lpContainer); if (hr!= S_OK||lpClientSite==NULL) break; hr=lpContainer->EnumObjects(OLECONTF_EMBEDDINGS,&lpEnumUnknown); if (hr!= S_OK||lpEnumUnknown==NULL) break; IUnknown* pUnk=NULL; ULONG uFetched=0; for (UINT i = 0; S_OK == lpEnumUnknown->Next(1, &pUnk, &uFetched); i++) { ImageOleLib::IGifAnimator* pAnimator=NULL; do{ hr=pUnk->QueryInterface(__uuidof(ImageOleLib::IGifAnimator),(LPVOID*)&pAnimator); if (hr!= S_OK) break; try{ ImageOleLib::IGifAnimatorPtr lpAnimator; lpAnimator.Attach(pAnimator,true); lpAnimator->TriggerFrameChange(); } catch( _com_error e ) { LPCTSTR lpszErrMessage=e.ErrorMessage(); } } while(FALSE); pUnk->Release(); if(pAnimator) pAnimator->Release(); } }while(FALSE); if(lpEnumUnknown) lpEnumUnknown->Release(); if(lpContainer) lpContainer->Release(); if(lpRichEditOle) lpRichEditOle->Release(); if(lpClientSite) lpClientSite->Release(); }
这两个函数里面的方法也可以用于插入其他类型控件,以及和插入的对象通讯。
使用windows 2000,QQ2004SP1,Visual C++ 2005 Express, Platform SDK (Windows 2003)编译测试。
本文地址:http://com.8s8s.com/it/it369.htm