📄 text.cpp
字号:
/**///////////////////////////////////////////////////////////////////////
// Text.cpp: implementation of the CText class.
//
/**///////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Text.h"
//--------------------------------------------------------------------
//Macro define
#define DEFAULT_BKMODE TRANSPARENT
#define DEFAULT_TEXT_COLOR RGB(0,0,0)
#define DEFAULT_BK_COLOR RGB(255,255,255)
#define DEFAULT_FORMAT (DT_LEFT | DT_SINGLELINE)
#define DEFAULT_TEXT_HEIGHT 0
//--------------------------------------------------------------------
/**///////////////////////////////////////////////////////////////////////
// Construction/Destruction
/**///////////////////////////////////////////////////////////////////////
CText::CText()
{
memset(&m_rcWndPos,0,sizeof(m_rcWndPos));
m_pszText = NULL;
m_ulSizeText = 0;
m_iBkMode = DEFAULT_BKMODE;
m_crTextColor = DEFAULT_TEXT_COLOR;
m_crBkColor = DEFAULT_BK_COLOR;
m_uFormat = DEFAULT_FORMAT;
m_iTextHeight = DEFAULT_TEXT_HEIGHT;
}
CText::~CText()
{
if(m_pszText != NULL)
{
delete [] m_pszText;
m_pszText = NULL;
}
}
//--------------------------------------------------------------------
//Description:
// Set the control position
//
//--------------------------------------------------------------------
void CText::SetPosition(const RECT *prc)
{
m_rcWndPos = *prc;
}
//--------------------------------------------------------------------
//Description:
// Set the text. If you want to display the text ,you should call the Display()
//
//--------------------------------------------------------------------
BOOL CText::SetText(const TCHAR *pszText)
{
ULONG ulLen = _tcslen(pszText);
if(m_pszText == NULL)
{
m_pszText = new TCHAR [ulLen + 1];
if(m_pszText == NULL)
{
return FALSE;
}
m_ulSizeText = ulLen + 1;
}
else if(ulLen + 1 > m_ulSizeText)
{
delete [] m_pszText;
m_pszText = new TCHAR [ulLen + 1];
if(m_pszText == NULL)
{
return FALSE;
}
m_ulSizeText = ulLen + 1;
}
_tcscpy(m_pszText,pszText);
return TRUE;
}
//--------------------------------------------------------------------
//Description:
// Display the text stored.
//
//--------------------------------------------------------------------
void CText::Draw(HDC hdc)
{
COLORREF crOldTextColor = ::SetTextColor(hdc,m_crTextColor);
COLORREF crOldBkColor = ::SetBkColor(hdc,m_crBkColor);
int iOldMode = ::SetBkMode(hdc,m_iBkMode);
LOGFONT lf = {0};
HFONT hFontNew, hFontOld;
lf.lfQuality = CLEARTYPE_QUALITY;
lf.lfHeight = -1 * (m_iTextHeight * GetDeviceCaps(hdc,LOGPIXELSY) / 72); //pound
hFontNew = CreateFontIndirect(&lf);
hFontOld = (HFONT) SelectObject(hdc, hFontNew);
DrawText(hdc,m_pszText,-1,&m_rcWndPos,m_uFormat);
SelectObject(hdc, hFontOld);
DeleteObject(hFontNew);
::SetTextColor(hdc,crOldTextColor);
::SetBkColor(hdc,crOldBkColor);
::SetBkMode(hdc,iOldMode);
}
//--------------------------------------------------------------------
//Description:
// Set the background mode.
//
//Parameters:
// iMode: [in] The value is just like as follow:
// OPAQUE -- Background is filled with the current background color before the text,
// hatched brush, or pen is drawn.
// TRANSPARENT -- Background remains untouched.
//--------------------------------------------------------------------
BOOL CText::SetBkMode(int iMode)
{
if(iMode == OPAQUE || iMode == TRANSPARENT)
{
m_iBkMode = iMode;
return TRUE;
}
else
{
return FALSE;
}
}
//--------------------------------------------------------------------
//Description:
// Set the text color
//
//--------------------------------------------------------------------
void CText::SetTextColor(COLORREF crColor)
{
m_crTextColor = crColor;
}
//--------------------------------------------------------------------
//Description:
// Set the background color
//
//--------------------------------------------------------------------
void CText::SetBkColor(COLORREF crColor)
{
m_crBkColor = crColor;
}
//--------------------------------------------------------------------
//Description:
// Set Format.
//
//Parameters:
// The value you should see the uFormat of DrawText()
//--------------------------------------------------------------------
void CText::SetFormat(UINT uFormat)
{
m_uFormat = uFormat;
}
//--------------------------------------------------------------------
//Description:
// Set the height of text as pound
//
//---------------------------------------------------------------------
void CText::SetTextHeight(int iHeight)
{
m_iTextHeight = iHeight;
}
//--------------------------------------------------------------------
//Description:
// Get the position as rect
//
//---------------------------------------------------------------------
void CText::GetPosition(RECT *prcOut)
{
*prcOut = m_rcWndPos;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -