Copying a Bitmap to the Clipboard with MFCLast reviewed: July 18, 1997Article ID: Q112530 |
7.00 | 1.00 1.50
MS-DOS | WINDOWS
kbprg
The information in this article applies to:
The Microsoft Foundation Classes (MFC) included with: - Microsoft C/C++ version 7.0 - Microsoft Visual C++ for Windows, versions 1.0 and 1.5
SUMMARYIn order to pass a handle to a Bitmap to the clipboard, you have to get it from the GetSafeHandle method and then "detach" it from the objects that are using it so the clipboard has the only handle.
MORE INFORMATIONHere is a sample application that can be used to copy a bitmap from the client window to the clipboard. The easiest way to implement this is to follow these steps:
Sample Code
void CClipmfcView::OnEditCopy(){ // TODO: Add your command handler code here CBitmap cBmp; CClientDC cWndDC(this); // View is an hWnd, so we can use "this" CDC cMemDC; // Handle to a memory DC CRect rect; // For storing the size of the window cMemDC.CreateCompatibleDC(&cWndDC); // Create the memory DC. GetWindowRect(rect); // Get the size of the window // Here we are going to do a little drawing so we can see a line. cWndDC.MoveTo(0,0); cWndDC.LineTo(rect.Width(),rect.Height()); cBmp.CreateCompatibleBitmap(&cMemDC, rect.Width(),rect.Height() ); // Keep the old bitmap CBitmap* pOldBitmap = cMemDC.SelectObject(&cBmp); cMemDC.BitBlt(0, 0, rect.Width(),rect.Height(), &cWndDC, 0, 0,SRCCOPY);
// here are the actual clipboard functions. AfxGetApp()->m_pMainWnd->OpenClipboard() ; EmptyClipboard() ; SetClipboardData (CF_BITMAP, cBmp.GetSafeHandle() ) ; CloseClipboard () ; // next we select the old bitmap back into the memory DC // so that our bitmap is not deleted when cMemDC is destroyed. // Then we detach the bitmap handle from the cBmp object so that // the bitmap is not deleted when cBmp is destroyed. cMemDC.SelectObject(pOldBitmap); cBmp.Detach(); return;}
|
Additional reference words: kbinf 7.00 1.00 1.50 2.00 2.50
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |