⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 magiclabel.cpp

📁 一些关于C++开发的多媒体制作书籍的源代码
💻 CPP
字号:
/*
* Copyright (c) 2002, Bcdliang
* All rights reserved.
*
* 文件名称:MagicLabel.cpp
* 摘    要:类CMagicLabel的实现
*               Implementation file of CMagicLabel class, which is based on 
*           the CStatic class.
*
* 当前版本:1.0
* 作    者:LIANG Zheng
* 完成日期:2002年7月2日
*/

#include "stdafx.h"
#include "MagicLabel.h"
//#include "MagicLabelThread.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMagicLabel
// 使用时必须在OnInitDialog中调用CMagicLabel::Initialize()

CMagicLabel::CMagicLabel()
{
}

CMagicLabel::~CMagicLabel()
{	m_MemDC.DeleteDC();
	m_Bmp.DeleteObject();
	m_Font.DeleteObject();
}


BEGIN_MESSAGE_MAP(CMagicLabel, CStatic)
	//{{AFX_MSG_MAP(CMagicLabel)
	ON_WM_PAINT()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:DrawText
* 函数介绍:根据m_Text重画m_Bmp
* 输入参数:无
* 输出参数:无
* 返回值  :无
*/
void CMagicLabel::DrawText()
{
	CDC *pDC=GetDC();
	m_Bmp.DeleteObject();		//在再次调用CreateCompatibleBitmap前必须先删除原来的
	m_Bmp.CreateCompatibleBitmap(pDC,
		m_TextWidth,
		m_TextHeight);
	m_MemDC.SelectObject(&m_Font);
	m_MemDC.SelectObject(&m_Bmp);

	m_MemDC.SetBkColor(m_BkColor);
	m_MemDC.SetTextColor(m_TextColor);
	m_MemDC.TextOut(0, 0, m_Text);
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetBkColor
* 函数介绍:返回当前底色
* 输入参数:无
* 输出参数:无
* 返回值  :当前底色
*/
COLORREF CMagicLabel::GetBkColor() const
{
	return this->m_BkColor;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetFont
* 函数介绍:返回当前字体
* 输入参数:无
* 输出参数:无
* 返回值  :当前字体
*/
CFont *CMagicLabel::GetFont()
{
	return &this->m_Font;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetMagicStyle
* 函数介绍:返回当前Magic效果
* 输入参数:无
* 输出参数:无
* 返回值  :当前Magic效果
*/
UINT CMagicLabel::GetMagicStyle() const
{
	return this->m_uMagicStyle;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetStep
* 函数介绍:返回当前步长
* 输入参数:无
* 输出参数:无
* 返回值  :当前步长
*/
UINT CMagicLabel::GetStep() const
{
	return this->m_uStep;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetTextAlign
* 函数介绍:返回当前对齐方式
* 输入参数:无
* 输出参数:无
* 返回值  :当前对齐方式
*/
UINT CMagicLabel::GetTextAlign() const
{
	return this->m_TextAlign;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetTextColor
* 函数介绍:返回当前文字颜色
* 输入参数:无
* 输出参数:无
* 返回值  :当前文字颜色
*/
COLORREF CMagicLabel::GetTextColor() const
{
	return this->m_TextColor;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:CalculateTextSize
* 函数介绍:计算字符串的宽度、高度,存于m_TextWidth,m_TextHeight
* 输入参数:无
* 输出参数:无
* 返回值  :无
*/
void CMagicLabel::CalculateTextSize()
{
	CSize size=m_MemDC.GetOutputTextExtent(m_Text);
	m_TextWidth=size.cx;
	m_TextHeight=size.cy;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetTick
* 函数介绍:返回当前速度
* 输入参数:无
* 输出参数:无
* 返回值  :当前速度
*/
UINT CMagicLabel::GetTick() const
{
	return this->m_uTick;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetWindowText
* 函数介绍:重写父类成员GetWindowText, 返回this->m_Text
* 输入参数:无
* 输出参数:
			CString &rString, 返回this->m_Text
* 返回值  :无
*/
void CMagicLabel::GetWindowText(CString &rString) const
{
	rString = m_Text;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:Initialize
* 函数介绍:初始化对象, 必须在调用其它成员前调用
* 输入参数:无
* 输出参数:无
* 返回值  :无
*/
void CMagicLabel::Initialize()
{
	CDC *pDC=this->GetDC();
	m_uMagicStyle=ML_NONE;
	m_TextAlign=ML_TA_LEFT | ML_TA_TOP;
	CStatic::GetWindowText(m_Text);
	GetClientRect(&m_Rect);
	m_x=0; m_y=0;
	m_TextWidth=0; m_TextHeight=0;
	m_Font.CreatePointFont(9*10, "宋体");
	m_MemDC.CreateCompatibleDC(pDC);
	m_MemDC.SelectObject(&m_Font);
	m_Bmp.CreateCompatibleBitmap(&m_MemDC,//&dc, 
		m_TextWidth,
		m_TextHeight);
	m_MemDC.SelectObject(&m_Bmp);
	
	SetTextAlign(m_TextAlign);
	m_uStep=4;
	m_uTick=200;

	m_MemDC.SetBkMode(OPAQUE);
	m_TextColor=RGB(255,255,255);
	m_BkColor=RGB(0,0,0);
	DrawText();
	Invalidate();
}

void CMagicLabel::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	int index=3;

	// draw background
	CBrush brush(m_BkColor), *oldbrush;
	oldbrush=dc.SelectObject(&brush);
	dc.FillRect(&m_Rect, &brush);

	switch (m_uMagicStyle)
	{case ML_NONE:
		break;
	case ML_HSCROLLLEFT:
		if (m_x<-m_TextWidth)	// appear again
			m_x=m_Rect.Width();
		if (m_x<=0)
		{	m_BBx=0;
			m_BBxScr=-m_x;
			if (m_Rect.Width()>m_TextWidth+m_x)
				m_BBWidth=m_TextWidth+m_x;
			else
				m_BBWidth=m_Rect.Width();
		}else
		{	m_BBx=m_x;
			m_BBxScr=0;
			if (m_Rect.Width()>m_TextWidth+m_x)
				m_BBWidth=m_TextWidth;
			else
				m_BBWidth=m_Rect.Width()-m_x;
		}
		m_x-=m_uStep;
		break;
	case ML_HSCROLLRIGHT:
		if (m_x>m_Rect.Width())	// appear again
			m_x=-m_TextWidth;
		if (m_x<=0)
		{	m_BBx=0;
			m_BBxScr=-m_x;
			if (m_Rect.Width()>m_TextWidth+m_x)
				m_BBWidth=m_TextWidth+m_x;
			else
				m_BBWidth=m_Rect.Width();
		}else
		{	m_BBx=m_x;
			m_BBxScr=0;
			if (m_Rect.Width()>m_TextWidth+m_x)
				m_BBWidth=m_TextWidth;
			else
				m_BBWidth=m_Rect.Width()-m_x;
		}
		m_x+=m_uStep;
		break;
	case ML_HSCROLLBOTH:
		if (m_Rect.Width()>m_TextWidth)
		{	if (m_x<0)
			{	m_x=0;
				m_Left=false;
			}
			if (m_x>m_Rect.Width()-m_TextWidth)
			{	m_x=m_Rect.Width()-m_TextWidth;
				m_Left=true;
			}
			m_BBx=m_x;
			m_BBWidth=m_TextWidth;
			m_BBxScr=0;
		}else
		{	if (m_x<m_Rect.Width()-m_TextWidth)
			{	m_x=m_Rect.Width()-m_TextWidth;
				m_Left=false;
			}
			if (m_x>0)
			{	m_x=0;
				m_Left=true;
			}
			m_BBx=0;
			m_BBWidth=m_Rect.Width();
			m_BBxScr=-m_x;
		}
		if (m_Left)
			m_x-=m_uStep;
		else
			m_x+=m_uStep;
		break;
	case ML_HSCROLLAUTO:
		// Bug correcting, 2002.6.29
		// if the width of the text < 1/3 of the width of the drawing area,
		// then ML_HSCROLLAUTO is the same as ML_LHSCROLL.
		// ***************
		if (m_TextWidth<m_Rect.Width()/index)
		{	if (m_x<-m_TextWidth)
				m_x=m_Rect.Width();
			if (m_x<=0)
			{	m_BBx=0;
				m_BBxScr=-m_x;
				if (m_Rect.Width()>m_TextWidth+m_x)
					m_BBWidth=m_TextWidth+m_x;
				else
					m_BBWidth=m_Rect.Width();
			}else
			{	m_BBx=m_x;
				m_BBxScr=0;
				if (m_Rect.Width()>m_TextWidth+m_x)
					m_BBWidth=m_TextWidth;
				else
					m_BBWidth=m_Rect.Width()-m_x;
			}
			m_x-=m_uStep;
		}
		// ***************

		if (m_x<=0)
		{	m_BBx=0;
			m_BBxScr=-m_x;
			if (m_Rect.Width()>m_TextWidth+m_x)
				m_BBWidth=m_TextWidth+m_x;
			else
				m_BBWidth=m_Rect.Width();
		}else
		{	m_BBx=m_x;
			m_BBxScr=0;
			if (m_Rect.Width()>m_TextWidth+m_x)
				m_BBWidth=m_TextWidth;
			else
				m_BBWidth=m_Rect.Width()-m_x;
		}
		m_x-=m_uStep;
		if (m_x<0 && 
			m_TextWidth>m_Rect.Width()/index && 
			m_BBWidth<m_Rect.Width()/index)
		{	if (m_BBWidth<=0)
			{	m_x=m_Rect.Width()-m_Rect.Width()/index-m_uStep;
				m_BBx=m_Rect.Width()-m_Rect.Width()/index;
				m_BBWidth=m_Rect.Width()/index;
				m_BBxScr=0;
			}else
			{	dc.BitBlt(m_Rect.Width()-m_Rect.Width()/index+m_BBWidth, m_BBy,
					m_Rect.Width()/index-m_BBWidth, m_BBHeight,
					&m_MemDC,
					0, m_BByScr,
					SRCPAINT);
			}
		
		}
		break;
	default:
		break;
	}
	// Combine the text-drawn dc with the background dc
	dc.BitBlt(m_BBx, m_BBy,
		m_BBWidth, m_BBHeight,
		&m_MemDC,
		m_BBxScr, m_BByScr,
		SRCCOPY);

	dc.SelectObject(oldbrush);
//	Paint(&dc);

	// Do not call CStatic::OnPaint() for painting messages
}

void CMagicLabel::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	Invalidate();
	
	CStatic::OnTimer(nIDEvent);
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:SetBkColor
* 函数介绍:设定背景色
* 输入参数:
            COLORREF crColor, 新背景色
* 输出参数:无
* 返回值  :无
*/
void CMagicLabel::SetBkColor(COLORREF crColor)
{	m_BkColor=crColor;
	DrawText();
	Invalidate();
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:SetFont
* 函数介绍:设定字体
* 输入参数:
            const CFont &f, 新字体
* 输出参数:无
* 返回值  :无
*/
void CMagicLabel::SetFont(const CFont &f)
{
	ASSERT_VALID(&f);
	m_Font.DeleteObject();
	m_Font.Attach(f.GetSafeHandle());
	m_MemDC.SelectObject(&m_Font);
	SetTextAlign(m_TextAlign);
	DrawText();
	Invalidate();
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:SetMagicStyle
* 函数介绍:设置Magic效果
* 输入参数:
            UINT nStyle, 新Magic效果
* 输出参数:无
* 返回值  :无
*/
void CMagicLabel::SetMagicStyle(UINT nStyle)
{
	m_uMagicStyle=nStyle;

	switch (m_uMagicStyle)
	{case ML_NONE:
		m_x=0;
		m_y=0;
		m_BBx=0; m_BBy=0;
		m_BBWidth=m_TextWidth>m_Rect.Width()?m_Rect.Width():m_TextWidth;
		m_BBHeight=m_TextHeight>m_Rect.Height()?m_Rect.Height():m_TextHeight;
		m_BBxScr=0; m_BByScr=0;
		SetTextAlign(m_TextAlign);
		KillTimer((UINT)this);
		Invalidate();
		break;
	case ML_HSCROLLLEFT:
	case ML_HSCROLLRIGHT:
	case ML_HSCROLLBOTH:
	case ML_HSCROLLAUTO:
		m_x=0;
		m_y=0;
		m_BBy=0;
		m_BBHeight=m_TextHeight>m_Rect.Height()?m_Rect.Height():m_TextHeight;
		m_BByScr=0;
		SetTextAlign(m_TextAlign);
		SetTimer((UINT)this, m_uTick, NULL);
		break;
	}
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:SetStep
* 函数介绍:设定步长
* 输入参数:
            UINT uStep, 新步长值
* 输出参数:无
* 返回值  :无
*/
void CMagicLabel::SetStep(UINT uStep)
{
/*	if (uStep<1 || uStep>10)
		return;
*/	
	ASSERT(uStep>0 && uStep<10);
	this->m_uStep=uStep;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:SetTextAlign
* 函数介绍:设定对齐方式
* 输入参数:
            UINT nTextAlign, 新对齐方式
* 输出参数:无
* 返回值  :无
*/
void CMagicLabel::SetTextAlign(UINT nTextAlign)
{
	m_TextAlign=nTextAlign;
	CalculateTextSize();

	UINT nHAlign=m_TextAlign & 0x0f;
	UINT nVAlign=m_TextAlign & 0xf0;
	// Set Horizon align
	switch (nHAlign)
	{case ML_TA_RIGHT:
		m_x=0;
		m_BBx=m_TextWidth>m_Rect.Width()?0:(m_Rect.Width()-m_TextWidth);
		m_BBWidth=m_TextWidth>m_Rect.Width()?m_Rect.Width():m_TextWidth;
		m_BBxScr=m_TextWidth>m_Rect.Width()?(m_TextWidth-m_Rect.Width()):0;
		break;
	case ML_TA_CENTER:
		m_x=0;
		m_BBx=m_TextWidth>m_Rect.Width()?0:(m_Rect.Width()-m_TextWidth)/2;
		m_BBWidth=m_TextWidth>m_Rect.Width()?m_Rect.Width():m_TextWidth;
		m_BBxScr=m_TextWidth>m_Rect.Width()?(m_TextWidth-m_Rect.Width())/2:0;
		break;
	default:
		m_x=0;
		m_BBx=0;
		m_BBWidth=m_TextWidth>m_Rect.Width()?m_Rect.Width():m_TextWidth;
		m_BBxScr=0;
		break;
	}

	// Set Vertical align
	switch (nVAlign)
	{case ML_TA_BOTTOM:
		m_y=0;
		m_BBy=m_TextHeight>m_Rect.Height()?0:(m_Rect.Height()-m_TextHeight);
		m_BBHeight=m_TextHeight>m_Rect.Height()?m_Rect.Height():m_TextHeight;
		m_BByScr=m_TextHeight>m_Rect.Height()?(m_TextHeight-m_Rect.Height()):0;
		break;
	case ML_TA_VCENTER:
		m_y=0;
		m_BBy=m_TextHeight>m_Rect.Height()?0:(m_Rect.Height()-m_TextHeight)/2;
		m_BBHeight=m_TextHeight>m_Rect.Height()?m_Rect.Height():m_TextHeight;
		m_BByScr=m_TextHeight>m_Rect.Height()?(m_TextHeight-m_Rect.Height())/2:0;
		break;
	default:
		m_y=0;
		m_BBy=0;
		m_BBHeight=m_TextHeight>m_Rect.Height()?m_Rect.Height():m_TextHeight;
		m_BByScr=0;
		break;
	}
//	Invalidate();
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:SetTextColor
* 函数介绍:设定文字颜色
* 输入参数:
            COLORREF crColor, 新文字颜色
* 输出参数:无
* 返回值  :无
*/
void CMagicLabel::SetTextColor(COLORREF crColor)
{	m_TextColor=crColor;
	DrawText();
	Invalidate();
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:SetTick
* 函数介绍:设定速度
* 输入参数:
            UINT uTick, 新速度
* 输出参数:无
* 返回值  :无
*/
void CMagicLabel::SetTick(UINT uTick)
{
	if (uTick<50 || uTick>2000)
		return;
	this->m_uTick=uTick;
	if (this->m_uMagicStyle!=ML_NONE)
	{	KillTimer((UINT)this);
		SetTimer((UINT)this, m_uTick, NULL);
	}
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:SetWindowText
* 函数介绍:重写父类成员SetWindowText
* 输入参数:
            LPCTSTR lpszString, 新字符串
* 输出参数:无
* 返回值  :无
*/
void CMagicLabel::SetWindowText(LPCTSTR lpszString)
{
	m_Text.Format("%s", lpszString);
	m_x=m_y=0;
	SetTextAlign(m_TextAlign);
	DrawText();
	Invalidate();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -