概述:
CQsAnimaStatic继承与 CStatic,主要是实现跑马灯的效果的。
代码实现如下:
#pragma once
#include "QsInclude.h"
#define TIMERID 823
#define TIMERPAUSE 824
class CQsAnimaStatic:
public CWindowImpl<CQsAnimaStatic, CStatic>,
public CImageMgrCtrlBase< CQsAnimaStatic>
{
public:
typedef CWindowImpl< CQsAnimaStatic, CStatic > theBaseClass;
typedef CImageMgrCtrlBase< CQsAnimaStatic> theImageCtrlBaseClass;
private:
HBITMAP m_hBmp;
SIZE m_size;
HDC m_hMemDC;
int m_position;
BOOL bAnimaStatic;
UINT m_bTimer;
UINT m_bTimerPause;
UINT m_TimerInter;
COLORREF m_txtColor;
public:
CQsAnimaStatic(void)
{
m_hMemDC = NULL;
m_hBmp = NULL;
m_position = 0;
bAnimaStatic = FALSE;
m_bTimer = 0;
m_bTimerPause = 0;
m_TimerInter = 1000;
m_txtColor = RGB(0,0,0);
}
virtual ~CQsAnimaStatic(void)
{
}
BEGIN_MSG_MAP( CQsAnimaStatic )
MESSAGE_HANDLER( WM_PAINT, OnPaint )
MESSAGE_HANDLER( WM_TIMER, OnTimer )
MESSAGE_HANDLER( WM_DESTROY, OnDestroy )
CHAIN_MSG_MAP( theImageCtrlBaseClass )
DEFAULT_REFLECTION_HANDLER()
END_MSG_MAP()
public:
void SetTimerIner(UINT TimerInter )
{
m_TimerInter = TimerInter;
}
BOOL SubclassWindow( HWND hWnd )
{
BOOL bret = theBaseClass::SubclassWindow( hWnd );
Start();
m_position =1;
return bret;
}
void Start()
{
bAnimaStatic = TRUE;
CRect rc;
GetClientRect(rc);
HDC hDC = ::GetDC(m_hWnd);
int nLength = GetWindowTextLength();
TCHAR *cTitle = new TCHAR[nLength+1];
GetWindowText(cTitle,nLength+1);
CString txt(cTitle);
m_hMemDC = ::CreateCompatibleDC(hDC);
::GetTextExtentPoint(m_hMemDC,txt,txt.GetLength(),&m_size);
m_hBmp = ::CreateCompatibleBitmap(hDC,m_size.cx,m_size.cy);
::SelectObject(m_hMemDC,m_hBmp);
::SetBkMode(m_hMemDC,TRANSPARENT);
delete []cTitle;
ReleaseDC(hDC);
m_bTimer = (UINT)SetTimer(TIMERID,300,0);
}
void SetTextColor(COLORREF txtColor)
{
m_txtColor = txtColor;
}
LRESULT OnPaint( UINT , WPARAM , LPARAM , BOOL& bHandled )
{
if(bAnimaStatic==FALSE)
{
bHandled = FALSE;
return TRUE;
}
CPaintDC dc(m_hWnd);
dc.SetBkMode( TRANSPARENT);
CRect rc;
GetClientRect(rc);
::SendMessage( GetParent(), WM_DRAWBKGNDUI, ( WPARAM )m_hMemDC, ( LPARAM )m_hWnd );
int nLength = GetWindowTextLength();
TCHAR *cTitle = new TCHAR[nLength+1];
GetWindowText(cTitle,nLength+1);
CString txt(cTitle);
HFONT hFont = GetStateFont( CONTROL_STA_NORMAL );
HGDIOBJ hOldFont =::SelectObject(m_hMemDC,hFont);
::SetTextColor(m_hMemDC,m_txtColor);
::TextOut(m_hMemDC,0,0,txt,txt.GetLength());
::SelectObject(m_hMemDC,hOldFont);
::DeleteObject(hFont);
delete []cTitle;
if(m_hMemDC)
{
int width = m_size.cx;
if(m_position+width>rc.Width())
{
width = rc.Width()-m_position;
}
::BitBlt(dc.m_hDC,m_position,0,width,rc.Height(),m_hMemDC,0,0,SRCCOPY);
int idle = m_position;
if(idle>0)
{
::BitBlt(dc.m_hDC,0,0,idle,rc.Height(),m_hMemDC,rc.Width()-idle,0,SRCCOPY);
}
else
{
KillTimer( m_bTimer );
m_bTimerPause = (UINT)SetTimer(TIMERPAUSE,m_TimerInter,0);
}
}
return TRUE;
}
LRESULT OnTimer( UINT , WPARAM wParam, LPARAM , BOOL& )
{
if(wParam==TIMERID)
{
m_position+=5;
CRect rc;
GetClientRect(rc);
if(m_position>=rc.Width())
{
m_position = 0;
}
Invalidate();
}
if(m_bTimerPause ==TIMERPAUSE)
{
m_bTimer = (UINT)SetTimer(TIMERID,300,0);
BOOL bFlag = KillTimer( m_bTimerPause );
m_bTimerPause =0;
}
return TRUE;
}
LRESULT OnDestroy( UINT , WPARAM , LPARAM , BOOL& bHandled )
{
KillTimer( m_bTimer );
KillTimer( m_bTimerPause );
if( NULL != m_hBmp)
{
::DeleteObject(m_hBmp);
m_hBmp = NULL;
}
if(NULL != m_hMemDC)
{
::DeleteDC(m_hMemDC);
m_hMemDC = NULL;
}
bHandled = FALSE;
return TRUE;
}
};
|