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

drawing tool to rectangular selection (if it is not). By doing this, everything will go on as if we just selected<br />

a portion of image using the rectangular selection tool.<br />

When the user executes Edit | Paste command, function CGDIDoc::OnEditPaste()will be called. The<br />

DIB data in the clipboard will be replicated and passed to function CGDIView::PasteDIB(…). Within the<br />

function, a new bitmap will be created using variable m_bmpSelBackup, and the DIB data contained in the<br />

clipboard will be copied to it. Please note we must replicate the clipboard data instead of using it directly<br />

because the data may be used by other applications later. The following is the implementation of function<br />

CGDIDoc::OnEditPaste():<br />

void CGDIDoc::OnEditPaste()<br />

{<br />

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

{<br />

HGLOBAL hDIB;<br />

LPBYTE lpByte;<br />

HGLOBAL hDIBPaste;<br />

LPBYTE lpBytePaste;<br />

}<br />

}<br />

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

hDIB=::GetClipboardData(CF_DIB);<br />

ASSERT(hDIB);<br />

hDIBPaste=::GlobalAlloc(GHND, ::GlobalSize(hDIB));<br />

ASSERT(hDIBPaste);<br />

lpByte=(LPBYTE)::GlobalLock(hDIB);<br />

ASSERT(lpByte);<br />

lpBytePaste=(LPBYTE)::GlobalLock(hDIBPaste);<br />

ASSERT(lpBytePaste);<br />

memcpy(lpBytePaste, lpByte, ::GlobalSize(hDIB));<br />

::GlobalUnlock(hDIB);<br />

::GlobalUnlock(hDIBPaste);<br />

::CloseClipboard();<br />

GetCGDIView()->PasteDIB(hDIB);<br />

::GlobalFree(hDIBPaste);<br />

The following portion of function CGDIView::PasteDIB(…) shows how to copy the clipboard DIB data<br />

to the backup DIB image by calling function ::SetDIBits(…) (the clipboard data is passed through<br />

parameter hData):<br />

void CGDIView::PasteDIB(HGLOBAL hData)<br />

{<br />

CClientDC dc(this);<br />

LPBITMAPINFO lpBi;<br />

int nRatio;<br />

CGDIDoc *pDoc;<br />

pDoc=GetDocument();<br />

ASSERT_VALID(pDoc);<br />

AfxGetApp()->DoWaitCursor(TRUE);<br />

lpBi=(LPBITMAPINFO)::GlobalLock(hData);<br />

ASSERT(lpBi);<br />

if(lpBi->bmiHeader.biBitCount != 8)<br />

{<br />

::GlobalUnlock(hData);<br />

return;<br />

}<br />

nRatio=pDoc->GetRatio();<br />

if(m_bmpSelBackup.GetSafeHandle() != NULL)m_bmpSelBackup.DeleteObject();<br />

m_bmpSelBackup.CreateCompatibleBitmap<br />

(<br />

&dc,<br />

lpBi->bmiHeader.biWidth,<br />

lpBi->bmiHeader.biHeight<br />

364

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

Saved successfully!

Ooh no, something went wrong!