// Original: I don't know (any other foreigner)
// Translator: Jia Xubin, Optoelectronic Engineering, Chongqing University
/ / Welcome everyone to criticize, thank you!
/ / How to copy bitmap into the clipboard
Copying the bitmap into the clipboard is relatively simple, but you can don't forget, if you have a palette, you have to copy the palette together, otherwise you can't correct it. shown.
Function 1: Copy a DDB to the clipboard
The CopybitmaptoClipboard () function can copy a DDB to the clipboard, if there is a palette it, it is copied together. Finally, pay attention to call the Detach () function. This is very important because the GDI (Graphic Device Interface) object has been transferred to the clipboard.
// copybitmaptoclipboard - copy DDB to the clipboard
// PWND - Open the pointer to the clipboard window
// bitmap -ddb
// ppal - the pointer of the logical palette, it may be empty
// Note - After the bitmap is copied, Bitmap and PPAL owned by the clipboard, it should be separated
//
Void CopyBitmaptoclipboard (Const CWnd * PWND, 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 a DIB to the clipboard
Functions CopyDibtoClipboard () is very similar to the function copybitmaptoclipboard (), and the memory handle should include BitmapInfo and Bitmap bits, you can use the function GlobalAlloc () to be implemented. Once the DIB is copied into the clipboard, the memory handle will be owned by the clipboard, and will not be released by your application.
// CopyDibtoclipboard - copy a DIB to the clipboard
// PWND - Pointer with clipboard
// HDIB - contains BitmapInfo and Bitmap bits memory handle
// ppal - logical palette pointer, may be empty
// Note - After the bitmap is copied, Bitmap and PPAL owned by the clipboard, it should be separated
//
Void CopyDibtoclipboard (Const CWND * PWND, HGLOBAL HDIB, CPALETTTTE * 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 an image to the clipboard
The function COPYWNDTOCLIPBOARD () function is to copy an image to the clipboard
Void CopyWndToclipboard (CWND * PWND) {
CBitmap Bitmap;
CClientDC DC (PWND);
CDC MEMDC;
CRECT RECT;
Memdc.createCompatibleDC (& DC);
PWND-> getWindowRect (RECT);
Bitmap.createCompatibleBitmap (& DC, Rect.width (), Rect.Height ());
CBitMap * PoldbitMap = MEMDC.SELECTOBJECT (& Bitmap);
MEMDC.BITBLT (0, 0, Rect.width (), Rect.Height (), & DC, 0, 0, SRCCOPY;
PWND-> OpenClipboard ();
EMPTYCLIPBOARD ();
SetClipboardData (cf_bitmap, bitmap.getsafehandle ());
CloseClipboard ();
MEMDC.SELECTOBJECT (POLDBITMAP);
Bitmap.detach ();
}