Place a bitmap image on the clipboard (uses OLE)

(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;
	
}



Comments

  • I'm want source code c++ for bitmap graphic

    Posted by Legacy on 12/25/2003 12:00am

    Originally posted by: Wichit

    I'm want source code c++ and theory for open bitmap file graphic and copy cut pase on clipboard and select picture is layer
    

    Reply
  • Initialization failed

    Posted by Legacy on 06/09/2001 12:00am

    Originally posted by: Biju Abraham

    when I creating the clip board it diplays an error. colinitialize failed.what is the reson for this thing.
    please reply me
    Biju

    Reply
  • Bug fix for Access violation

    Posted by Legacy on 07/24/2000 12:00am

    Originally posted by: Mahantesh

    There is one problem with this code, This is visible when the function is called second time, at SetClipBoard() function. When this functions is called second time you can see a "first chance exception -Access violation" warning in the debug window, This also causes crash in the release version. Here is the fix for the problem.

    COLORREF BACKGROUND_COLOR = RGB(255,255,255);
    tagSTGMEDIUM * data;
    FORMATETC fe = {
    CF_BITMAP,NULL,DVASPECT_CONTENT,-1,TYMED_GDI
    };
    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->DeleteObject();
    junk->CreateCompatibleBitmap(&cdc,client.Width(),client.Height());
    CBitmap* pOld = dc.SelectObject(junk);
    CBrush fill(BACKGROUND_COLOR);
    dc.FillRect(client,&fill);

    //replace this with something that draws to a CDC
    OnDraw(&dc);
    dc.SelectObject(pOld);

    fe.cfFormat = CF_BITMAP;
    fe.ptd = NULL;
    fe.dwAspect = DVASPECT_CONTENT;
    fe.lindex = -1;
    fe.tymed = TYMED_GDI;
    data->tymed = TYMED_GDI;
    data->hBitmap = (HBITMAP)junk->m_hObject;// HBITMAP(*junk);
    data->pUnkForRelease = NULL;
    pData->CacheData( CF_BITMAP, data, &fe);
    pData->SetClipboard();
    delete data;
    delete junk;

    Reply
  • Memory leak

    Posted by Legacy on 05/12/2000 12:00am

    Originally posted by: Brent Mantooth

    As stated earlier you must delete the junk variable,but does this cause a memory leak? Is it taken care of elsewhere or does this just eat memory every time you copy stuff?

    Reply
  • How to retrieve from clipboard?

    Posted by Legacy on 02/16/2000 12:00am

    Originally posted by: Lawrence Tay

    hi, the editcopy works well, but how do i paste the clipboard bmp?

    Reply
  • Is it correct?

    Posted by Legacy on 11/17/1999 12:00am

    Originally posted by: Julius Noche

    With the previous comments stated. It seems that the code may have lots of bugs. Could anyone provide me with the correct code?

    Reply
  • Doesn't the Bitmap data need to be in Global memory?

    Posted by Legacy on 05/08/1999 12:00am

    Originally posted by: James Skinner


    Doesn't the memory the bitmap is in need
    to have allocated with GlobalAlloc() ?

    Reply
  • bugfix:must remove "delete junk;" from codes

    Posted by Legacy on 03/31/1999 12:00am

    Originally posted by: kevin

    bugfix19990331:
    
    you MUST remove "delete junk;" from above codes
    or the owner of clipboard can NOT get bitmap from it

    Reply
  • How can diable one item after I click on it?

    Posted by Legacy on 12/07/1998 12:00am

    Originally posted by: Xiaoliang Chen

    I still do not how to disable a item after I click on it or on
    special situation.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • When it comes to desktops – physical or virtual – it's all about the applications. Cloud-hosted virtual desktops are growing fast because you get local data center-class security and 24x7 access with the complete personalization and flexibility of your own desktop. Organizations make five common mistakes when it comes to planning and implementing their application management strategy. This eBook tells you what they are and how to avoid them, and offers real-life case studies on customers who didn't let …

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds