以下是VC播放gif动画的代码,请在阅读中解决问题
void CPlayGifDlg::OnButton1()
{
// TODO: Add your control notification handler code here
//以下代码是我加入的,意思是得到当前可执行文件的绝对路径
TCHAR szPathName[MAX_PATH];
GetModuleFileName(NULL, szPathName, MAX_PATH);
CString strPath = szPathName;
int nPos;
nPos = strPath.ReverseFind (_T('\\'));
strPath = strPath.Left(nPos); strPath += _T("\\123.gif"); //以下代码可以直接打开所示地址的gif,
//我想将上面的路径strPath带入下面,变为Image image(strPath);
//可是变了以后就不能编译了
//请前辈们给出一个方法,可以直接利用路径strPath播放该gif动画
Image image(L"C:\\123.gif"); // UINT uiCount = image.GetFrameDimensionsCount();
GUID *pDimensionIDs=(GUID*)new GUID[uiCount];
image.GetFrameDimensionsList(pDimensionIDs, uiCount);
UINT uiFrameCount=image.GetFrameCount(&pDimensionIDs[0]);
delete []pDimensionIDs; UINT uiSize;
uiSize = image.GetPropertyItemSize(PropertyTagFrameDelay);
PropertyItem* pItem = (PropertyItem*)malloc(uiSize);
image.GetPropertyItem(PropertyTagFrameDelay, uiSize, pItem); GUID Guid = FrameDimensionTime;
CDC* pDC = GetDC();
while(true)
{
Graphics gh(pDC->m_hDC); //hDC是外部传入的画图DC
gh.DrawImage(&image,0, 0, image.GetWidth(), image.GetHeight());
image.SelectActiveFrame(&Guid, uiCount++);//重新设置当前的活动数据帧
if(uiCount == uiFrameCount) uiCount= 0;
long lPause = ((long*)(pItem->value))[uiCount];//计算此帧要延迟的时间
Sleep(lPause);
}
}
void CPlayGifDlg::OnButton1()
{
// TODO: Add your control notification handler code here
//以下代码是我加入的,意思是得到当前可执行文件的绝对路径
TCHAR szPathName[MAX_PATH];
GetModuleFileName(NULL, szPathName, MAX_PATH);
CString strPath = szPathName;
int nPos;
nPos = strPath.ReverseFind (_T('\\'));
strPath = strPath.Left(nPos); strPath += _T("\\123.gif"); //以下代码可以直接打开所示地址的gif,
//我想将上面的路径strPath带入下面,变为Image image(strPath);
//可是变了以后就不能编译了
//请前辈们给出一个方法,可以直接利用路径strPath播放该gif动画
Image image(L"C:\\123.gif"); // UINT uiCount = image.GetFrameDimensionsCount();
GUID *pDimensionIDs=(GUID*)new GUID[uiCount];
image.GetFrameDimensionsList(pDimensionIDs, uiCount);
UINT uiFrameCount=image.GetFrameCount(&pDimensionIDs[0]);
delete []pDimensionIDs; UINT uiSize;
uiSize = image.GetPropertyItemSize(PropertyTagFrameDelay);
PropertyItem* pItem = (PropertyItem*)malloc(uiSize);
image.GetPropertyItem(PropertyTagFrameDelay, uiSize, pItem); GUID Guid = FrameDimensionTime;
CDC* pDC = GetDC();
while(true)
{
Graphics gh(pDC->m_hDC); //hDC是外部传入的画图DC
gh.DrawImage(&image,0, 0, image.GetWidth(), image.GetHeight());
image.SelectActiveFrame(&Guid, uiCount++);//重新设置当前的活动数据帧
if(uiCount == uiFrameCount) uiCount= 0;
long lPause = ((long*)(pItem->value))[uiCount];//计算此帧要延迟的时间
Sleep(lPause);
}
}
如果不是UNICODE工程,那么就用宏A2W或API MultiByteToWideChar转换一下.
(其实A2W也是调用MultiByteToWideChar)
因为GDIPlus的字符串、文件路径等都需要传递UNICODE.
Image image("123.gif"); //
就可以打开程序所在文件夹中的123.gif
但是为了能够开机启动,还是需要转换的,因为开机启动需要绝对地址。
错误的结果如下:
C:\Documents and Settings\Administrator\桌面\PlayGif\PlayGifDlg.cpp(186) : error C2664: '__thiscall Gdiplus::Image::Gdiplus::Image(const unsigned short *,int)' : cannot convert parameter 1 from 'class CString' to 'const unsigned short *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
GetModuleFileNameW(NULL, szPathName, MAX_PATH);
CStringW strPath = szPathName;
int nPos;
nPos = strPath.ReverseFind (L'\\');
strPath = strPath.Left(nPos);
strPath += L"\\123.gif;