Place a enhanced metafile 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 metafile copy of a view on the clipboard. It can be easily modified to work with any CWnd that draws to a CDC.

void CMyView::OnEditCopyWmf() 
{ 
	tagSTGMEDIUM * data;
	CClientDC dcRef( this );
	data = new tagSTGMEDIUM;
	CMetaFileDC * dcMeta;
	dcMeta = new CMetaFileDC();
	COleDataSource* pData = new COleDataSource;
	CRect lmbr;
	GetClientRect(lmbr);
	//replace "ImageName" with a description of this image
	dcMeta->CreateEnhanced(&dcRef, NULL, NULL,"ImageName\0Copy\0\0" );
	dcMeta->SetMapMode(MM_TEXT);
	//actually draw to the device context
	OnDraw(dcMeta);
	//done drawing
	data->tymed = TYMED_ENHMF;
	data->hEnhMetaFile = dcMeta->CloseEnhanced();
	pData->CacheData( CF_ENHMETAFILE, data );
	pData->SetClipboard();
	delete dcMeta;
	delete data;
}



Comments

  • How to send the text in a dialog box to clipboard

    Posted by Legacy on 10/24/2002 12:00am

    Originally posted by: Vidya Rajkumar

    I have some 4 edit boxes in a dialog.

    For eg.

    Name Vidya
    Age 23
    Sex Female
    occupation S/W Engineer


    Assume that these are the controls in a single dialog box.

    What i want is,on just pressing a button in toolbar,the data has to be sent to the clipboard ..Note that the nothing is selected or focussed.

    The data in the clipboard should be in the below format
    Name Vidya
    Age 23
    Sex Female
    occupation S/W Engineer


    How is it?

    Reply
  • oops

    Posted by Legacy on 10/21/2002 12:00am

    Originally posted by: W. J. Heitler

    Sorry, I've just noticed that the SetAtribDC modification was incorported in the earlier comment.
    It's not immediately obvious how to withdraw a comment, but if someone tells me how I'll pull this one and the previous.

    Reply
  • reading attributes like text height etc

    Posted by Legacy on 10/21/2002 12:00am

    Originally posted by: W. J. Heitler

    Thank you for your code, which is very useful. When I first tried it I had crash problems, because in my standard screen drawing routine I was reading attributes in order to draw text. The following modification seems to have sorted it.
    
    

    dcMeta->SetAttribDC(dcRef.m_hAttribDC);
    OnDraw(dcMeta);
    //done drawing
    dcMeta->ReleaseAttribDC(); // not sure if needed

    I hope this comment may be helpful.

    Reply
  • Adapted solution with correct rectangle dimensions and some simplification

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

    Originally posted by: Wim

    void CMyView::OnEditCopy()
    
    {
    // Create an OLE data source on the heap
    COleDataSource *pData = new COleDataSource;
    tagSTGMEDIUM data;
    CClientDC dcRef( this );
    CMetaFileDC dcMeta;
    RECT rect;
    int iWidthMM, iHeightMM, iWidthPels, iHeightPels;

    // Determine the picture frame dimensions.
    // iWidthMM is the display width in millimeters.
    // iHeightMM is the display height in millimeters.
    // iWidthPels is the display width in pixels.
    // iHeightPels is the display height in pixels
    iWidthMM = dcRef.GetDeviceCaps(HORZSIZE);
    iHeightMM = dcRef.GetDeviceCaps(VERTSIZE);
    iWidthPels = dcRef.GetDeviceCaps(HORZRES);
    iHeightPels = dcRef.GetDeviceCaps(VERTRES);

    // Retrieve the coordinates of the client
    // rectangle, in pixels.
    GetClientRect( &rect );

    // Convert client coordinates to .01-mm units.
    // Use iWidthMM, iWidthPels, iHeightMM, and
    // iHeightPels to determine the number of
    // .01-millimeter units per pixel in the x-
    // and y-directions.
    rect.left = (rect.left * iWidthMM * 100)/iWidthPels;
    rect.top = (rect.top * iHeightMM * 100)/iHeightPels;
    rect.right = (rect.right * iWidthMM * 100)/iWidthPels;
    rect.bottom = (rect.bottom * iHeightMM * 100)/iHeightPels;

    // Replace "ImageName" with a description of this image
    dcMeta.CreateEnhanced( &dcRef, NULL, &rect,"ImageName\0Copy\0\0" );
    dcMeta.SetMapMode( MM_TEXT );
    dcMeta.SetAttribDC( dcRef );

    // Actually draw to the device context
    OnDraw( &dcMeta );
    // Done drawing

    data.tymed = TYMED_ENHMF;
    data.hEnhMetaFile = dcMeta.CloseEnhanced();

    pData->CacheData( CF_ENHMETAFILE, &data );
    pData->SetClipboard();
    }

    Reply
  • Images do not render as expected when pasted to MS Word or MS PowerPoint

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

    Originally posted by: John W. Blakely

         Thank you for the code sample.  I used it in a very simple test application.  Within that application, I draw a box and then write "Hello, World."  However, when I cut that image and paste it into an MS Powerpoint or an MS Word document, the results are not as drawn.  In both cases, the box is visible, but test is not.  Within MS Powerpoint, I can ungroup the image and recover the tst.  The rectangle is drawn as a series of lines and I used MM_TEXT mode to draw the image.
    
    

    Some questions:
    1. Is there an agreed map mdoe that is used to cut and past objects?
    2. Is it better to use TextOut or DrawText functions?
    3. What can I do to correctly cut and paste images?

    The nest few lines contain the code I added:
    dcMeta->SetMapMode(MM_TEXT);
    //actually draw to the device context
    dcMeta->MoveTo(0, 0);
    dcMeta->LineTo(0, 20000);
    dcMeta->LineTo(20000, 20000);
    dcMeta->LineTo(20000, 0);
    dcMeta->LineTo(0, 0);
    dcMeta->TextOut(10000, 10000, "Hello, World.");
    //done drawing

    Reply
  • How do I edit enhance metafile from clipboard ?

    Posted by Legacy on 08/21/2001 12:00am

    Originally posted by: Sapto Priyono

    I have a enhance metafile which is copied from the shape of Power Point object, but after I play with PlayEnhMetaFile, this enhmetafile has uncorrect displaying (fontsize is not correct, appear bullet in the first character). Why and how do I edit this enhmetafile ?. I have edited it's enhmetaheader, but no influence for the displaying.

    Reply
  • How one can render metafile image from dialog control?

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

    Originally posted by: Michael

    How one can render metafile image from dialog control?
    
    Like List Conrol, for instance.

    Reply
  • duplicating Alt+PrtScrn

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

    Originally posted by: P. McLaughlin

    I cut this code, added it as shown below.  Builds & runs fine...but what I'm looking to do is essentially duplicate the Alt+PrtScrn functionality within my code.  It seems to me that the View's OnDraw(pDC) function needs to do this, but how?  Any ideas?
    
    

    Thanks,
    Patrick

    Reply
  • How Can I use it For CFrameWnd Derieved Classes

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

    Originally posted by: A.Qudus Abbasi

    Its really well written article but the problem is that 
    
    How Can I use it For CFrameWnd Derieved Classes?

    I had one option which is:

    When one is using CFrameWnd derieved classes then the hAttribute of the CMataFileDC object becomes null because it provides facility of text drawing only and hAttribute is necessary for Graphics drawings like Pens and brushes. So using CFrameWnd derieved classes, one must assign the hAttributes of main DC to CMetaFileDC object by this statement

    dcMeta->SetAttribDC(dcRef->m_hAttribDC); //dcRef is DC of your CFrameWnd class.

    right after the statement (you can't call it before this statement)
    dcMeta->CreateEnhanced(dcRef, NULL, NULL,"MyApp" );

    and at the end of the function before deleting CMetaFileDC object release these attributes by
    dcMeta->ReleaseAttribDC();

    If any one has other options please let me know too.
    Thanks

    A.Qudus Abbasi
    Lahore Pakistan

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

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date