如何在对话框上现实gif图片?急!!!

suwenk 2003-09-29 10:38:58
看前面的帖子说可以用CPictureEx的类 可是怎样找到这个类的源文件哪?
那个codeproject的网站很难登陆 谁有给俺一个 不胜感激!!!
...全文
82 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
suwenk 2003-09-30
  • 打赏
  • 举报
回复
谢谢各位。忘记写email了 现在补上。
suwenk@sina.com或suwenk@163.com
seasoft2003 2003-09-30
  • 打赏
  • 举报
回复
老兄,上面的那些都是我发的,可CPP发了还不到1/6,就不让发了,没办法,如果需要,就给我发封邮件吧,我给你传过去。yintongshun@sina.com
yintongshun 2003-09-30
  • 打赏
  • 举报
回复
inline int CPictureEx::TGIFLSDescriptor::GetPackedValue(enum LSDPackedValues Value)
{
int nRet = (int)m_cPacked;

switch (Value)
{
case LSD_PACKED_GLOBALCT:
nRet = nRet >> 7;
break;

case LSD_PACKED_CRESOLUTION:
nRet = ((nRet & 0x70) >> 4) + 1;
break;

case LSD_PACKED_SORT:
nRet = (nRet & 8) >> 3;
break;

case LSD_PACKED_GLOBALCTSIZE:
nRet &= 7;
break;
};

return nRet;
}

inline int CPictureEx::TGIFImageDescriptor::GetPackedValue(enum IDPackedValues Value)
{
int nRet = (int)m_cPacked;

switch (Value)
{
case ID_PACKED_LOCALCT:
nRet >>= 7;
break;

case ID_PACKED_INTERLACE:
nRet = ((nRet & 0x40) >> 6);
break;

case ID_PACKED_SORT:
nRet = (nRet & 0x20) >> 5;
break;

case ID_PACKED_LOCALCTSIZE:
nRet &= 7;
break;
};

return nRet;
}


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPictureEx::CPictureEx()
{
// check structures size
ASSERT(sizeof(TGIFImageDescriptor) == 10);
ASSERT(sizeof(TGIFAppExtension) == 14);
ASSERT(sizeof(TGIFPlainTextExt) == 15);
ASSERT(sizeof(TGIFLSDescriptor) == 7);
ASSERT(sizeof(TGIFControlExt) == 8);
ASSERT(sizeof(TGIFCommentExt) == 2);
ASSERT(sizeof(TGIFHeader) == 6);

m_pGIFLSDescriptor = NULL;
m_pGIFHeader = NULL;
m_pPicture = NULL;
m_pRawData = NULL;
m_hThread = NULL;
m_hBitmap = NULL;
m_hMemDC = NULL;
m_bIsInitialized = FALSE;
m_bExitThread = FALSE;
m_bIsGIF = FALSE;
m_clrBackground = RGB(255,255,255); // white by default
m_nGlobalCTSize = 0;
m_nCurrOffset = 0;
m_nDataSize = 0;
m_PictureSize.cx = m_PictureSize.cy = 0;

m_hExitEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
}

CPictureEx::~CPictureEx()
{
UnLoad();
CloseHandle(m_hExitEvent);
}

