Webocreation

Monday, November 23, 2009

Using the CDC Class

Using the CDC Class

The CDC class contains a large amount of functionality. This section introduces you to the CDC class and several of its important text and graphics functions. Classes derived from the CDC class are also discussed.

The CDC class defines a class of device context objects. The CDC class provides member functions for device context operations, working with drawing tools, graphics device interface (GDI) object selection, and working with colors and palettes. Member functions are also provided for drawing text.

This section includes the following topics:

Setting Colors

In Windows, colors for both text and graphics are represented by a 32-bit value. Because MFC does not contain a class that encapsulates color information, MFC and SDK developers use the COLORREF data type. To see an illustration that shows COLORREF using eight bits, or a range of 0 – 255, for each of the red, green, and blue color components, click this icon.




The high-order eight bits of a COLORREF should not be changed. These bits must contain zeros in order for a COLORREF variable to function correctly.

Windows provides the following macros to manipulate colors:

® RGB, which returns the COLORREF value based on the component values.

® GetRValue, which returns the red component of a COLORREF value.

® GetGValue, which returns the green component of a COLORREF value.

® GetBValue, which returns the blue component of a COLORREF value.

The following example code illustrates the use of these macros:

COLORREF color = RGB(12,34,56);
CBrush brush(color);

TRACE("The Red component of the brush is %d\n", GetRValue(color));

TRACE("The Green component of the brush is %d\n", GetGValue(color));

TRACE("The Blue component of the brush is %d\n", GetBValue(color));

Simple Graphics Functions

The CDC class provides the basic drawing shapes from which you can create more complex shapes. You can, for example, manipulate individual pixels, draw lines, or create polygons.

Pixel Manipulation

Pixel manipulation (CDC::SetPixel and CDC::GetPixel) enables you to have the finest control of drawing shapes. For example, the following example code uses OnDraw to set individual pixels at startup:

void CMyView::OnDraw(CDC* pDC)
{
// Draw a red dot at location 100, 100
pDC->SetPixel(CPoint(100,100), RGB(255, 0, 0));
}


Line Drawing Functions
Several functions are available for drawing line output.
You can draw simple line segments using the CDC::MoveTo and CDC::LineTo functions. The CDC::MoveTo function is used to set the starting position for a pen. The CDC::LineTo function then draws a line from the starting position to the new position. This new position then becomes the current position and further CDC::LineTo functions can be called.

CDC::Polyline can be used to draw a set of line segments connecting the points that are specified in an array of POINT structures or CPoint objects. The lines are drawn from the first point through subsequent points by using the current pen. Unlike the LineTo member function, the Polyline function neither uses nor updates the current position.

The following example code draws a simple X out to the device:

pDC->MoveTo(0,0);
pDC->LineTo(100,100);
pDC->MoveTo(0,100);
pDC->LineTo(100,0);


Other Drawing Functions

CDC::Rectangle draws a rectangle by using the current pen for the border and the current brush for the interior of the rectangle.

CDC::Ellipse draws an ellipse by using the current pen for the border and the current brush for the interior of the rectangle.

CDC::Polygon draws a polygon from an array of points.
CDC::FrameRect draws a rectangular border with a supplied brush.
CDC::FillRect fills a rectangular area with a supplied brush.
The following example code draws a circle embedded within a square:

CRect rect(0,0,100,100);
pDC->Rectangle(&rect);
pDC->Ellipse(&rect);

Simple Text Functions

The CDC class contains functions for drawing font-independent text. It also contains functions to set and get basic attributes of the text, such as color and alignment. Logically, drawing text consists of two operations: first, setting the attributes of the text, and then writing the text out to the device.

Text Output Functions

Several CDC functions are commonly used for text. The following table lists four functions that control text output.

Function Description


TextOut Displays a text string at the given location by using the currently selected font, colors, and alignment.

TabbedTextOut Similar to TextOut, but supports expansion of tab stops.

DrawText Displays formatted text within a bounding rectangle.

ExtTextOut Similar to TextOut, but allows specification of clipping options.

Text Display Attributes

These functions can be used to affect text display attributes. The following table lists four sets of functions that control text display attributes.

Function Description

SetTextColor and GetTextColor Control the color of the text displayed.


SetBkMode and GetBkMode Control whether or not the cells that surround each letter are filled before the corresponding characters are displayed. In Opaque mode, the cells are filled with the current background color. In Transparent mode, no fill takes place.

SetBkColor and GetBkColor Control the color of the cells that contain the displayed text. The background mode must be Opaque before this attribute is used in a fill.

SetTextAlign and GetTextAlign Control the alignment of text relative to the window position specified in one of the text output functions.

Note The background mode and color are also used for pen and brush operations. The following example code shows how to display simple text in the view:


void CSimpleTextView::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
dc.SetTextcolor(RGB(0, 0, 255));
dc.TextOut(point.x, point.y, "Hello");
CView::OnLButtonDown(nFlags, point);
}

CDC-Derived Classes

There are four CDC-derived classes that provide specialized device contexts.

CDC-derived class Drawing area affected

CClientDC Client area of a window

CPaintDC Invalid region of a client area
CWindowDC Client and nonclient areas
CMetafileDC A metafile

Note You can also use a CDC object to create a memory device context, which is compatible with an existing DC. This would be useful for preparing images in memory before you transfer them to the device. A simple example of painting text to the screen uses the derived class CPaintDC, called from the OnPaint member function. The following example code shows you how to paint a single line of text to the center of the screen, using the DrawText member function:


void CMyView::OnPaint()
{
CPaintDC dc(this);

CRect rect;

GetClientRect(&rect);
dc.DrawText("Hello world", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}

Note The – 1 tells Windows that the string is null-terminated; the three flags tell it how to draw the text. The DrawText function uses the default system font. If you wanted to use another font, you would use the CFont class. For more information about CFont, see Using Fonts in this chapter.

No comments:

Post a Comment