11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8. DC, Pen, Brush and Palette<br />

1) Select the GDI object into the DC, use a pointer to store the old GDI object.<br />

1) Perform drawing operations.<br />

1) Select the old GDI object into the DC, this will select the new GDI object out of the DC.<br />

1) Destroy the GDI object if necessary (If the GDI object was created in step 2 and will not be used by<br />

other DCs from now on).<br />

The following sections will discuss how to use specific GDI objects to draw various kind of graphic<br />

objects.<br />

8.1 Line<br />

Creating Pen<br />

Sample 8.1\GDI demonstrates how to create a pen and use it to draw lines. The sample is a standard<br />

SDI application generated from Application Wizard.<br />

To draw a line, we need the following information: starting and ending points, width of the line,<br />

pattern of the line, and color. There are several types of pens that can be created: solid pen, dotted pen,<br />

dashed pen. Besides drawing patterns, each pen can have a different color and different width. So if we<br />

want to draw two types of lines, we need to create two different pens.<br />

In <strong>MFC</strong>, pen is supported by class CPen, it has a member function CPen::CreatePen(…), which can be<br />

used to create a pen. This function has several versions, the following is one of them:<br />

BOOL CPen::CreatePen(int nPenStyle, int nWidth, COLORREF crColor);<br />

Parameter nPenStyle specifies the pen style, it can be any of the following: PS_SOLID, PS_DASH,<br />

PS_DOT, PS_DASHDOT, PS_DASHDOTDOT, PS_NULL, etc. The meanings of these styles are self-explanatory. The<br />

second parameter nWidth specifies width of the pen. Please note that if we create pen with a style other than<br />

PS_SOLID, this width can not exceed 1 device unit. Parameter crColor specifies color of the pen, which can<br />

be specified using RGB macro.<br />

In <strong>MFC</strong>’s document/view structure, the data should be stored in CDocument derived class and the<br />

drawing should be carried out in CView derived class (for SDI or MDI applications). Since CView is derived<br />

from CWnd, we can obtain its DC by either calling function CWnd::GetDC() or declare a CClientDC type<br />

variable using the window’s pointer. To select a GDI object into the DC, we need to call function<br />

CDC::SelectObject(…). This function returns a pointer to a GDI object of the same type that is being<br />

selected by the DC. Before we delete the GDI object, we need to use this pointer to select the old GDI<br />

object into the DC so that the new DC will be selected out of the DC.<br />

We can not delete a GDI object when it is being selected by a DC. If we do so, the application will<br />

become abnormal, and may cause GPF (General protection fault) error.<br />

Drawing Mode<br />

Besides drawing lines, sample 8.1\GDI also demonstrates how to implement an interactive<br />

environment that lets the user use mouse to draw lines anywhere within the client window. In the sample,<br />

the user can start drawing by clicking mouse’s left button, and dragging the mouse with left button held<br />

down, releasing the button to finish the drawing. When the user is dragging the mouse, dotted outlines will<br />

be drawn temporarily on the window. After the ending point is decided, the line will be actually drawn<br />

(with red color and a width of 1). In order to implement this, the following messages are handled in the<br />

application: WM_LBUTTONDOWN, WM_MOUSEMOVE and WM_LBUTTONUP.<br />

Before the left mouse button is released, we have to erase the old outline before drawing a new one in<br />

order to give the user an impression that the outline “moves” with the mouse cursor. The best way to<br />

implement this type of operations is using XOR drawing mode. With this method, we can simply draw an<br />

object twice in order to remove it from the device.<br />

XOR bit-wise operation is very powerful, we can use it to generate many special drawing effects.<br />

Remember when we perform a drawing operation, what actually happens in the hardware level is that data<br />

202

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!