Copying a bitmap to the clipboard is quite simple. Remember that if the bitmap needs a color palette, you should also copy the palette.
Function 1: copy the device-related bitmap to the clipboard.
The CopyBitmapToClipboard () function copies a DDB bitmap to the clipboard. If a color palette is provided, it copies the color palette at the same time.
Note thatDetach (). This is important, because at this time the GDI object owner has been transferred to the clipboard.
// CopyBitmapToClipboard-Copies a device-dependent bitmap to clipboard
// PWnd-Pointer to window that opens the clipboard
// Bitmap-The device-dependent bitmap
// PPal-Pointer to logical palette-Can be NULL
// NOTE-GDI objects are detached from bitmap & pPal
//As the clipboard owns them after the copy
Void CopyBitmapToClipboard (const CWnd * pWnd, CBitmap & bitmap, CPalette * pPal)
{
: OpenClipboard (pWnd-> GetSafeHwnd ());
: EmptyClipboard ();
If (pPal)
: SetClipboardData (CF_PALETTE, pPal-> GetSafeHandle ());
: SetClipboardData (CF_BITMAP, bitmap. GetSafeHandle ());
: CloseClipboard ();
Bitmap. Detach ();
If (pPal)
PPal-> Detach ();
}
Function 2: copy the device-independent bitmap to the clipboard.
The CopyDIBToClipboard () function is very similar to the CopyBitmapToClipboard () function. The memory handle contains BITMAPINFO and bitmap bit information, which is allocated through GlobalAlloc.
// CopyDIBToClipboard-Copies a device-dependent bitmap to clipboard
// PWnd-Pointer to window that opens the clipboard
// HDIB-Memory handle that contains BITMAPINFO & bitmap bits
// PPal-Pointer to logical palette-Can be NULL
// NOTE-GDI objects are detached from bitmap & pPal
//As the clipboard owns them after the copy
Void CopyDIBToClipboard (const CWnd * pWnd, HGLOBAL hDIB, CPalette * pPal)
{
: OpenClipboard (pWnd-> GetSafeHwnd ());
: EmptyClipboard ();
If (pPal)
: SetClipboardData (CF_PALETTE, pPal-> GetSafeHandle ());
: SetClipboardData (CF_DIB, hDIB );
: CloseClipboard ();
Bitmap. Detach ();
If (pPal)
PPal-> Detach ();
}
Function 3: copy one imageWindows Image to clipboard
Void CopyWndToClipboard (CWnd * pWnd)
{
CBitmapBitmap;
CClientDCDc (pWnd );
CDC&