BEGIN_MESSAGE_MAP(CPictureEx, CStatic)
//{{AFX_MSG_MAP(CMyStatic)
ON_WM_DESTROY()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CPictureEx::Load(HGLOBAL hGlobal, DWORD dwSize)
{
IStream *pStream = NULL;
UnLoad();

if (!(m_pRawData = reinterpret_cast<unsigned char*> (GlobalLock(hGlobal))) )
{
TRACE(_T("Load: Error locking memory\n"));
return FALSE;
};

m_nDataSize = dwSize;
m_pGIFHeader = reinterpret_cast<TGIFHeader *> (m_pRawData);

if ((memcmp(&m_pGIFHeader->m_cSignature,"GIF",3) != 0) &&
((memcmp(&m_pGIFHeader->m_cVersion,"87a",3) != 0) ||
(memcmp(&m_pGIFHeader->m_cVersion,"89a",3) != 0)) )
{
// it's neither GIF87a nor GIF89a
// do the default processing

// clear GIF variables
m_pRawData = NULL;
GlobalUnlock(hGlobal);

// don't delete memory on object's release
if (CreateStreamOnHGlobal(hGlobal,FALSE,&pStream) != S_OK)
return FALSE;

if (OleLoadPicture(pStream,dwSize,FALSE,IID_IPicture,
reinterpret_cast<LPVOID *>(&m_pPicture)) != S_OK)
{
pStream->Release();
return FALSE;
};
pStream->Release();

// store picture's size

long hmWidth;
long hmHeight;
m_pPicture->get_Width(&hmWidth);
m_pPicture->get_Height(&hmHeight);

HDC hDC = ::GetDC(m_hWnd);
m_PictureSize.cx = MulDiv(hmWidth, GetDeviceCaps(hDC,LOGPIXELSX), 2540);
m_PictureSize.cy = MulDiv(hmHeight, GetDeviceCaps(hDC,LOGPIXELSY), 2540);
::ReleaseDC(m_hWnd,hDC);
}
else
{
// it's a GIF
m_bIsGIF = TRUE;
m_pGIFLSDescriptor = reinterpret_cast<TGIFLSDescriptor *>
(m_pRawData + sizeof(TGIFHeader));
if (m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCT) == 1)
{
// calculate the globat color table size
m_nGlobalCTSize = static_cast<int>
(3*pow(2,m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCTSIZE)+1));
// get the background color if GCT is present
unsigned char *pBkClr = m_pRawData + sizeof(TGIFHeader) +
sizeof(TGIFLSDescriptor) + 3*m_pGIFLSDescriptor->m_cBkIndex;
m_clrBackground = RGB(pBkClr[0],pBkClr[1],pBkClr[2]);
};

// store the picture's size
m_PictureSize.cx = m_pGIFLSDescriptor->m_wWidth;
m_PictureSize.cy = m_pGIFLSDescriptor->m_wHeight;

// determine frame count for this picture
UINT nFrameCount=0;
ResetDataPointer();
while (SkipNextGraphicBlock())
nFrameCount++;

#ifdef GIF_TRACING
TRACE(
_T(" -= GIF encountered\n"
"Logical Screen dimensions = %dx%d\n"
"Global color table = %d\n"
"Color depth = %d\n"
"Sort flag = %d\n"
"Size of Global Color Table = %d\n"
"Background color index = %d\n"
"Pixel aspect ratio = %d\n"
"Frame count = %d\n"
"Background color = %06Xh\n\n"
),
m_pGIFLSDescriptor->m_wWidth,
m_pGIFLSDescriptor->m_wHeight,
m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCT),
m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_CRESOLUTION),
m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_SORT),
m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCTSIZE),
m_pGIFLSDescriptor->m_cBkIndex,
m_pGIFLSDescriptor->m_cPixelAspect,
nFrameCount,
m_clrBackground
);
EnumGIFBlocks();
#endif

if (nFrameCount == 0) // it's an empty GIF!
{
m_pRawData = NULL;
GlobalUnlock(hGlobal);
return FALSE;
};

// now check the frame count
// if there's only one frame, no need to animate this GIF
// therefore, treat it like any other pic

