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

color table size to starting addresses of the DIB buffer. Since one raster line must use multiple of 4 bytes,<br />

we need to use WIDTHBYTES macro to calculate the actual bytes that are used by one raster line. The<br />

following shows how the selected rectangular area of the bitmap is copied to the target bitmap:<br />

……<br />

……<br />

for(i=0; ibmiHeader.biBitCount*lpBi->bmiHeader.biWidth<br />

)*(lpBi->bmiHeader.biHeight-sizeTgtOffset.cy-i-1)<br />

);<br />

memcpy<br />

(<br />

lpRowTgt,<br />

lpRowSrc,<br />

WIDTHBYTES(lpBi->bmiHeader.biBitCount*rectSrc.Width())<br />

);<br />

}<br />

Cut & Copy<br />

With function CGDIDoc::CreateCopyCutDIB(…), it is much easier to implement cut and copy<br />

commands. For copy command, we need to open the clipboard, empty it, set clipboard data, and close the<br />

clipboard. For the cut command, we also need to fill the selected area with the current background color.<br />

The following shows how the cut command is implemented:<br />

void CGDIDoc::OnEditCut()<br />

{<br />

if(GetCGDIView()->OpenClipboard() == TRUE)<br />

{<br />

HGLOBAL hDIB;<br />

}<br />

}<br />

::EmptyClipboard();<br />

hDIB=CreateCopyCutDIB();<br />

ASSERT(hDIB != NULL);<br />

::SetClipboardData(CF_DIB, hDIB);<br />

::CloseClipboard();<br />

GetCGDIView()->FillSelectionWithBgdColor();<br />

UpdateAllViews(NULL);<br />

Paste<br />

Paste command is the reverse of cut or copy command: we need to obtain DIB data from the clipboard<br />

and copy it back to the bitmap image that is being edited. To let the user place pasted image everywhere,<br />

we need to implement tracker again to select the pasted image. With this implementation, the user can<br />

move or resize the image surrounded by the tracker just like using the rectangular selection tool.<br />

So instead of copying DIB data from the clipboard directly to the bitmap being edited, we can first<br />

create and copy it to the backup bitmap image (CGDIView::m_bmpSelBackup), then change the current<br />

363

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

Saved successfully!

Ooh no, something went wrong!