//========================================================================
//TITLE:
// CText类使用例程
//AUTHOR:
// norains
//DATE:
// Tuesday 17-April-2007
//Environment:
// EVC4.0 + Standard SDK 4.2
// EVC4.0 + Standard SDK 5.0
//========================================================================
CText是为了方便在屏幕中输出文本而封装的类.该类将复杂的设置操作封装成简单的函数,便于代码书写的简便性.
该类使用简单,示例如下:
//设置显示范围
txtInfo.SetPosition(&rcWnd);
//设置字体颜色
txtInfo.SetTextColor(RGB(255,0,0));
//设置字体磅值,0为默认的字体大小
txtInfo.SetTextHeight(0);
//设置显示的文本
txtInfo.SetText(TEXT("测试"));
//设置格式,该参数和DrawText()相同
txtInfo.SetFormat(DT_RIGHT);
//绘制文本
txtInfo.Draw(hdc);
默认的是透明显示,如果需要显示背景颜色,可以如下操作:
//设置背景色
txtInfo.SetBkColor(RGB(0,0,233));
//OPAQUE为绘制背景色
txtInfo.SetBkMode(OPAQUE);
然后再调用Draw进行绘制即可.


/**////////////////////////////////////////////////////////////////////////
//Text.h:interfacefortheCTextclass.
//
//Version:
//1.0
//Data:
//2007.03.27

/**///////////////////////////////////////////////////////////////////////

#ifndefTEXT_H
#defineTEXT_H


//------------------------------------------------------------------
classCText

...{
public:
voidGetPosition(RECT*prcOut);
voidSetTextHeight(intiHeight);
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_iTextHeight;
intm_iBkMode;
COLORREFm_crTextColor;
COLORREFm_crBkColor;

};

#endif//#ifndefTEXT_H


/**///////////////////////////////////////////////////////////////////////
//Text.cpp:implementationoftheCTextclass.
//

/**///////////////////////////////////////////////////////////////////////

#include"stdafx.h"
#include"Text.h"


//--------------------------------------------------------------------
//Macrodefine
#defineDEFAULT_BKMODETRANSPARENT
#defineDEFAULT_TEXT_COLORRGB(0,0,0)
#defineDEFAULT_BK_COLORRGB(255,255,255)
#defineDEFAULT_FORMAT(DT_LEFT|DT_SINGLELINE)
#defineDEFAULT_TEXT_HEIGHT0


//--------------------------------------------------------------------

/**///////////////////////////////////////////////////////////////////////
//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:
//Setthecontrolposition
//
//--------------------------------------------------------------------
voidCText::SetPosition(constRECT*prc)

...{
m_rcWndPos=*prc;
}


//--------------------------------------------------------------------
//Description:
//Setthetext.Ifyouwanttodisplaythetext,youshouldcalltheDisplay()
//
//--------------------------------------------------------------------
BOOLCText::SetText(constTCHAR*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.
//
//--------------------------------------------------------------------
voidCText::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.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:
//Setthebackgroundmode.
//
//Parameters:
//iMode:[in]Thevalueisjustlikeasfollow:
//OPAQUE--Backgroundisfilledwiththecurrentbackgroundcolorbeforethetext,
//hatchedbrush,orpenisdrawn.
//TRANSPARENT--Backgroundremainsuntouched.

//--------------------------------------------------------------------
BOOLCText::SetBkMode(intiMode)

...{
if(iMode==OPAQUE||iMode==TRANSPARENT)

...{
m_iBkMode=iMode;
returnTRUE;
}
else

...{
returnFALSE;
}
}


//--------------------------------------------------------------------
//Description:
//Setthetextcolor
//
//--------------------------------------------------------------------
voidCText::SetTextColor(COLORREFcrColor)

...{
m_crTextColor=crColor;
}


//--------------------------------------------------------------------
//Description:
//Setthebackgroundcolor
//
//--------------------------------------------------------------------
voidCText::SetBkColor(COLORREFcrColor)

...{
m_crBkColor=crColor;
}

//--------------------------------------------------------------------
//Description:
//SetFormat.
//
//Parameters:
//ThevalueyoushouldseetheuFormatofDrawText()
//--------------------------------------------------------------------
voidCText::SetFormat(UINTuFormat)

...{
m_uFormat=uFormat;
}


//--------------------------------------------------------------------
//Description:
//Settheheightoftextaspound
//
//---------------------------------------------------------------------
voidCText::SetTextHeight(intiHeight)

...{
m_iTextHeight=iHeight;
}


//--------------------------------------------------------------------
//Description:
//Getthepositionasrect
//
//---------------------------------------------------------------------
voidCText::GetPosition(RECT*prcOut)

...{
*prcOut=m_rcWndPos;
}
//TITLE:
// CText类使用例程
//AUTHOR:
// norains
//DATE:
// Tuesday 17-April-2007
//Environment:
// EVC4.0 + Standard SDK 4.2
// EVC4.0 + Standard SDK 5.0
//========================================================================
CText是为了方便在屏幕中输出文本而封装的类.该类将复杂的设置操作封装成简单的函数,便于代码书写的简便性.
该类使用简单,示例如下:
//设置显示范围
txtInfo.SetPosition(&rcWnd);
//设置字体颜色
txtInfo.SetTextColor(RGB(255,0,0));
//设置字体磅值,0为默认的字体大小
txtInfo.SetTextHeight(0);
//设置显示的文本
txtInfo.SetText(TEXT("测试"));
//设置格式,该参数和DrawText()相同
txtInfo.SetFormat(DT_RIGHT);
//绘制文本
txtInfo.Draw(hdc);
默认的是透明显示,如果需要显示背景颜色,可以如下操作:
//设置背景色
txtInfo.SetBkColor(RGB(0,0,233));
//OPAQUE为绘制背景色
txtInfo.SetBkMode(OPAQUE);
然后再调用Draw进行绘制即可.


/**////////////////////////////////////////////////////////////////////////
//Text.h:interfacefortheCTextclass.
//
//Version:
//1.0
//Data:
//2007.03.27
/**///////////////////////////////////////////////////////////////////////
#ifndefTEXT_H
#defineTEXT_H

//------------------------------------------------------------------
classCText
...{
public:
voidGetPosition(RECT*prcOut);
voidSetTextHeight(intiHeight);
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_iTextHeight;
intm_iBkMode;
COLORREFm_crTextColor;
COLORREFm_crBkColor;
};
#endif//#ifndefTEXT_H


/**///////////////////////////////////////////////////////////////////////
//Text.cpp:implementationoftheCTextclass.
//
/**///////////////////////////////////////////////////////////////////////
#include"stdafx.h"
#include"Text.h"

//--------------------------------------------------------------------
//Macrodefine
#defineDEFAULT_BKMODETRANSPARENT
#defineDEFAULT_TEXT_COLORRGB(0,0,0)
#defineDEFAULT_BK_COLORRGB(255,255,255)
#defineDEFAULT_FORMAT(DT_LEFT|DT_SINGLELINE)
#defineDEFAULT_TEXT_HEIGHT0

//--------------------------------------------------------------------
/**///////////////////////////////////////////////////////////////////////
//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:
//Setthecontrolposition
//
//--------------------------------------------------------------------
voidCText::SetPosition(constRECT*prc)
...{
m_rcWndPos=*prc;
}

//--------------------------------------------------------------------
//Description:
//Setthetext.Ifyouwanttodisplaythetext,youshouldcalltheDisplay()
//
//--------------------------------------------------------------------
BOOLCText::SetText(constTCHAR*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.
//
//--------------------------------------------------------------------
voidCText::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.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:
//Setthebackgroundmode.
//
//Parameters:
//iMode:[in]Thevalueisjustlikeasfollow:
//OPAQUE--Backgroundisfilledwiththecurrentbackgroundcolorbeforethetext,
//hatchedbrush,orpenisdrawn.
//TRANSPARENT--Backgroundremainsuntouched.
//--------------------------------------------------------------------
BOOLCText::SetBkMode(intiMode)
...{
if(iMode==OPAQUE||iMode==TRANSPARENT)
...{
m_iBkMode=iMode;
returnTRUE;
}
else
...{
returnFALSE;
}
}

//--------------------------------------------------------------------
//Description:
//Setthetextcolor
//
//--------------------------------------------------------------------
voidCText::SetTextColor(COLORREFcrColor)
...{
m_crTextColor=crColor;
}

//--------------------------------------------------------------------
//Description:
//Setthebackgroundcolor
//
//--------------------------------------------------------------------
voidCText::SetBkColor(COLORREFcrColor)
...{
m_crBkColor=crColor;
}
//--------------------------------------------------------------------
//Description:
//SetFormat.
//
//Parameters:
//ThevalueyoushouldseetheuFormatofDrawText()
//--------------------------------------------------------------------
voidCText::SetFormat(UINTuFormat)
...{
m_uFormat=uFormat;
}

//--------------------------------------------------------------------
//Description:
//Settheheightoftextaspound
//
//---------------------------------------------------------------------
voidCText::SetTextHeight(intiHeight)
...{
m_iTextHeight=iHeight;
}

//--------------------------------------------------------------------
//Description:
//Getthepositionasrect
//
//---------------------------------------------------------------------
voidCText::GetPosition(RECT*prcOut)
...{
*prcOut=m_rcWndPos;
}
本文介绍了一个用于简化屏幕文本输出的CText类。通过设置文本属性如位置、颜色、格式等,可在不同环境下轻松实现文本绘制。适用于EVC4.0及StandardSDK4.2/5.0。
677

被折叠的 条评论
为什么被折叠?



