YOU CAN CODE!

 

With The Case Of UCanCode.net  Release The Power OF  Visual C++ !   HomeProducts | PurchaseSupport | Downloads  
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Overview
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
Purchase
ActiveX COM Products
Overview
Download
Purchase
Technical Support
  General Q & A
Discussion Board
Contact Us

Links

Get Ready to Unleash the Power of UCanCode .NET

 


UCanCode Software focuses on general application software development. We provide complete solution for developers. No matter you want to develop a simple database workflow application, or an large flow/diagram based system, our product will provide a complete solution for you. Our product had been used by hundreds of top companies around the world!

"100% source code provided! Free you from not daring to use components because of unable to master the key technology of components!"


 

GDI Topics: Drawing Text, TextOut, ExtTextOut, SetTextColor

 

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

 

 

Copyright ?1998-2022 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to webmaster@ucancode.net