11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11. Sample: Simple Paint<br />

Figure 11.8 A Path<br />

A path can record almost all types of outputs to the device context. Like other GDI objects, it must be<br />

first selected into a DC before being used. However, there is no class such as CPath that lets us declare a<br />

path type variable. Therefore, we can not select a path into DC by calling function CDC::<br />

SelectObject(…). To use path, we must call function CDC::BeginPath() to start path recording and call<br />

CDC::EndPath() to end it.<br />

Between the above two functions, we can call any of the drawing functions such as CDC::LineTo(…),<br />

CDC::Rectangle(…), and CDC::TextOut(…). The trace of the output will be recorded in the path and can be<br />

rendered later. When rendering the recorded path, we can either draw only the outline of the path using the<br />

selected pen or fill the interior with the selected brush, or we can do both.<br />

The following functions can be used to implement these path drawing:<br />

BOOL CDC::StrokePath();<br />

BOOL CDC::FillPath();<br />

BOOL CDC::StrokeAndFillPath();<br />

Function CDC::StrokePath() will render a specific path using the currently selected pen. This will<br />

draw outline of the closed figure. Function CDC::FillPath() will close any open figures in the path and fill<br />

its interior using the currently selected brush. After the interior is filled, the path will be discarded from the<br />

device context. Function CDC::StrokeAndFillPath() implements both: it will stroke the outline of the path<br />

and fill the interior.<br />

Please note that the last function can not be replaced by calling the first two functions consecutively.<br />

After function CDC::StrokePath() is called, the path will be discarded, so further calling<br />

CDC::FillPath() will not have any effect.<br />

Path & Region<br />

Region can also be created from path. One straightforward method is to call function CDC::<br />

SelectClipPath(…) to create a region from the current path then select it into the DC. If we create region<br />

this way, there is no need for us to use CRgn type variable. Also, we can explicitly create a region from the<br />

path by calling function CRgn::CreateFromPath(…). The parameter we need to pass to this function is a<br />

pointer to the DC that contains the path. This is a very powerful method: by creating an irregular-shaped<br />

path, we can use it to create a region that can be used to confine the output of DC.<br />

Sample 11.7-1\GDI<br />

Sample 11.7-1\GDI demonstrates path implementation. It is a standard SDI application generated by<br />

Application Wizard. No new variable is declared. In function CGDIView::OnDraw(…), we begin path<br />

recording and output four characters ‘P’, ‘a’, ‘t’, ‘h’ to the client window. Then we stroke the outlines of<br />

the four characters and fill the path with a hatched brush.<br />

In the sample, the font used to output the text is “Times New Roman”, and its height is 400. The brush<br />

used to fill the interior of the path is a hatched brush whose pattern is cross hatch at 45 degrees. Between<br />

function CDC::BeginPath() and CDC::EndPath(), there is only one statement that calls function<br />

CDC::TextOut(…) to output the four characters. Please note that while path recording is undergoing, no<br />

output will be generated to the target device. So this will not output anything to the client window. Finally<br />

354

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

Saved successfully!

Ooh no, something went wrong!