if (nFrameCount == 1)
{
// clear GIF variables
m_pRawData = NULL;
GlobalUnlock(hGlobal);

// don't delete memory on object's release
if (CreateStreamOnHGlobal(hGlobal,FALSE,&pStream) != S_OK)
return FALSE;

if (OleLoadPicture(pStream,dwSize,FALSE,IID_IPicture,
(LPVOID *)&m_pPicture) != S_OK)
{
pStream->Release();
return FALSE;
};

pStream->Release();
}
else
{
// if, on the contrary, there are several frames
// then store separate frames in an array

TFrame frame;
UINT nBlockLen;
HGLOBAL hFrameData;
UINT nCurFrame = 0;

ResetDataPointer();
while (hFrameData = GetNextGraphicBlock(&nBlockLen,
&frame.m_nDelay, &frame.m_frameSize,
&frame.m_frameOffset, &frame.m_nDisposal) )
{
#ifdef GIF_TRACING
//////////////////////////////////////////////
// uncomment the following strings if you want
// to write separate frames on disk
//
// CString szName;
// szName.Format(_T("%.4d.gif"),nCurFrame);
// WriteDataOnDisk(szName,hFrameData,nBlockLen);
// nCurFrame++;
#endif // GIF_TRACING

IStream *pStream = NULL;

// delete memory on object's release
if (CreateStreamOnHGlobal(hFrameData,TRUE,&pStream) != S_OK)
{
GlobalFree(hFrameData);
continue;
};

if (OleLoadPicture(pStream,nBlockLen,FALSE,
IID_IPicture,
reinterpret_cast<LPVOID *>(&frame.m_pPicture)) != S_OK)
{
pStream->Release();
continue;
};
pStream->Release();

// everything went well, add this frame
m_arrFrames.push_back(frame);
};

// clean after ourselves
m_pRawData = NULL;
GlobalUnlock(hGlobal);

if (m_arrFrames.empty()) // couldn't load any frames
return FALSE;
};
}; // if (!IsGIF...

return PrepareDC(m_PictureSize.cx,m_PictureSize.cy);
}
yintongshun 2003-09-30
  • 打赏
  • 举报
回复
//CPictureEX.cpp
#include "stdafx.h"
#include "PictureEx.h"
#include <math.h>
#include <process.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Nested structures member functions
//////////////////////////////////////////////////////////////////////

inline int CPictureEx::TGIFControlExt::GetPackedValue(enum ControlExtValues Value)
{
int nRet = (int)m_cPacked;
switch (Value)
{
case GCX_PACKED_DISPOSAL:
nRet = (nRet & 28) >> 2;
break;

case GCX_PACKED_USERINPUT:
nRet = (nRet & 2) >> 1;
break;

case GCX_PACKED_TRANSPCOLOR:
nRet &= 1;
break;
};

return nRet;
}

inline int CPictureEx::TGIFLSDescriptor::GetPackedValue(enum LSDPackedValues Value)
{
int nRet = (int)m_cPacked;

switch (Value)
{
case LSD_PACKED_GLOBALCT:
nRet = nRet >> 7;
break;

case LSD_PACKED_CRESOLUTION:
nRet = ((nRet & 0x70) >> 4) + 1;
break;

case LSD_PACKED_SORT:
nRet = (nRet & 8) >> 3;
break;

case LSD_PACKED_GLOBALCTSIZE:
nRet &= 7;
break;
};

return nRet;
}

yintongshun 2003-09-30
  • 打赏
  • 举报
回复
//CPictureEX.h
#if !defined(AFX_PICTUREEX_H__0EFE5DE0_7B68_4DB7_8B34_5DC634948438__INCLUDED_)
#define AFX_PICTUREEX_H__0EFE5DE0_7B68_4DB7_8B34_5DC634948438__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <vector>

//#define GIF_TRACING // uncomment it if you want detailed TRACEs

