15,979
社区成员
发帖
与我相关
我的任务
分享
//下面这段代码,调用两次,生成两个包含m_pictureExStatus的类实例。是基于dialog的。
CRect rcPic;
RECT _rect;
m_staticStatus.GetWindowRect( &rcPic);
ScreenToClient( &rcPic);
_rect.left = rcPic.left;
_rect.right = rcPic.right;
_rect.top = rcPic.top;
_rect.bottom = rcPic.bottom;
//m_shockWaveFlash.Create( NULL, WS_CHILD|WS_VISIBLE, m_rect, this, 0xFF);
m_pictureExStatus.Create( _T(""), WS_CHILD|WS_VISIBLE, _rect, this);
///
m_staticType.GetWindowRect( &rcPic);
ScreenToClient( &rcPic);
_rect.left = rcPic.left;
_rect.right = rcPic.right;
_rect.top = rcPic.top;
_rect.bottom = rcPic.bottom;
m_pictureExType.Create( _T(""), WS_CHILD|WS_VISIBLE, _rect, this);
CString strModulePath, strGifFile;
strModulePath = CDirectory2::GetModulePath();
strGifFile = strModulePath + LSF_ZONE_NORMAL;
m_pictureExStatus.Load( strGifFile);
m_pictureExStatus.Draw();
//////////////////////////////////////////////////////////////////////
// PictureEx.cpp: implementation of the CPictureEx class.
//
// Picture displaying control with support for the following formats:
// GIF (including animated GIF87a and GIF89a), JPEG, BMP, WMF, ICO, CUR
//
// Written by Oleg Bykov (oleg_bykoff@rsdn.ru)
// Copyright (c) 2001
//
// To use CPictureEx, follow these steps:
//////////////////////////////////////////////////////////////////////
BOOL CPictureEx::Draw()
{
if (!m_bIsInitialized)
{
TRACE(_T("Call one of the CPictureEx::Load() member functions before calling Draw()\n"));
return FALSE;
};
if (IsAnimatedGIF())
{
// the picture needs animation
// we'll start the thread that will handle it for us
unsigned int nDummy;
m_hThread = (HANDLE) _beginthreadex(NULL,0,_ThreadAnimation,this,
CREATE_SUSPENDED,&nDummy);
if (!m_hThread)
{
TRACE(_T("Draw: Couldn't start a GIF animation thread\n"));
return FALSE;
}
else
ResumeThread(m_hThread);
}
else
{
if (m_pPicture)
{
long hmWidth;
long hmHeight;
m_pPicture->get_Width(&hmWidth);
m_pPicture->get_Height(&hmHeight);
if (m_pPicture->Render(m_hMemDC, 0, 0, m_PictureSize.cx, m_PictureSize.cy,
0, hmHeight, hmWidth, -hmHeight, NULL) == S_OK)
{
Invalidate(FALSE);
return TRUE;
};
};
};
return FALSE;
}
BOOL CPictureEx::Load(LPCTSTR szResourceName, LPCTSTR szResourceType)
{
ASSERT(szResourceName);
ASSERT(szResourceType);
HRSRC hPicture = FindResource(AfxGetResourceHandle(),szResourceName,szResourceType);
HGLOBAL hResData;
if (!hPicture || !(hResData = LoadResource(AfxGetResourceHandle(),hPicture)))
{
TRACE(_T("Load (resource): Error loading resource %s\n"),szResourceName);
return FALSE;
};
DWORD dwSize = SizeofResource(AfxGetResourceHandle(),hPicture);
// hResData is not the real HGLOBAL (we can't lock it)
// let's make it real
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD,dwSize);
if (!hGlobal)
{
TRACE(_T("Load (resource): Error allocating memory\n"));
FreeResource(hResData);
return FALSE;
};
char *pDest = reinterpret_cast<char *> (GlobalLock(hGlobal));
char *pSrc = reinterpret_cast<char *> (LockResource(hResData));
if (!pSrc || !pDest)
{
TRACE(_T("Load (resource): Error locking memory\n"));
GlobalFree(hGlobal);
FreeResource(hResData);
return FALSE;
};
CopyMemory(pDest,pSrc,dwSize);
FreeResource(hResData);
GlobalUnlock(hGlobal);
BOOL bRetValue = Load(hGlobal,dwSize);
GlobalFree(hGlobal);
return bRetValue;
}
UINT WINAPI CPictureEx::_ThreadAnimation(LPVOID pParam)
{
ASSERT(pParam);
CPictureEx *pPic = reinterpret_cast<CPictureEx *> (pParam);
pPic->m_bIsPlaying = TRUE;
pPic->ThreadAnimation();
pPic->m_bIsPlaying = FALSE;
// this thread has finished its work so we close the handle
CloseHandle(pPic->m_hThread);
// and init the handle to zero (so that Stop() doesn't Wait on it)
pPic->m_hThread = 0;
return 0;
}
void CPictureEx::ThreadAnimation()
{
// first, restore background (for stop/draw support)
// disposal method #2
if (m_arrFrames[m_nCurrFrame].m_nDisposal == 2)
{
HBRUSH hBrush = CreateSolidBrush(m_clrBackground);
if (hBrush)
{
RECT rect = {
m_arrFrames[m_nCurrFrame].m_frameOffset.cx,
m_arrFrames[m_nCurrFrame].m_frameOffset.cy,
m_arrFrames[m_nCurrFrame].m_frameOffset.cx + m_arrFrames[m_nCurrFrame].m_frameSize.cx,
m_arrFrames[m_nCurrFrame].m_frameOffset.cy + m_arrFrames[m_nCurrFrame].m_frameSize.cy };
FillRect(m_hMemDC,&rect,hBrush);
DeleteObject(hBrush);
};
}
else
// disposal method #3
if (m_hDispMemDC && (m_arrFrames[m_nCurrFrame].m_nDisposal == 3) )
{
// put it back
BitBlt(m_hMemDC,
m_arrFrames[m_nCurrFrame].m_frameOffset.cx,
m_arrFrames[m_nCurrFrame].m_frameOffset.cy,
m_arrFrames[m_nCurrFrame].m_frameSize.cx,
m_arrFrames[m_nCurrFrame].m_frameSize.cy,
m_hDispMemDC,0,0, SRCCOPY);
// init variables
SelectObject(m_hDispMemDC,m_hDispOldBM);
DeleteDC(m_hDispMemDC); m_hDispMemDC = NULL;
DeleteObject(m_hDispMemBM); m_hDispMemBM = NULL;
};
while (!m_bExitThread)
{
if (m_arrFrames[m_nCurrFrame].m_pPicture)
{
///////////////////////////////////////////////////////
// Before rendering a frame we should take care of what's
// behind that frame. TFrame::m_nDisposal will be our guide:
// 0 - no disposal specified (do nothing)
// 1 - do not dispose (again, do nothing)
// 2 - restore to background color (m_clrBackground)
// 3 - restore to previous
//////// disposal method #3
if (m_arrFrames[m_nCurrFrame].m_nDisposal == 3)
{
// prepare a memory DC and store the background in it
m_hDispMemDC = CreateCompatibleDC(m_hMemDC);
m_hDispMemBM = CreateCompatibleBitmap(m_hMemDC,
m_arrFrames[m_nCurrFrame].m_frameSize.cx,
m_arrFrames[m_nCurrFrame].m_frameSize.cy);
if (m_hDispMemDC && m_hDispMemBM)
{
m_hDispOldBM = reinterpret_cast<HBITMAP> (SelectObject(m_hDispMemDC,m_hDispMemBM));
BitBlt(m_hDispMemDC,0,0,
m_arrFrames[m_nCurrFrame].m_frameSize.cx,
m_arrFrames[m_nCurrFrame].m_frameSize.cy,
m_hMemDC,
m_arrFrames[m_nCurrFrame].m_frameOffset.cx,
m_arrFrames[m_nCurrFrame].m_frameOffset.cy,
SRCCOPY);
};
};
///////////////////////
long hmWidth;
long hmHeight;
m_arrFrames[m_nCurrFrame].m_pPicture->get_Width(&hmWidth);
m_arrFrames[m_nCurrFrame].m_pPicture->get_Height(&hmHeight);
if (m_arrFrames[m_nCurrFrame].m_pPicture->Render(m_hMemDC,
m_arrFrames[m_nCurrFrame].m_frameOffset.cx,
m_arrFrames[m_nCurrFrame].m_frameOffset.cy,
m_arrFrames[m_nCurrFrame].m_frameSize.cx,
m_arrFrames[m_nCurrFrame].m_frameSize.cy,
0, hmHeight, hmWidth, -hmHeight, NULL) == S_OK)
{
Invalidate(FALSE);
};
if (m_bExitThread) break;
// if the delay time is too short (like in old GIFs), wait for 100ms
if (m_arrFrames[m_nCurrFrame].m_nDelay < 5)
WaitForSingleObject(m_hExitEvent, 100);
else
WaitForSingleObject(m_hExitEvent, 10*m_arrFrames[m_nCurrFrame].m_nDelay);
if (m_bExitThread) break;
// disposal method #2
if (m_arrFrames[m_nCurrFrame].m_nDisposal == 2)
{
HBRUSH hBrush = CreateSolidBrush(m_clrBackground);
if (hBrush)
{
RECT rect = {
m_arrFrames[m_nCurrFrame].m_frameOffset.cx,
m_arrFrames[m_nCurrFrame].m_frameOffset.cy,
m_arrFrames[m_nCurrFrame].m_frameOffset.cx + m_arrFrames[m_nCurrFrame].m_frameSize.cx,
m_arrFrames[m_nCurrFrame].m_frameOffset.cy + m_arrFrames[m_nCurrFrame].m_frameSize.cy };
FillRect(m_hMemDC,&rect,hBrush);
DeleteObject(hBrush);
};
}
else
if (m_hDispMemDC && (m_arrFrames[m_nCurrFrame].m_nDisposal == 3) )
{
// put it back
BitBlt(m_hMemDC,
m_arrFrames[m_nCurrFrame].m_frameOffset.cx,
m_arrFrames[m_nCurrFrame].m_frameOffset.cy,
m_arrFrames[m_nCurrFrame].m_frameSize.cx,
m_arrFrames[m_nCurrFrame].m_frameSize.cy,
m_hDispMemDC,0,0, SRCCOPY);
// init variables
SelectObject(m_hDispMemDC,m_hDispOldBM);
DeleteDC(m_hDispMemDC); m_hDispMemDC = NULL;
DeleteObject(m_hDispMemBM); m_hDispMemBM = NULL;
};
};
m_nCurrFrame++;
if (m_nCurrFrame == m_arrFrames.size())
{
m_nCurrFrame
= 0;
// init the screen for the first frame,
HBRUSH hBrush = CreateSolidBrush(m_clrBackground);
if (hBrush)
{
RECT rect = {0,0,m_PictureSize.cx,m_PictureSize.cy};
FillRect(m_hMemDC,&rect,hBrush);
DeleteObject(hBrush);
};
};
};
}
BOOL CPictureEx::PrepareDC(int nWidth, int nHeight)
{
SetWindowPos(NULL,0,0,nWidth,nHeight,SWP_NOMOVE | SWP_NOZORDER);
HDC hWinDC = ::GetDC(m_hWnd);
if (!hWinDC) return FALSE;
m_hMemDC = CreateCompatibleDC(hWinDC);
if (!m_hMemDC)
{
::ReleaseDC(m_hWnd,hWinDC);
return FALSE;
};
m_hBitmap = CreateCompatibleBitmap(hWinDC,nWidth,nHeight);
if (!m_hBitmap)
{
::ReleaseDC(m_hWnd,hWinDC);
::DeleteDC(m_hMemDC);
return FALSE;
};
m_hOldBitmap = reinterpret_cast<HBITMAP>
(SelectObject(m_hMemDC,m_hBitmap));
// fill the background
m_clrBackground = GetSysColor(COLOR_3DFACE);
RECT rect = {0,0,nWidth,nHeight};
FillRect(m_hMemDC,&rect,(HBRUSH)(COLOR_WINDOW));
::ReleaseDC(m_hWnd,hWinDC);
m_bIsInitialized = TRUE;
return TRUE;
}