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 11. Sample: Simple Paint<br />

CGDIDoc::GetGridOn() is added to allow its value be retrieved outside the document. Also, a new button<br />

(whose command ID is ID_GRID) is added to tool bar IDR_MAINFRAME, whose message handlers are also<br />

added through using Class Wizard. The value of m_bGridOn is toggled between TRUE and FALSE in<br />

function CGDIDoc::OnGrid(). Within function CGDIDoc::OnUpdateGrid(…), the button’s state (checked or<br />

unchecked) is set to represent the current state of grid:<br />

void CGDIDoc::OnUpdateGrid(CCmdUI* pCmdUI)<br />

{<br />

pCmdUI->SetCheck(m_bGridOn == TRUE);<br />

}<br />

We must modify function CGDIView::OnDraw(…) to implement grid. First we need to check the current<br />

value of CGDIDoc::m_bGridOn. If it is TRUE, we should draw both the image and the grid; if it is FALSE,<br />

we need to draw only the image.<br />

We can draw various types of grids, for example, the simplest way to implement grid would be just<br />

drawing parallel horizontal and vertical lines. However, there is a disadvantage of implementing grid with<br />

solid lines. If the image happens to have the same color with grid lines, the grid will become unable to be<br />

seen. An alternate solution is to draw grid lines using image’s complement colors, this can be easily<br />

implemented by calling function CDC::SetROP2(…) and passing R2_NOT to its parameter before the grid is<br />

drawn. However, this type of grid does not have a uniform color, this makes the image looks a little<br />

awkward.<br />

Pattern Brush and Its Origin<br />

The best grid is implemented with alternate colors (for example, black and white), thus at any time one<br />

of the two contiguous grid pixels will always have a different color with the image pixels under them<br />

(When an image is enlarged, one pixel of the image will become several pixels). We might want to use<br />

dashed or dotted lines to implement this type of grid. However, the alternating frequency of dotted or<br />

dashed lines is not one pixel.<br />

In the sample a pattern brush is used to implement grid. Remember we can use pattern brush to fill a<br />

rectangle with a bitmap pattern. If we limit width and height of the pattern brush to 1 unit, the filling result<br />

will become a straight line (vertical or horizontal). We can prepare a bitmap with the pattern that any two<br />

adjacent pixels (not diagnal) have different colors, and use it to create the pattern brush (Figure 11-1).<br />

Figure 11-1. Bitmap used to implement pattern brush<br />

If we write program for Windows 95, the size of the bitmap for making pattern brush must be 8×8. In<br />

the sample, this image is included in the application as a bitmap resource, whose ID is IDB_BITMAP_GRID.<br />

The variable used for creating pattern brush is CGDIView::m_brGrid, and the pattern brush will be created<br />

in the constructor of class CGDIView.<br />

In function CGDIView::OnDraw(…), after drawing the bitmap, we must obtain the value of<br />

CGDIDoc::m_bGridOn. If it is true, we will use the pattern brush to draw the grid. When using pattern brush,<br />

we must pay special attention to its origin. By default, the brush’s origin will always be set to (0, 0). This<br />

will not cause problem so long as the client window is not scrolled. However, if scrolled position (either<br />

horizontal or vertical, but not both) happens not to be an even number, we need to adjust the origin of the<br />

pattern brush to let the pattern be drawn started from 1 (horizontal or vertical coordinate). This is because<br />

our pattern repeats every other pixel.<br />

337

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

Saved successfully!

Ooh no, something went wrong!