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 8. DC, Pen, Brush and Palette<br />

CGDIDoc *ptrDoc;<br />

……<br />

}<br />

ptrDoc=(CGDIDoc *)GetDocument();<br />

if((nFlags & MK_LBUTTON) && ptrDoc->GetFloodFill() == FALSE)<br />

{<br />

void CGDIView::OnLButtonUp(UINT nFlags, CPoint point)<br />

{<br />

CClientDC dc(this);<br />

CPen pen;<br />

CPen *ptrPenOld;<br />

int nMode;<br />

CGDIDoc *ptrDoc;<br />

……<br />

}<br />

ptrDoc=(CGDIDoc *)GetDocument();<br />

if(ptrDoc->GetFloodFill() == FALSE)<br />

{<br />

This will enable flood filling.<br />

Sample 8.5-2\GDI is exactly the same with sample 8.5-1\GDI except that here we use function CDC::<br />

ExtFloodFill(…) instead of CDC::FloodFill(…). The following is the difference between the function<br />

calling in two samples:<br />

8.5-1\GDI:<br />

dc.FloodFill(point.x, point.y, RGB(255, 0, 0));<br />

8.5-2\GDI:<br />

dc.ExtFloodFill(point.x, point.y, dc.GetPixel(point), FLOODFILLSURFACE);<br />

We call function CDC::GetPixel(…) to retrieve the color of the pixel specified by point, and fill the<br />

area that has the same color with it. The flood filling will stretch out to all directions until borders with<br />

different colors are reached. For samples 8.5-1\GDI and 8.5-2\GDI, we don’t see much difference between<br />

function CDC::ExtFloodFill(…) and CDC::FloodFill(…). However, in the sample, if the lines can be<br />

drawn with different colors, we need to use CDC::ExtFloodFill(…) rather than CDC::FloodFill(…) to<br />

implement flood filling.<br />

8.6 Pattern Brush<br />

Pattern brush is very useful in filling an area with a specific pattern. We already know how to create a<br />

hatch brush, which could have several simple patterns. By using pattern brush, we are able to create a brush<br />

with any custom pattern.<br />

The pattern brush can be created from a bitmap with a dimension of 8×8. If we use a larger bitmap<br />

image, only the upper-left portion of the image (with a size of 8×8) will be used. The bitmap used to create<br />

pattern brush must be stored in a CBitamp declared variable.<br />

The simplest way of implementing pattern brush is to prepare a bitmap resource, load it with a CBitmap<br />

type variable, then create the brush. The function that can be used to create pattern brush is CBrush::<br />

CreatePatternBrush(…), which has the following format:<br />

BOOL CBrush::CreatePatternBrush(CBitmap* pBitmap);<br />

Sample 8.6\GDI demonstrates how to create and use pattern brush. It is based on sample 8.2-1\GDI. In<br />

the new sample, pattern brush is used to fill the interior of the rectangles instead of solid brush.<br />

219

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

Saved successfully!

Ooh no, something went wrong!