place a bitmap image on the clipboard


this article was contributed by randy more.
see also copying a bitmap to clipboard

(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 =../di2001.jpg5,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../di2001.jpgd_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;
	
}