VC++ tutorial

Visual C++ Examples

How To Add Menu In VC++

adplus-dvertising
Font In VC++
Previous Home Next

A font is a list of symbols that can be drawn on a device context to produce a readable message. A font is designed by an artist but usually follows a specific pattern. Fonts are GDI objects, A collection of characters and other symbols that share the same attributes is a font.

Although this is the general principle of a font, it is possible for a font to be made of a series of non-readable symbols.

Selection of Fonts:

Before using a font to draw text in a device, the font must have been installed on the computer. Microsoft Windows installs many fonts during setup. To handle its various assignments, the operating system uses a particular font known as the System Font. If you want to use a different font to draw text in your application, you must select it.

Selecting a font, as well as selecting any other GDI object, is equivalent to specifying the characteristics of a GDI object you want to use. If you want to use a different font to draw text in your application, you must select it. To select a font, pass it as a pointer to the CDC::SelectObject method.

CFONT: A font is created as a variable of the CFont class (of course, you can also use the Win32 API's HFONT class). The CFont class is based on the CGdiObject. To declare a CFont variable, you can use the default constructor of this class. This can be easily done as follows:

Syntax:

virtual CFont* SelectObject(CFont* pFont);

Example:

void CExoView::OnDraw(CDC* pDC)
{
	CExoDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	CFont font;

	font.CreatePointFont(780, "TimeRoman");
	CFont *pFont = pDC->SelectObject(&font);
	pDC->TextOut(30, 35, "programming", 15);

	pDC->SelectObject(pFont);
	font.DeleteObject();
}

LOGFONT:The CFont::CreateFont() method is used to specify all characteristics of a font in one step. Alternatively, if you want to specify each font property, you can declare a LOGFONT variable and initialize it. After initializing the LOGFONT variable, call the CFont::CreateFontIndirect() method. Its syntax is:

Syntax:

BOOL CreateFontIndirect(const LOGFONT* lpLogFont);

Example:

void 
CDCTestView::OnDraw(CDC*PDC)
{
CRect rcClint;
GetClientRect;
GetClientRect(rcClient);
pDC->DPtoLP(rcClient);
COLORREF clrOld = PDC->SetTextColor(m_clrChoice);
int nOldMode = PDC->SetBkMode(TRANSPARENT);
CFont fntTimeRoman, fntBoldSwiss;
fntArial.CreateFont(1,1,1,1,1,1,1,1,1,1,"TimeRoman");
ftnBoldSwiss.CreateFont(rcClient.Height()/40,0,FW_BOLD,TRUE,FALSE,10,ANSI_CHARSET, 
OUT_TT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH | FF_SWISS,NULL);
CString szMsg = "Hello ! change the color and mpping mod";
CFont* pOldFont = PDC->SelectObject(&fnt Arial);
int cy = rcClient.Height()/4;
PDC->TextOut(0,cy,szMsg);
PDC->SelectObject(&fntBoldSwiss);
TEXTMETRIC tm;
PDC->GetTextMetrices(&tm);
cy +=tm.tmHeight + tm.tmExternalLeading;
PDC->TextOut(0,cy szMsg);
pDC->SelectObject(pOldFont);
pDC->SelectTextColor(clrOld);
pDC->SelectObject(nOldFont);
}
Previous Home Next