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 />

However, the problem is not that simple. Because the image size can be different from time to time,<br />

also the image could be displayed in various zoomed ratio, it is difficult to decide the dimension of the<br />

memory bitmap. To avoid creating and destroying memory bitmaps whenever the size (or ratio) of the<br />

output image changes, we can create a bitmap with fixed size for painting the client window. If the actual<br />

output needs a larger area, we can use a loop to update the whole client area bit by bit: within any loop we<br />

can copy a portion of the source image to the memory bitmap, add the grid, then output it to the client<br />

window. Because CDC::BitBlt(…) is a relatively fast function, and the bitmap drawing will be<br />

implemented directly by the hardware, calling this function several times will not cause obvious delay.<br />

In the sample application, some new variables are added for this purpose. We know that in order to<br />

prepare a memory bitmap, we also need to prepare a memory DC that will be used to select the bitmap. In<br />

class CGDIView, a CBitmap type variable m_bmpBKStore and a CDC type variable m_dcBKMem are declared.<br />

Also, since the DC will select the bitmap and logical palette, two other pointers CGDIView::m_pBmpBKOld<br />

and CGDIView::m_pPalBKOld are also declared. The two pointers are initialized to NULL in the constructor,<br />

and the memory bitmap CGDIView::m_bmpBKStore is created in function<br />

CGDIView::OnInitialUpdate()when an image is first loaded. The reverse procedure is done in function<br />

CDC::OnDestroy(), where the memory bitmap and the palette are selected out of the memory DC.<br />

In function CGDIView::OnDraw(…), the drawing procedure is modified. First the zoomed image is<br />

divided horizontally and vertically into small portions, all of which can fit into the memory bitmap. Then<br />

each portion of the image is copied to the memory bitmap, and the grid is added if necessary. Next the<br />

image contained in the memory bitmap is copied to the client window at the corresponding position. Here<br />

we need to calculate the origin and dimension of the output image within each loop. The following portion<br />

of function CGDIView::OnDraw(…) shows how the zoomed source image is divided into several portions<br />

and copied to the memory bitmap:<br />

……<br />

……<br />

size.cx=BMP_BKSTORE_SIZE_X/nRatio;<br />

size.cy=BMP_BKSTORE_SIZE_Y/nRatio;<br />

nRepX=(bm.bmWidth-1)/size.cx+1;<br />

nRepY=(bm.bmHeight-1)/size.cy+1;<br />

for(j=0; j

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

Saved successfully!

Ooh no, something went wrong!