📄 textmove.cpp
字号:
// TextMove.cpp : implementation file
//
#include "stdafx.h"
#include "TEXTMOVETEST.h"
#include "TextMove.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//----------------------------------------------------------------------
//Macro define
#define DEFAULT_BKGND_COLOR RGB(0,0,255)
//For the text move
#define DEFAULT_INTERVAL 50 //500ms
#define DEFAULT_MOVE_PIXEL 2 //pixel
//The text information
#define DEFAULT_TEXT_COLOR RGB(255,255,255)
#define DEFAULT_TEXT_POINTSIZE 20
#define DEFAULT_TEXT_WEIGHT 10
//----------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
// CTextMove
CTextMove::CTextMove():
m_iWndWidth(0),
m_iWndHeight(0),
m_iTxtInfoX(0),
m_iTxtInfoY(0),
m_iTxtInfoWidth(0),
m_iTxtInfoHeight(0),
m_dwInterval(DEFAULT_INTERVAL),
m_hEventTimer(CreateEvent(NULL,FALSE,FALSE,NULL)),
m_iMovePixel(DEFAULT_MOVE_PIXEL),
m_iTxtInfoPointSize(DEFAULT_TEXT_POINTSIZE),
m_crTxtInfoColor(DEFAULT_TEXT_COLOR),
m_iTxtInfoWeight(DEFAULT_TEXT_WEIGHT),
m_crBkColor(DEFAULT_BKGND_COLOR)
{
//Create the timer thread to move the text
HANDLE hdThrd = CreateThread(NULL,NULL,TimerThread,this,NULL,NULL);
CloseHandle(hdThrd);
}
CTextMove::~CTextMove()
{
if(m_hEventTimer != NULL)
{
CloseHandle(m_hEventTimer);
m_hEventTimer = NULL;
}
if(m_DCTxtInfo.IsOK() == TRUE)
{
m_DCTxtInfo.Delete();
}
}
BEGIN_MESSAGE_MAP(CTextMove, CWnd)
//{{AFX_MSG_MAP(CTextMove)
ON_WM_PAINT()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTextMove message handlers
void CTextMove::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
HDC hdc = ::GetDC(this->m_hWnd);
//Create the memory DC
CMemDC memDC;
SIZE size = {m_iWndWidth,m_iWndHeight};
memDC.Create(hdc,&size);
DrawBackground(memDC.GetDC()); //设置背景色
//Draw the text
if(m_DCTxtInfo.IsOK() == TRUE)
{
TransparentBlt(memDC.GetDC(),
m_iTxtInfoX,
m_iTxtInfoY,
m_iTxtInfoWidth,
m_iTxtInfoHeight,
m_DCTxtInfo.GetDC(),
0,
0,
m_DCTxtInfo.GetWidth(),
m_DCTxtInfo.GetHeight(),
m_crBkColor);
}
//Draw the device context
BitBlt(hdc,0,0,m_iWndWidth,m_iWndHeight,memDC.GetDC(),0,0,SRCCOPY);
//Delete the memory DC
memDC.Delete();
::ReleaseDC(this->m_hWnd,hdc);
// Do not call CWnd::OnPaint() for painting messages
}
void CTextMove::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CWnd::OnTimer(nIDEvent);
}
//设定显示文字
BOOL CTextMove::SetText(const TCHAR *pcszText)
{
if(m_pszTxtInfo != NULL)
{
delete [] m_pszTxtInfo;
m_pszTxtInfo = NULL;
}
m_pszTxtInfo = new TCHAR[_tcslen(pcszText) + 1];
_tcscpy(m_pszTxtInfo,pcszText);
//Set the flag to reinitialize the memory DC
//m_bInited = FALSE;
return TRUE;
}
BOOL CTextMove::InitDCTxtInfo()
{
HDC hdc = ::GetDC(this->m_hWnd);
GetClientRect( &m_rectStaticClient ); //得到滚动文字显示控件大小
m_iWndWidth=m_rectStaticClient.Width();
m_iWndHeight=m_rectStaticClient.Height();
//得到文本的宽度和高度
ResetTxtInfoRect();
//If existing the memory DC, delete it.
m_DCTxtInfo.Delete();
//Create the memory DC
SIZE size = {m_iTxtInfoWidth,m_iTxtInfoHeight};
m_DCTxtInfo.Create(hdc,&size);
//Draw the background with the transparent color
HPEN hPen = CreatePen(PS_SOLID,1,m_crBkColor); //创建一个背景色的画笔
HPEN hOldPen = NULL;
hOldPen = (HPEN)SelectObject(m_DCTxtInfo.GetDC(),hPen);//选中画笔
//the rect color
HBRUSH hBrush = CreateSolidBrush(m_crBkColor); //创建一个背景色的画刷
HGDIOBJ hOldBrush = SelectObject(m_DCTxtInfo.GetDC(),hBrush);//选中画刷
//Draw
Rectangle(m_DCTxtInfo.GetDC(),0,0,m_iTxtInfoWidth + 1, m_iTxtInfoHeight + 1); //用画笔画轮廓,用画刷填充
//Realse the resource
SelectObject(m_DCTxtInfo.GetDC(),hOldBrush);
DeleteObject(hBrush);
SelectObject(m_DCTxtInfo.GetDC(),hOldPen);
DeleteObject(hPen);
//Set the background mode
::SetBkMode(m_DCTxtInfo.GetDC(),TRANSPARENT);//设置背景为透明模式
//Draw the text
UINT uFormat = 0;
uFormat = DT_VCENTER | DT_SINGLELINE | DT_LEFT;
//Set the text color
COLORREF crOldTextColor =::SetTextColor(m_DCTxtInfo.GetDC(),m_crTxtInfoColor);
//Set the font to caculate
LOGFONT lf = {0};
HFONT hFontNew, hFontOld;
lf.lfQuality = CLEARTYPE_QUALITY;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfHeight = -1 * (m_iTxtInfoPointSize * GetDeviceCaps(m_DCTxtInfo.GetDC(),LOGPIXELSY) / 72);
lf.lfWeight = m_iTxtInfoWeight;
//Create the new font
hFontNew = CreateFontIndirect(&lf);
hFontOld = (HFONT) SelectObject(m_DCTxtInfo.GetDC(), hFontNew);
//将文本画到定的内存去
RECT rcDraw = {0, 0, m_iTxtInfoWidth, m_iTxtInfoHeight};
DrawText(m_DCTxtInfo.GetDC(),m_pszTxtInfo,-1,&rcDraw,uFormat);
//Restore the font
SelectObject(m_DCTxtInfo.GetDC(), hFontOld);
DeleteObject(hFontNew);
//Restore the color
::SetTextColor(m_DCTxtInfo.GetDC(),crOldTextColor);
::ReleaseDC(this->m_hWnd,hdc);
return true;
}
BOOL CTextMove::Play()
{
if(m_DCTxtInfo.IsOK() == FALSE )
{
//Create the memory DC for text information
InitDCTxtInfo();
}
m_taCurAction = TA_MOVE;
SetEvent(m_hEventTimer);
return true;
}
//----------------------------------------------------------------------
//Description:
// Pause the text
//----------------------------------------------------------------------
BOOL CTextMove::Pause(void)
{
m_taCurAction = TA_NULL;
SetEvent(m_hEventTimer);
return TRUE;
}
//---------------------------------------------------------------------
//Description:
// Stop playing
//
//---------------------------------------------------------------------
BOOL CTextMove::Stop(void)
{
m_taCurAction = TA_STOP;
SetEvent(m_hEventTimer);
return TRUE;
}
void CTextMove::DrawBackground(HDC hdc)
{
//Create the pen
HPEN hPen = CreatePen(PS_SOLID,1,m_crBkColor);
HPEN hOldPen = NULL;
hOldPen = (HPEN)SelectObject(hdc,hPen);
//the rect color
HBRUSH hBrush = CreateSolidBrush(m_crBkColor);
HGDIOBJ hOldBrush = SelectObject(hdc,hBrush);
//Draw
Rectangle(hdc,0,0,m_iWndWidth + 1, m_iWndHeight + 1);
//Realse the resource
SelectObject(hdc,hOldBrush);
DeleteObject(hBrush);
SelectObject(hdc,hOldPen);
DeleteObject(hPen);
}
BOOL CTextMove::ResetTxtInfoRect()
{
if(m_pszTxtInfo == NULL)
{
return FALSE;
}
HDC hdc = ::GetDC(this->m_hWnd);
//Set the font to caculate
LOGFONT lf = {0};
HFONT hFontNew, hFontOld;
lf.lfQuality = CLEARTYPE_QUALITY;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfHeight = -1 * (m_iTxtInfoPointSize * GetDeviceCaps(hdc,LOGPIXELSY) / 72);
lf.lfWeight = m_iTxtInfoWeight;
//Create the new font
hFontNew = CreateFontIndirect(&lf);
hFontOld = (HFONT) SelectObject(hdc, hFontNew);
//Get the size of the string
SIZE size = {0};
GetTextExtentPoint(hdc,m_pszTxtInfo,_tcslen(m_pszTxtInfo),&size);
//Restore the font
SelectObject(hdc, hFontOld);
DeleteObject(hFontNew);
::ReleaseDC(this->m_hWnd,hdc);
m_iTxtInfoWidth = size.cx; //得到文本的实际宽度
m_iTxtInfoHeight = size.cy; //得到文本的实际高度
//The begin position of X and Y
m_iTxtInfoX = m_iWndWidth;
m_iTxtInfoY = (m_iWndHeight - m_iTxtInfoHeight) / 2;
return TRUE;
}
//----------------------------------------------------------------------
//Description:
// Set the move distance once, base on pixel.
//
//----------------------------------------------------------------------
void CTextMove::SetMovePixel(int iPixel)
{
m_iMovePixel = iPixel;
}
//----------------------------------------------------------------------
//Description:
// Set the text color. Don't set the same color with the background.
// It would take effect in next calling Play().
//
//----------------------------------------------------------------------
void CTextMove::SetTxtColor(COLORREF crColor)
{
m_crTxtInfoColor = crColor;
//Set the flag to reinitialize the memory DC
//m_bInited = FALSE;
}
//----------------------------------------------------------------------
//Description:
// Set the text point size.
// It would take effect in next calling Play().
//
//----------------------------------------------------------------------
void CTextMove::SetTxtPointSize(int iPointSize)
{
m_iTxtInfoPointSize = iPointSize;
//Set the flag to reinitialize the memory DC
//m_bInited = FALSE;
}
//----------------------------------------------------------------------
//Description:
// Set the text weight. See LOGFONT for a list of value.
// It would take effect in next calling Play().
//
//----------------------------------------------------------------------
void CTextMove::SetTxtWeight(int iWeight)
{
m_iTxtInfoWeight = iWeight;
//Set the flag to reinitialize the memory DC
//m_bInited = FALSE;
}
//----------------------------------------------------------------------
//Description:
// The timer thread
//
//----------------------------------------------------------------------
DWORD CTextMove::TimerThread(PVOID pArg)
{
CTextMove *pObject = (CTextMove *)pArg;
while(TRUE)
{
if(WaitForSingleObject(pObject->m_hEventTimer,pObject->m_dwInterval) == WAIT_TIMEOUT)
{
if(pObject->m_taCurAction == TA_MOVE)
{
pObject->m_iTxtInfoX -= pObject->m_iMovePixel;
if(pObject->m_iTxtInfoX + pObject->m_iTxtInfoWidth <= 0)
{
pObject->m_iTxtInfoX = pObject->m_iWndWidth;
}
//Redraw the text information
// printf("m_iTxtInfoX is %d m_iTxtInfoY is %d \n",pObject->m_iTxtInfoX,pObject->m_iTxtInfoY);
::InvalidateRect(pObject->m_hWnd,NULL,FALSE);
}
else if(pObject->m_taCurAction == TA_EXIT)
{
break;
}
else if(pObject->m_taCurAction == TA_STOP)
{
pObject->m_iTxtInfoX = pObject->m_iWndWidth;
pObject->m_taCurAction = TA_NULL;
::InvalidateRect(pObject->m_hWnd,NULL,FALSE);
}
}
}
return 0;
}
//----------------------------------------------------------------------
//Description:
// Set the interval for text moving
//
//----------------------------------------------------------------------
void CTextMove::SetInterval(DWORD dwInterval)
{
m_dwInterval = dwInterval;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -