//========================================================================
//TITLE:
// CText更新至V1.1.0
//AUTHOR:
// norains
//DATE:
// Saturday 28-April-2007
//Environment:
// EVC4.0 + Standard SDK 4.2
// EVC4.0 + Standard SDK 5.0
//========================================================================
版本历史:
v1.0: http://blog.csdn.net/norains/archive/2007/04/17/1568429.aspx
接口和上一版本的差异:
*SetTextHeight()更名为SetPointSize()
修正的bug:
*创建字体时没有lfCharSet变量,在wince5.0中会导致文字无法对准.
增加的函数:
*SetStrikeOut(BOOL bStrikeOut) //文件中划线
*SetUnderline(BOOL bUnderline) //下划线
*SetItalic(BOOL bItalic) //斜体
*SetWeight(int iWeight) //粗细
*重载 "="操作符,下列代码功能能够正常运作:
CText a, b;
a.SetText(TEXT("TEXT"));
a.SetTextColor(RGB(0,0,255));
b = a;
基本的操作可参照版本v1.0的文章
/**/ //
// Text.h:interfacefortheCTextclass.
//
// Version:
// 1.1.0
// Data:
// 2007.04.28
/**/ //
#ifndefTEXT_H
#define TEXT_H
// ------------------------------------------------------------------
class CText
... {
public:
CText&operator=(constCText&rhs);
voidSetStrikeOut(BOOLbStrikeOut);
voidSetUnderline(BOOLbUnderline);
voidSetItalic(BOOLbItalic);
BOOLSetWeight(intiWeight);
voidGetPosition(RECT*prcOut);
voidSetPointSize(intiPointSize);
voidSetFormat(UINTuFormat);
voidSetBkColor(COLORREFcrColor);
voidSetTextColor(COLORREFcrColor);
BOOLSetBkMode(intiMode);
voidDraw(HDChdc);
BOOLSetText(constTCHAR*pszText);
voidSetPosition(constRECT*prc);
CText();
virtual~CText();
protected:
RECTm_rcWndPos;
TCHAR*m_pszText;
ULONGm_ulSizeText;
UINTm_uFormat;
intm_iPointSize;
intm_iBkMode;
COLORREFm_crTextColor;
COLORREFm_crBkColor;
intm_iWeight;
BOOLm_bItalic;
BOOLm_bUnderline;
BOOLm_bStrikeOut;
} ;
#endif // !defined(AFX_TEXT_H__15E5AD94_9958_4AAC_A6DD_37527FC294CB__INCLUDED_)
/**/ //
// Text.cpp:implementationoftheCTextclass.
//
/**/ //
#include " stdafx.h "
#include " Text.h "
// --------------------------------------------------------------------
// Macrodefine
#define DEFAULT_BKMODETRANSPARENT
#define DEFAULT_TEXT_COLORRGB(0,0,0)
#define DEFAULT_BK_COLORRGB(255,255,255)
#define DEFAULT_FORMAT(DT_LEFT|DT_SINGLELINE)
#define DEFAULT_POINT_SIZE0
#define DEFAULT_WEIGHT0
#define MAX_WEIGHT1000
#define MIN_WEIGHT0
// --------------------------------------------------------------------
/**/ //
// 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_iPointSize=DEFAULT_POINT_SIZE;
m_iWeight=0;
m_bItalic=FALSE;
m_bUnderline=FALSE;
m_bStrikeOut=FALSE;
}
CText:: ~ CText()
... {
if(m_pszText!=NULL)
...{
delete[]m_pszText;
m_pszText=NULL;
}
}
// --------------------------------------------------------------------
// Description:
// Setthecontrolposition
//
// --------------------------------------------------------------------
void CText::SetPosition( const RECT * prc)
... {
m_rcWndPos=*prc;
}
// --------------------------------------------------------------------
// Description:
// Setthetext.Ifyouwanttodisplaythetext,youshouldcalltheDisplay()
//
// --------------------------------------------------------------------
BOOLCText::SetText( const TCHAR * pszText)
... {
ULONGulLen=_tcslen(pszText);
if(m_pszText==NULL)
...{
m_pszText=newTCHAR[ulLen+1];
if(m_pszText==NULL)
...{
returnFALSE;
}
m_ulSizeText=ulLen+1;
}
elseif(ulLen+1>m_ulSizeText)
...{
delete[]m_pszText;
m_pszText=newTCHAR[ulLen+1];
if(m_pszText==NULL)
...{
returnFALSE;
}
m_ulSizeText=ulLen+1;
}
_tcscpy(m_pszText,pszText);
returnTRUE;
}
// --------------------------------------------------------------------
// Description:
// Displaythetextstored.
//
// --------------------------------------------------------------------
void CText::Draw(HDChdc)
... {
COLORREFcrOldTextColor=::SetTextColor(hdc,m_crTextColor);
COLORREFcrOldBkColor=::SetBkColor(hdc,m_crBkColor);
intiOldMode=::SetBkMode(hdc,m_iBkMode);
LOGFONTlf=...{0};
HFONThFontNew,hFontOld;
lf.lfQuality=CLEARTYPE_QUALITY;
lf.lfCharSet=DEFAULT_CHARSET;
lf.lfHeight=-1*(m_iPointSize*GetDeviceCaps(hdc,LOGPIXELSY)/72);
lf.lfItalic=m_bItalic;
lf.lfUnderline=m_bUnderline;
lf.lfStrikeOut=m_bStrikeOut;
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:
// Setthebackgroundmode.
//
// Parameters:
// iMode:[in]Thevalueisjustlikeasfollow:
// OPAQUE--Backgroundisfilledwiththecurrentbackgroundcolorbeforethetext,
// hatchedbrush,orpenisdrawn.
// TRANSPARENT--Backgroundremainsuntouched.
// --------------------------------------------------------------------
BOOLCText::SetBkMode( int iMode)
... {
if(iMode==OPAQUE||iMode==TRANSPARENT)
...{
m_iBkMode=iMode;
returnTRUE;
}
else
...{
returnFALSE;
}
}
// --------------------------------------------------------------------
// Description:
// Setthetextcolor
//
// --------------------------------------------------------------------
void CText::SetTextColor(COLORREFcrColor)
... {
m_crTextColor=crColor;
}
// --------------------------------------------------------------------
// Description:
// Setthebackgroundcolor
//
// --------------------------------------------------------------------
void CText::SetBkColor(COLORREFcrColor)
... {
m_crBkColor=crColor;
}
// --------------------------------------------------------------------
// Description:
// SetFormat.
//
// Parameters:
// ThevalueyoushouldseetheuFormatofDrawText()
// --------------------------------------------------------------------
void CText::SetFormat(UINTuFormat)
... {
m_uFormat=uFormat;
}
// --------------------------------------------------------------------
// Description:
// Setthepointsizeoftext
//
// ---------------------------------------------------------------------
void CText::SetPointSize( int iPointSize)
... {
m_iPointSize=iPointSize;
}
// --------------------------------------------------------------------
// Description:
// Getthepositionasrect
//
// ---------------------------------------------------------------------
void CText::GetPosition(RECT * prcOut)
... {
*prcOut=m_rcWndPos;
}
// --------------------------------------------------------------------
// Description:
// Specifiestheweightofthefontintherange0through1000.Forexample,
// 400isnormaland700isbold.Ifthisvalueiszero,adefaultweightisused.
//
// ---------------------------------------------------------------------
BOOLCText::SetWeight( int iWeight)
... {
if(iWeight<MIN_WEIGHT||iWeight>MAX_WEIGHT)
...{
returnFALSE;
}
m_iWeight=iWeight;
returnTRUE;
}
// --------------------------------------------------------------------
// Description:
// Settheitalic
//
// ---------------------------------------------------------------------
void CText::SetItalic(BOOLbItalic)
... {
m_bItalic=bItalic;
}
// --------------------------------------------------------------------
// Description:
// Settheunderline
//
// ---------------------------------------------------------------------
void CText::SetUnderline(BOOLbUnderline)
... {
m_bUnderline=bUnderline;
}
// --------------------------------------------------------------------
// Description:
// SetthestrikeOut
//
// ---------------------------------------------------------------------
void CText::SetStrikeOut(BOOLbStrikeOut)
... {
m_bStrikeOut=bStrikeOut;
}
// --------------------------------------------------------------------
// Description:
// Overloadtheoperator"="
//
// ---------------------------------------------------------------------
CText & CText:: operator = ( const CText & rhs)
... {
if(this==&rhs)
...{
return*this;
}
this->m_bItalic=rhs.m_bItalic;
this->m_bStrikeOut=rhs.m_bStrikeOut;
this->m_bUnderline=rhs.m_bUnderline;
this->m_crBkColor=rhs.m_crBkColor;
this->m_crTextColor=rhs.m_crTextColor;
this->m_iBkMode=rhs.m_iBkMode;
this->m_iPointSize=rhs.m_iPointSize;
this->m_iWeight=rhs.m_iWeight;
this->m_rcWndPos=rhs.m_rcWndPos;
this->m_uFormat=rhs.m_uFormat;
this->m_ulSizeText=rhs.m_ulSizeText;
if(this->m_pszText!=NULL)
...{
delete[]this->m_pszText;
}
this->m_pszText=newTCHAR[_tcslen(rhs.m_pszText)+1];
_tcscpy(this->m_pszText,rhs.m_pszText);
return*this;
}
//TITLE:
// CText更新至V1.1.0
//AUTHOR:
// norains
//DATE:
// Saturday 28-April-2007
//Environment:
// EVC4.0 + Standard SDK 4.2
// EVC4.0 + Standard SDK 5.0
//========================================================================
版本历史:
v1.0: http://blog.csdn.net/norains/archive/2007/04/17/1568429.aspx
接口和上一版本的差异:
*SetTextHeight()更名为SetPointSize()
修正的bug:
*创建字体时没有lfCharSet变量,在wince5.0中会导致文字无法对准.
增加的函数:
*SetStrikeOut(BOOL bStrikeOut) //文件中划线
*SetUnderline(BOOL bUnderline) //下划线
*SetItalic(BOOL bItalic) //斜体
*SetWeight(int iWeight) //粗细
*重载 "="操作符,下列代码功能能够正常运作:
CText a, b;
a.SetText(TEXT("TEXT"));
a.SetTextColor(RGB(0,0,255));
b = a;
基本的操作可参照版本v1.0的文章
/**/ //
// Text.h:interfacefortheCTextclass.
//
// Version:
// 1.1.0
// Data:
// 2007.04.28
/**/ //
#ifndefTEXT_H
#define TEXT_H
// ------------------------------------------------------------------
class CText
... {
public:
CText&operator=(constCText&rhs);
voidSetStrikeOut(BOOLbStrikeOut);
voidSetUnderline(BOOLbUnderline);
voidSetItalic(BOOLbItalic);
BOOLSetWeight(intiWeight);
voidGetPosition(RECT*prcOut);
voidSetPointSize(intiPointSize);
voidSetFormat(UINTuFormat);
voidSetBkColor(COLORREFcrColor);
voidSetTextColor(COLORREFcrColor);
BOOLSetBkMode(intiMode);
voidDraw(HDChdc);
BOOLSetText(constTCHAR*pszText);
voidSetPosition(constRECT*prc);
CText();
virtual~CText();
protected:
RECTm_rcWndPos;
TCHAR*m_pszText;
ULONGm_ulSizeText;
UINTm_uFormat;
intm_iPointSize;
intm_iBkMode;
COLORREFm_crTextColor;
COLORREFm_crBkColor;
intm_iWeight;
BOOLm_bItalic;
BOOLm_bUnderline;
BOOLm_bStrikeOut;
} ;
#endif // !defined(AFX_TEXT_H__15E5AD94_9958_4AAC_A6DD_37527FC294CB__INCLUDED_)
/**/ //
// Text.cpp:implementationoftheCTextclass.
//
/**/ //
#include " stdafx.h "
#include " Text.h "
// --------------------------------------------------------------------
// Macrodefine
#define DEFAULT_BKMODETRANSPARENT
#define DEFAULT_TEXT_COLORRGB(0,0,0)
#define DEFAULT_BK_COLORRGB(255,255,255)
#define DEFAULT_FORMAT(DT_LEFT|DT_SINGLELINE)
#define DEFAULT_POINT_SIZE0
#define DEFAULT_WEIGHT0
#define MAX_WEIGHT1000
#define MIN_WEIGHT0
// --------------------------------------------------------------------
/**/ //
// 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_iPointSize=DEFAULT_POINT_SIZE;
m_iWeight=0;
m_bItalic=FALSE;
m_bUnderline=FALSE;
m_bStrikeOut=FALSE;
}
CText:: ~ CText()
... {
if(m_pszText!=NULL)
...{
delete[]m_pszText;
m_pszText=NULL;
}
}
// --------------------------------------------------------------------
// Description:
// Setthecontrolposition
//
// --------------------------------------------------------------------
void CText::SetPosition( const RECT * prc)
... {
m_rcWndPos=*prc;
}
// --------------------------------------------------------------------
// Description:
// Setthetext.Ifyouwanttodisplaythetext,youshouldcalltheDisplay()
//
// --------------------------------------------------------------------
BOOLCText::SetText( const TCHAR * pszText)
... {
ULONGulLen=_tcslen(pszText);
if(m_pszText==NULL)
...{
m_pszText=newTCHAR[ulLen+1];
if(m_pszText==NULL)
...{
returnFALSE;
}
m_ulSizeText=ulLen+1;
}
elseif(ulLen+1>m_ulSizeText)
...{
delete[]m_pszText;
m_pszText=newTCHAR[ulLen+1];
if(m_pszText==NULL)
...{
returnFALSE;
}
m_ulSizeText=ulLen+1;
}
_tcscpy(m_pszText,pszText);
returnTRUE;
}
// --------------------------------------------------------------------
// Description:
// Displaythetextstored.
//
// --------------------------------------------------------------------
void CText::Draw(HDChdc)
... {
COLORREFcrOldTextColor=::SetTextColor(hdc,m_crTextColor);
COLORREFcrOldBkColor=::SetBkColor(hdc,m_crBkColor);
intiOldMode=::SetBkMode(hdc,m_iBkMode);
LOGFONTlf=...{0};
HFONThFontNew,hFontOld;
lf.lfQuality=CLEARTYPE_QUALITY;
lf.lfCharSet=DEFAULT_CHARSET;
lf.lfHeight=-1*(m_iPointSize*GetDeviceCaps(hdc,LOGPIXELSY)/72);
lf.lfItalic=m_bItalic;
lf.lfUnderline=m_bUnderline;
lf.lfStrikeOut=m_bStrikeOut;
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:
// Setthebackgroundmode.
//
// Parameters:
// iMode:[in]Thevalueisjustlikeasfollow:
// OPAQUE--Backgroundisfilledwiththecurrentbackgroundcolorbeforethetext,
// hatchedbrush,orpenisdrawn.
// TRANSPARENT--Backgroundremainsuntouched.
// --------------------------------------------------------------------
BOOLCText::SetBkMode( int iMode)
... {
if(iMode==OPAQUE||iMode==TRANSPARENT)
...{
m_iBkMode=iMode;
returnTRUE;
}
else
...{
returnFALSE;
}
}
// --------------------------------------------------------------------
// Description:
// Setthetextcolor
//
// --------------------------------------------------------------------
void CText::SetTextColor(COLORREFcrColor)
... {
m_crTextColor=crColor;
}
// --------------------------------------------------------------------
// Description:
// Setthebackgroundcolor
//
// --------------------------------------------------------------------
void CText::SetBkColor(COLORREFcrColor)
... {
m_crBkColor=crColor;
}
// --------------------------------------------------------------------
// Description:
// SetFormat.
//
// Parameters:
// ThevalueyoushouldseetheuFormatofDrawText()
// --------------------------------------------------------------------
void CText::SetFormat(UINTuFormat)
... {
m_uFormat=uFormat;
}
// --------------------------------------------------------------------
// Description:
// Setthepointsizeoftext
//
// ---------------------------------------------------------------------
void CText::SetPointSize( int iPointSize)
... {
m_iPointSize=iPointSize;
}
// --------------------------------------------------------------------
// Description:
// Getthepositionasrect
//
// ---------------------------------------------------------------------
void CText::GetPosition(RECT * prcOut)
... {
*prcOut=m_rcWndPos;
}
// --------------------------------------------------------------------
// Description:
// Specifiestheweightofthefontintherange0through1000.Forexample,
// 400isnormaland700isbold.Ifthisvalueiszero,adefaultweightisused.
//
// ---------------------------------------------------------------------
BOOLCText::SetWeight( int iWeight)
... {
if(iWeight<MIN_WEIGHT||iWeight>MAX_WEIGHT)
...{
returnFALSE;
}
m_iWeight=iWeight;
returnTRUE;
}
// --------------------------------------------------------------------
// Description:
// Settheitalic
//
// ---------------------------------------------------------------------
void CText::SetItalic(BOOLbItalic)
... {
m_bItalic=bItalic;
}
// --------------------------------------------------------------------
// Description:
// Settheunderline
//
// ---------------------------------------------------------------------
void CText::SetUnderline(BOOLbUnderline)
... {
m_bUnderline=bUnderline;
}
// --------------------------------------------------------------------
// Description:
// SetthestrikeOut
//
// ---------------------------------------------------------------------
void CText::SetStrikeOut(BOOLbStrikeOut)
... {
m_bStrikeOut=bStrikeOut;
}
// --------------------------------------------------------------------
// Description:
// Overloadtheoperator"="
//
// ---------------------------------------------------------------------
CText & CText:: operator = ( const CText & rhs)
... {
if(this==&rhs)
...{
return*this;
}
this->m_bItalic=rhs.m_bItalic;
this->m_bStrikeOut=rhs.m_bStrikeOut;
this->m_bUnderline=rhs.m_bUnderline;
this->m_crBkColor=rhs.m_crBkColor;
this->m_crTextColor=rhs.m_crTextColor;
this->m_iBkMode=rhs.m_iBkMode;
this->m_iPointSize=rhs.m_iPointSize;
this->m_iWeight=rhs.m_iWeight;
this->m_rcWndPos=rhs.m_rcWndPos;
this->m_uFormat=rhs.m_uFormat;
this->m_ulSizeText=rhs.m_ulSizeText;
if(this->m_pszText!=NULL)
...{
delete[]this->m_pszText;
}
this->m_pszText=newTCHAR[_tcslen(rhs.m_pszText)+1];
_tcscpy(this->m_pszText,rhs.m_pszText);
return*this;
}