class CPictureEx : public CStatic
{
public:

struct TFrame // structure that keeps a single frame info
{
IPicture *m_pPicture; // pointer to the interface used for drawing
SIZE m_frameSize;
SIZE m_frameOffset;
UINT m_nDelay; // delay (in 1/100s of a second)
UINT m_nDisposal; // disposal method
};

#pragma pack(1) // turn byte alignment on

enum GIFBlockTypes
{
BLOCK_UNKNOWN,
BLOCK_APPEXT,
BLOCK_COMMEXT,
BLOCK_CONTROLEXT,
BLOCK_PLAINTEXT,
BLOCK_IMAGE,
BLOCK_TRAILER
};

enum ControlExtValues // graphic control extension packed field values
{
GCX_PACKED_DISPOSAL, // disposal method
GCX_PACKED_USERINPUT,
GCX_PACKED_TRANSPCOLOR
};

enum LSDPackedValues // logical screen descriptor packed field values
{
LSD_PACKED_GLOBALCT,
LSD_PACKED_CRESOLUTION,
LSD_PACKED_SORT,
LSD_PACKED_GLOBALCTSIZE
};

enum IDPackedValues // image descriptor packed field values
{
ID_PACKED_LOCALCT,
ID_PACKED_INTERLACE,
ID_PACKED_SORT,
ID_PACKED_LOCALCTSIZE
};

struct TGIFHeader // GIF header
{
char m_cSignature[3]; // Signature - Identifies the GIF Data Stream
// This field contains the fixed value 'GIF'
char m_cVersion[3]; // Version number. May be one of the following:
// "87a" or "89a"
};

struct TGIFLSDescriptor // Logical Screen Descriptor
{
WORD m_wWidth; // 2 bytes. Logical screen width
WORD m_wHeight; // 2 bytes. Logical screen height

unsigned char m_cPacked; // packed field

unsigned char m_cBkIndex; // 1 byte. Background color index
unsigned char m_cPixelAspect; // 1 byte. Pixel aspect ratio
inline int GetPackedValue(enum LSDPackedValues Value);
};

struct TGIFAppExtension // application extension block
{
unsigned char m_cExtIntroducer; // extension introducer (0x21)
unsigned char m_cExtLabel; // app. extension label (0xFF)
unsigned char m_cBlockSize; // fixed value of 11
char m_cAppIdentifier[8]; // application identifier
char m_cAppAuth[3]; // application authentication code
};

struct TGIFControlExt // graphic control extension block
{
unsigned char m_cExtIntroducer; // extension introducer (0x21)
unsigned char m_cControlLabel; // control extension label (0xF9)
unsigned char m_cBlockSize; // fixed value of 4
unsigned char m_cPacked; // packed field
WORD m_wDelayTime; // delay time
unsigned char m_cTColorIndex; // transparent color index
unsigned char m_cBlockTerm; // block terminator (0x00)
public:
inline int GetPackedValue(enum ControlExtValues Value);
};

struct TGIFCommentExt // comment extension block
{
unsigned char m_cExtIntroducer; // extension introducer (0x21)
unsigned char m_cCommentLabel; // comment extension label (0xFE)
};

struct TGIFPlainTextExt // plain text extension block
{
unsigned char m_cExtIntroducer; // extension introducer (0x21)
unsigned char m_cPlainTextLabel; // text extension label (0x01)
unsigned char m_cBlockSize; // fixed value of 12
WORD m_wLeftPos; // text grid left position
WORD m_wTopPos; // text grid top position
WORD m_wGridWidth; // text grid width
WORD m_wGridHeight; // text grid height
unsigned char m_cCellWidth; // character cell width
unsigned char m_cCellHeight; // character cell height
unsigned char m_cFgColor; // text foreground color index
unsigned char m_cBkColor; // text background color index
};

struct TGIFImageDescriptor // image descriptor block
{
unsigned char m_cImageSeparator; // image separator byte (0x2C)
WORD m_wLeftPos; // image left position
WORD m_wTopPos; // image top position
WORD m_wWidth; // image width
WORD m_wHeight; // image height
unsigned char m_cPacked; // packed field
inline int GetPackedValue(enum IDPackedValues Value);
};

#pragma pack() // turn byte alignment off

public:
CPictureEx();
virtual ~CPictureEx();
void Stop(); // stops animation
void UnLoad(); // stops animation plus releases all resources

BOOL IsGIF() const;
BOOL IsAnimatedGIF() const;
SIZE GetSize() const;
int GetFrameCount() const;
COLORREF GetBkColor() const;
void SetBkColor(COLORREF clr);

// draws the picture (starts an animation thread if needed)
BOOL Draw();

// loads a picture from a file
// i.e. Load(_T("mypic.gif"));
BOOL Load(LPCTSTR szFileName);

// loads a picture from a global memory block (allocated by GlobalAlloc)
// Warning: this function DOES NOT free the global memory, pointed to by hGlobal
BOOL Load(HGLOBAL hGlobal, DWORD dwSize);

// loads a picture from a program resource
// i.e. Load(MAKEINTRESOURCE(IDR_MYPIC),_T("GIFTYPE"));
BOOL Load(LPCTSTR szResourceName,LPCTSTR szResourceType);

protected:

#ifdef GIF_TRACING
void EnumGIFBlocks();
void WriteDataOnDisk(CString szFileName, HGLOBAL hData, DWORD dwSize);
#endif // GIF_TRACING

SIZE m_PictureSize;
COLORREF m_clrBackground;
UINT m_nDataSize;
UINT m_nCurrOffset;
UINT m_nGlobalCTSize;
BOOL m_bIsGIF;
BOOL m_bExitThread;
BOOL m_bIsInitialized;
HDC m_hMemDC;
HBITMAP m_hBitmap;
HBITMAP m_hOldBitmap;
HANDLE m_hThread;
HANDLE m_hExitEvent;
IPicture * m_pPicture;
TGIFHeader * m_pGIFHeader;
unsigned char * m_pRawData;
TGIFLSDescriptor * m_pGIFLSDescriptor;
std::vector<TFrame> m_arrFrames;

void ThreadAnimation();
static UINT WINAPI _ThreadAnimation(LPVOID pParam);

int GetNextBlockLen() const;
BOOL SkipNextBlock();
BOOL SkipNextGraphicBlock();
BOOL PrepareDC(int nWidth, int nHeight);
void ResetDataPointer();
enum GIFBlockTypes GetNextBlock() const;
UINT GetSubBlocksLen(UINT nStartingOffset) const;
HGLOBAL GetNextGraphicBlock(UINT *pBlockLen, UINT *pDelay,
SIZE *pBlockSize, SIZE *pBlockOffset, UINT *pDisposal);

// Generated message map functions
//{{AFX_MSG(CMyStatic)
afx_msg void OnDestroy();
afx_msg void OnPaint();
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
};

#endif // !defined(AFX_PICTUREEX_H__0EFE5DE0_7B68_4DB7_8B34_5DC634948438__INCLUDED_)
wuxfBrave 2003-09-30
  • 打赏
  • 举报
回复
加入控件就可以了啊,Project->Add to project->Compontes and Controls
应该有的,我机器上就有啊
suwenk 2003-09-30
  • 打赏
  • 举报
回复
感谢yintongshun(踏雪有痕) !!
你的email已经收到
tonybaobao 2003-09-29
  • 打赏
  • 举报
回复
http://www.codeproject.com/bitmap/pictureex.asp?target=cpictureex

这上面有具体使用方法。
tonybaobao 2003-09-29
  • 打赏
  • 举报
回复
我有CPictureEx这个类,但是老实说,效果也不是那么好,并不是所有得gif他都能正常显示。他得文件里面写着:
// Picture displaying control with support for the following formats:
// GIF (including animated GIF87a and GIF89a), JPEG, BMP, WMF, ICO, CUR

你要得话,发消息给我,我发给你,请附上email地址。
fengqinggao 2003-09-29
  • 打赏
  • 举报
回复
要图片还是动画?我这里有一个能加载图片的类,要的话,留Email吧,:)

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