GDI Topics: Drawing Text

 

Introduction

By default, the CDC class is able to draw text using a font pre-selected, known as the System Font. To draw text, you can use the CDC::TextOut() method. Its syntax is:

virtual BOOL TextOut(int x, int y, 
		     LPCTSTR lpszString,
		     int nCount);

To us this method, you must specify where the text would start. This location is determined from the (0, 0) origin to the right (x) and to the bottom (y). The text to display is the lpszString. The nCount value is the length of the text. Here is an example:

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

	pDC->TextOut(50, 42, "Johnny Carson", 13);
}

If you want to control the color used to draw the text, use the CDC::SetTextColor() method whose syntax is:

virtual COLORREF SetTextColor(COLORREF crColor);

The argument can be provided as a COLORREF variable or by calling the RGB macro. is an example:

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

	pDC->SetTextColor(RGB(255, 25, 2));
	pDC->TextOut(50, 42, "Johnny Carson", 13);
}

As you will learn from now on concerning the device context, once you change one of its characteristics, it stays there until you change it again. It is similar to picking a spoon and start eating. As long as you are eating, you can use only the spoon and only that spoon. It you want to cut the meat, you must replace the spoon in your hand with a knife. In the same way, if you change the color of text and draw more than one line of text, all of them would use the same color. If you want to use a different color, you must change the color used by calling the SetTextColor() method again. Here is an example:

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

	pDC->SetTextColor(RGB(255, 25, 2));
	pDC->TextOut(50, 42, "Johnny Carson", 13);

	pDC->SetTextColor(RGB(12, 25, 255));
	pDC->TextOut(50, 80, "The once king of late-night", 27);
}
Text Out With a Different Color

If you want to highlight the text, which is equivalent to changing its background, you can call the CDC::SetBkColor() method. Its syntax is:

virtual COLORREF SetBkColor(COLORREF crColor);

You must provide the color you want to use as the crColor argument. If this method succeed, it changes the background of the next text that would be drawn and it returns the previous background color, which you can restore at a later time. Here is an example:

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

	pDC->SetTextColor(RGB(255, 25, 2));
	pDC->TextOut(50, 42, "Johnny Carson", 13);

	pDC->SetBkColor(RGB(0, 0, 128));
	pDC->SetTextColor(RGB(128, 255, 255));
	pDC->TextOut(50, 60, "The once king of late-night", 27);
}

If you want to know the background color applied on the object drawn, you can call the CDC::GetBkColor() method. Its syntax is:

COLORREF GetBkColor() const;

This member function returns the color used to highlight the text, if the text is highlighted.

The highlighting of text is actually controlled by the CDC::SetBkMode() method whose syntax is:

int SetBkMode(int nBkMode);

This method specifies whether the background color should be applied or not. This is set by the nBkMode argument. It can have one of two values. If it is:

  • OPAQUE: the background would be drawn using the crColor value
  • TRANSPARENT: the background would not be drawn

If you want to find out what background mode is applied to the object(s) drawn, you can call the CDC::GetBkMode() method. It is declared as follows:

int GetBkMode() const;

You can also draw text and include it in a (colored) rectangle. This can be done using the CDC::ExtTextOut() method. Its syntax is:

virtual BOOL ExtTextOut(int x, int y, UINT nOptions, LPCRECT lpRect,
		      LPCTSTR lpszString, UINT nCount, LPINT lpDxWidths);

The x and y values specify the location of the first character of the text to be drawn.

The nOptions argument holds a constant that determines how the rectangle will be drawn. It can be:

  • ETO_OPAQUE: in this case the color set by SetBkColor() would be used to fill the rectangle
  • ETO_CLIPPED: the color previously specified by SetBkColor() will only highlight the text

The lpRect is a RECT or CRect rectangle that will be drawn behind the text.

The lpszString value is the text to be drawn.

The nCount is the number of characters of lpszString.

The lpDxWidths argument is an array of integers that specifies the amount of empty spaces that will be used between each combination of two characters. Unless you know what you are doing, set this argument as 0, in which case the regular space used to separate characters would be used.

Here is an example:

 

 

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

    pDC->SetTextColor(RGB(25, 55, 200));
    pDC->SetBkColor(RGB(128, 255, 255));
    pDC->ExtTextOut(50, 42, ETO_OPAQUE, CRect(20, 28, 188, 128), "Johnny Carson", 13, NULL);
}
Extended Text Out
 

Home Copyright © 2003-2005 FunctionX, Inc.