Place a bitmap image on the clipboard (uses OLE)

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

(1) If you don’t have OLE in your project already then add this

#include <afxole.h>

and do the following during initialization

OleInitialize(NULL);

(2) The following places a bitmap copy of a view on the clipboard. It can be easily modified to work with any CWnd that draws to a CDC.

void CMyView::OnEditCopy()
{

	COLORREF BACKGROUND_COLOR = RGB(255,255,255);
	tagSTGMEDIUM * data;
	CBitmap * junk;
	COleDataSource* pData = new COleDataSource;
	data = new tagSTGMEDIUM;
	junk = new CBitmap();
	CClientDC cdc(this);
	CDC dc;
	dc.CreateCompatibleDC(&cdc);
	CRect client;
	//replace this with something that calculates 
	//the proper rectangle size
	GetClientRect(client);
	junk->CreateCompatibleBitmap(&cdc,client.Width(),client.Height());
	dc.SelectObject(junk);
	CBrush fill(BACKGROUND_COLOR);
	dc.FillRect(client,&fill);
	//replace this with something that draws to a CDC
	OnDraw(&dc);
	data->tymed = TYMED_GDI;
	data->hBitmap = HBITMAP(*junk);
	pData->CacheData( CF_BITMAP, data );
	pData->SetClipboard();
	delete data;
	delete junk;

}

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read