📄 demodlg.cpp
字号:
// demoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "demo.h"
#include "demoDlg.h"
#include "resourcesp.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CdemoDlg dialog
CdemoDlg::CdemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CdemoDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pAnimation=NULL;
m_CurrentLoop=0;
m_CurrentImage=0;
m_bAnimationPlaying=false;
}
void CdemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CdemoDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
ON_WM_SIZE()
#endif
//}}AFX_MSG_MAP
ON_WM_PAINT()
END_MESSAGE_MAP()
BOOL CdemoDlg::OnInitDialog()
{
m_bFullScreen = FALSE;
CDialog::OnInitDialog();
// CRect rc;
// ::GetClientRect(GetDesktopWindow()->GetSafeHwnd(),&rc);
// ClientToScreen(&rc);
// rc.bottom-=24;
// MoveWindow(&rc);
LoadAnimatedGif(_T("Storage\\ScreenSave.gif"));
return TRUE; // return TRUE unless you set the focus to a control
}
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CdemoDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
if (AfxIsDRAEnabled())
{
DRA::RelayoutDialog(
AfxGetResourceHandle(),
this->m_hWnd,
DRA::GetDisplayMode() != DRA::Portrait ?
MAKEINTRESOURCE(IDD_DEMO_DIALOG_WIDE) :
MAKEINTRESOURCE(IDD_DEMO_DIALOG));
}
}
#endif
void CdemoDlg::CreateDlg()
{
CDialog::Create(IDD);
}
void CdemoDlg::DestroyDlg()
{
Stop();
DestroyWindow();
}
void CdemoDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CRect rc;
GetClientRect(&rc);
C_Image* & current = m_pAnimation->img[m_CurrentImage];
if(m_CurrentImage == 0)
{
current->GDIPaint(dc.GetSafeHdc(),current->xPos,current->yPos,current->Width,current->Height);
}
else
{
CDC SrcDC;
SrcDC.CreateCompatibleDC(&dc);
void *ppvbits;
HBITMAP hBmp=CreateDIBSection(dc.GetSafeHdc(),current->pbmi,DIB_RGB_COLORS,(void**)&ppvbits,NULL,NULL);
if(hBmp!=NULL)
{
HBITMAP hOld = (HBITMAP)SrcDC.SelectObject(hBmp);
SetDIBitsToDevice (SrcDC.GetSafeHdc(),0,0,
current->Width,current->Height,0,0,0,current->Height,
(LPVOID)current->Raster,current->pbmi,0);
//定义源、掩码、透明绘制环境
CDC MaskDC,TransDC;
//定义掩码、透明位图
HBITMAP hMaskBmp,hTransBmp;
// BITMAP bm;
//创建源、掩码、透明绘制环境
MaskDC.CreateCompatibleDC(&dc);
TransDC.CreateCompatibleDC(&dc);
//创建透明位图
hTransBmp = CreateCompatibleBitmap(dc.GetSafeHdc(),current->Width,current->Height);
//创建掩码位图
hMaskBmp=CreateBitmap(current->Width,current->Height,1,1,NULL);
//将透明位图对象选入到透明绘图环境
HBITMAP hOldTransBmp = (HBITMAP)TransDC.SelectObject(hTransBmp);
//将目标位图绘制到透明位图中
TransDC.BitBlt(0,0,current->Width,current->Height,&dc,
current->xPos,
current->yPos,SRCCOPY);
//设置背景为透明模式
SrcDC.SetBkMode(TRANSPARENT);
//设置背景色
SrcDC.SetBkColor(0);
HBITMAP hOldMaskBmp = (HBITMAP)MaskDC.SelectObject(hMaskBmp);
//将源绘图环境绘制到掩码绘图环境中
MaskDC.BitBlt(0,0,current->Width,current->Height,&SrcDC,0,0,SRCCOPY);
//将源位图和透明位图进行异或操作融合
TransDC.BitBlt(0,0,current->Width,current->Height,&SrcDC,0,0,SRCINVERT);
//将透明位图和掩码位图进行与操作
TransDC.BitBlt(0,0,current->Width,current->Height,&MaskDC,0,0,SRCAND);
//再将源位图和透明位图进行异或操作
TransDC.BitBlt(0,0,current->Width,current->Height,&SrcDC,0,0,SRCINVERT);
//此时将透明位图绘制到目标设备上
dc.BitBlt(current->xPos,
current->yPos,
current->Width,current->Height,&TransDC,
0,
0,SRCCOPY);
//绘制绘图对象,并释放相应绘图环境和位图对象
SrcDC.SelectObject(hOld);
SrcDC.DeleteDC();
DeleteObject(hBmp);
TransDC.SelectObject(hOldTransBmp);
DeleteObject(hTransBmp);
TransDC.DeleteDC();
MaskDC.SelectObject(hOldMaskBmp);
MaskDC.DeleteDC();
DeleteObject(hMaskBmp);
}
}
// Do not call CDialog::OnPaint() for painting messages
}
int CdemoDlg::LoadAnimatedGif(LPTSTR FileName)
{
Stop();
m_AnimGif.Reset();
m_pAnimation = NULL;
#ifdef _UNICODE
char szTemp[MAX_PATH] = {0};
WideCharToMultiByte(CP_ACP,0,FileName,_tcslen(FileName),szTemp,_tcslen(FileName),NULL,false);
int Result=m_AnimGif.LoadGIF(szTemp);
#else
int Result=m_AnimGif.LoadGIF(FileName);
#endif
m_pAnimation=&m_AnimGif;
Play();
return Result;
}
// fnThread: Thread function in charge of looping animation frames.
DWORD WINAPI CdemoDlg::fnThread (LPVOID lpParameter)
{
do
{
Sleep(10);
CdemoDlg* window;
C_ImageSet* anim;
window=(CdemoDlg*) lpParameter;
if(window->m_pAnimation==NULL)
{
continue;
}
anim=window->m_pAnimation;
window->m_bAnimationPlaying=TRUE;
while ( anim->nLoops ? window->m_CurrentLoop < anim->nLoops : true )
{
while (1)
{
//window->DrawGif();
::InvalidateRect(window->m_hWnd,NULL,FALSE);
C_Image* & image = anim->img[window->m_CurrentImage];
int iDelay = image->Delay?image->Delay:100;
if (window->m_CurrentImage < anim->nImages-1)
++window->m_CurrentImage;
else
{
window->Rewind();
// window->m_CurrentImage=0;
// break;
}
Sleep (iDelay);
// CurrentImage must always be valid!
}
++window->m_CurrentLoop;
}
window->Rewind();
window->m_bAnimationPlaying=FALSE;
}while(1);
return 0;
}
// Play: Start/Resume animation loop
void CdemoDlg::Play ()
{
if (!m_bAnimationPlaying)
if (m_pAnimation->nImages > 1)
{
m_hThreadAnim = CreateThread(NULL,0,fnThread,this,0,&m_dwThreadIdAnim);
}
}
// Stop: Stop animation loop
void CdemoDlg::Stop ()
{
if (m_bAnimationPlaying)
{
TerminateThread(m_hThreadAnim,0);
Rewind();
m_bAnimationPlaying=FALSE;
}
}
// Rewind: Reset animation loop to its initial values
void CdemoDlg::Rewind ()
{
m_CurrentLoop=0;
m_CurrentImage=0;
}
BOOL CdemoDlg::IsPlaying()
{
return m_bAnimationPlaying;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -