www.pudn.com > 一个可自动演示的图片控件.rar > StaticBmp.cpp
// StaticBmp.cpp : implementation file
//
#include "stdafx.h"
#include "CStaticBmp.h"
#include "StaticBmp.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStaticBmp
CStaticBmp::CStaticBmp()
{
m_DelayTime=500;
m_bAutoDisplay=FALSE;
m_bDrawForm=TRUE;
m_FormColor=RGB(250,0,0);
m_bStretch=FALSE;
m_bCyc=TRUE;
m_BmpIDArray.RemoveAll();
m_NowBmpNo=-1;
}
CStaticBmp::~CStaticBmp()
{
}
BEGIN_MESSAGE_MAP(CStaticBmp, CStatic)
//{{AFX_MSG_MAP(CStaticBmp)
ON_WM_PAINT()
ON_WM_DESTROY()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStaticBmp message handlers
void CStaticBmp::SetDrawForm(BOOL bDraw, BOOL bErase)
{
this->m_bDrawForm=bDraw;
if ( bErase )
{
this->Invalidate(FORM);
}
}
BOOL CStaticBmp::IsDrawForm()
{
return this->m_bDrawForm;
}
void CStaticBmp::SetFormColor(const COLORREF &color, BOOL bErase)
{
this->m_FormColor=color;
if ( bErase )
{
CDC *pDC=this->GetDC();
this->DrawForm(pDC);
this->ReleaseDC(pDC);
}
}
COLORREF CStaticBmp::GetFormColor()
{
return this->m_FormColor;
}
void CStaticBmp::SetBitmap(UINT IDBmp, BOOL bErase)
{
this->m_BmpIDArray.RemoveAll();
this->m_BmpIDArray.Add(IDBmp);
this->m_NowBmpNo=1;
if ( bErase )
this->Invalidate(BMP);
}
void CStaticBmp::SetBitmap(const CUIntArray &IDBmpArray, BOOL bErase)
{
this->m_BmpIDArray.RemoveAll();
this->m_BmpIDArray.Copy(IDBmpArray);
this->m_NowBmpNo=1;
if ( bErase )
this->Invalidate(BMP);
}
BOOL CStaticBmp::SetNowBmp(int BmpNo, BOOL bErase)
{
if ( BmpNo<=0 || BmpNo>m_BmpIDArray.GetSize() )
return FALSE;
::SendMessage(this->GetParent()->GetSafeHwnd(), WM_STATICBMP_PRECHANGE, BmpNo, this->m_BmpIDArray.GetSize());
this->m_NowBmpNo=BmpNo;
if ( bErase )
this->Invalidate(BMP);
return TRUE;
}
int CStaticBmp::GetNowBmp()
{
if ( this->m_BmpIDArray.GetSize() == 0 )
return 0;
else
return m_NowBmpNo;
}
int CStaticBmp::GetBmpAmount()
{
return this->m_BmpIDArray.GetSize();
}
void CStaticBmp::SetAutoDisplay(BOOL bAuto)
{
this->m_bAutoDisplay=bAuto;
if ( this->m_bAutoDisplay )
this->SetTimer(1, this->m_DelayTime, NULL);
}
BOOL CStaticBmp::GetAutoDisplay()
{
return this->m_bAutoDisplay;
}
void CStaticBmp::SetDelayTime(int millisecond)
{
if ( millisecond <= 0 )
return ;
else
{
this->m_DelayTime=millisecond;
this->SetTimer(1, m_DelayTime, NULL);
}
}
int CStaticBmp::GetDelayTime()
{
return m_DelayTime;
}
void CStaticBmp::SetStretchMode(BOOL Stretch, BOOL bErase)
{
this->m_bStretch=Stretch;
if ( bErase )
this->Invalidate(BMP);
}
BOOL CStaticBmp::GetStretchMode()
{
return this->m_bStretch;
}
void CStaticBmp::SetCyc(BOOL bCyc)
{
this->m_bCyc=bCyc;
if ( m_bAutoDisplay && m_bCyc )
this->SetTimer(1, m_DelayTime, NULL);
}
BOOL CStaticBmp::IsCyc()
{
return this->m_bCyc;
}
void CStaticBmp::OnPaint()
{
TRACE( "OnPaint\n");
CPaintDC dc(this);
CRect rt;
int formWidth=1;
if ( m_BmpIDArray.GetSize() > 0 )
{
CBitmap tmpBmp;
tmpBmp.LoadBitmap(this->m_BmpIDArray[this->m_NowBmpNo-1]);
BITMAP szbitmap;
tmpBmp.GetObject(sizeof(BITMAP),&szbitmap);
CDC memdc;
memdc.CreateCompatibleDC(&dc);
memdc.SelectObject(&tmpBmp);
this->GetClientRect(&rt);
rt.DeflateRect(formWidth,formWidth,formWidth,formWidth);
if ( this->m_bStretch )
dc.StretchBlt(formWidth, formWidth, rt.Width(), rt.Height(), &memdc, 0, 0, szbitmap.bmWidth, szbitmap.bmHeight, SRCCOPY);
else
dc.BitBlt(formWidth, formWidth, rt.Width(), rt.Height(), &memdc, 0, 0, SRCCOPY);
}
this->DrawForm(&dc);
}
void CStaticBmp::DrawForm(CDC *pDC)
{
if ( !this->m_bDrawForm )
return ;
CRect rt;
this->GetWindowRect(&rt);
rt.OffsetRect(-rt.left, -rt.top);
pDC->Draw3dRect(&rt, this->m_FormColor, this->m_FormColor);
TRACE( "DrawForm ed\n");
}
void CStaticBmp::OnTimer(UINT nIDEvent)
{
CStatic::OnTimer(nIDEvent);
if ( !this->m_bAutoDisplay )
return ;
int Amount=this->m_BmpIDArray.GetSize();
if ( this->m_NowBmpNo < Amount )
{
::SendMessage(this->GetParent()->GetSafeHwnd(), WM_STATICBMP_PRECHANGE, m_NowBmpNo+1, Amount);
this->m_NowBmpNo++;
}
else if ( this->m_bCyc && Amount>1)
{
::SendMessage(this->GetParent()->GetSafeHwnd(), WM_STATICBMP_PRECHANGE, 1, Amount);
this->m_NowBmpNo=1;
}
else
return ;
this->Invalidate(BMP);
}
void CStaticBmp::Invalidate(InvalidateType type)
{
CRgn rg;
CRect rt;
this->GetWindowRect(&rt);
CWnd *pParent=this->GetParent();
pParent->ScreenToClient(&rt);
if ( type == ALL )
{
rg.CreateRectRgnIndirect(&rt);
}
else if ( type == BMP )
{
rt.DeflateRect(1, 1, 1, 1);
rg.CreateRectRgnIndirect(&rt);
}
else
{
CRgn rg2;
rg.CreateRectRgnIndirect(&rt);
rt.DeflateRect(1, 1, 1, 1);
rg2.CreateRectRgnIndirect(&rt);
rg.CombineRgn(&rg,&rg2,RGN_XOR);
}
pParent->InvalidateRgn(&rg);
}
void CStaticBmp::OnDestroy()
{
this->KillTimer(1);
CStatic::OnDestroy();
}
void CStaticBmp::PreSubclassWindow()
{
CStatic::PreSubclassWindow();
this->SetTimer(1, this->m_DelayTime, NULL);
}