IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> WTL 自绘控件库 (CQsAnimaStatic) -> 正文阅读

[C++知识库]WTL 自绘控件库 (CQsAnimaStatic)

概述:

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:

	/*
	*@method   CQsAnimaStatic
	*@brief    构造函数
	*    
	*@param    void
	*@return   
	*/
	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);	
	}

	/**
	*@method   ~CQsAnimaStatic
	*@brief    析构函数
	*    
	*@param    void
	*@return   
	*/
	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:

	/**
	*@method   SetTimerIner
	*@brief    设置时间间隔,全部显示时停顿的时间间隔毫秒表示
	*    
	*@param    UINT TimerInter 时间间隔值
	*@return   void
	*/
	void SetTimerIner(UINT TimerInter )
	{
		m_TimerInter = TimerInter;
	}

	/*****************************************************
	函数名:	SubclassWindow
	函数描述:	类对象关联
	入参:		IN HWND hWnd 对象句柄
	出参:		NA
	返回值:	成功返回TRUE,失败返回FALSE
	其他说明:	
	******************************************************/
	BOOL SubclassWindow( HWND hWnd )
	{
		BOOL bret =  theBaseClass::SubclassWindow( hWnd );
		Start(); 
		m_position =1;

		return bret;
	}

	/**
	*@method   Start
	*@brief    启动跑马灯
	*    
	*@return   void  成功返回TRUE,失败返回FALSE
	*/
	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);
	}

	/**
	*@method   SetTextColor
	*@brief    设置字体的颜色
	*    
	*@param    COLORREF txtColor 字体的颜色
	*@return   void
	*/
	void SetTextColor(COLORREF txtColor)
	{
		m_txtColor = txtColor;
	}

	/**
	*@method   OnPaint
	*@brief    
	*    
	*@param    UINT uMsg
	*@param    WPARAM wParam
	*@param    LPARAM lParam
	*@param    BOOL& bHandled
	*@return   LRESULT
	*/
	LRESULT OnPaint( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*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;
	}

	/**
	*@method   OnTimer
	*@brief    
	*    
	*@param    UINT uMsg
	*@param    WPARAM wParam
	*@param    LPARAM lParam
	*@param    BOOL& bHandled
	*@return   LRESULT
	*/
	LRESULT OnTimer( UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/ )
	{
		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;
	}

	/**
	*@method   OnDestroy
	*@brief    
	*    
	*@param    UINT uMsg
	*@param    WPARAM wParam
	*@param    LPARAM lParam
	*@param    BOOL& bHandled
	*@return   LRESULT
	*/
	LRESULT OnDestroy( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled )
	{
     //在窗口没有销毁之前销毁Timer
		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;
	}

};

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-04-28 11:37:29  更:2022-04-28 11:38:57 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 20:40:26-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码