Solved

Metafile to clipboard, then read

Posted on 1998-06-17
7
1,462 Views
Last Modified: 2013-12-03
I am trying to draw my app to clipboard as metafile records,
then let other apps to paste what's on the clipboard.
I am quite sure the data is writen to clipboard, but I could
not get it from clipboard.

I will list sending clipboard code first, then retrive part.
The other question is does the application who retrieve the
clipboard will deallocating the resource ? There is no
documentation on this issue.

When I write METAFILE to file (enhanced metafile format),
I can retrieve without any missing. But when I write metafile with windows metafile format (for Win3.x) and I
retrieve with corresponding old format function, then I missing all the drawing (lines), only flood fill and text.

Under both formats, I could not retrieve from clipboard.
Could somebody help me and notify me with email when you
give an answer.

gaohong@electro.psi.vcu.edu


Write to clipboard part

void writeToClipboard()
   {
    HDC hDCMeta;
    HENHMETAFILE   hMeta;
    //HMETAFILE   hMeta;

    TRect rect = VClientRect;
    rect.left *= 0;
    rect.top  *= 0;
    rect.right *= 100*200;
    rect.bottom *= 100*150;

  //Create the metafile with output going to the disk.

    //hDCMeta = CreateEnhMetaFile(DrawAttr->CurrentDC, "sample.emf", 0, 0);
    hDCMeta = CreateEnhMetaFile(DrawAttr->CurrentDC, 0, 0, 0);
    //hDCMeta = CreateMetaFile("sample.wmf");
    DrawAttr->SetAttribute(hDCMeta);
    DrawData->draw(DrawData->getDrawBound(0), *DrawAttr, 1);
    hMeta = CloseEnhMetaFile(hDCMeta);
    //hMeta = CloseMetaFile(hDCMeta);

    HGLOBAL hmfp = ::GlobalAlloc(GHND, sizeof(METAFILEPICT));
    if(hmfp)
      {
  METAFILEPICT far *mfp = (METAFILEPICT far *) GlobalLock(hmfp);

       mfp->mm   = MM_ANISOTROPIC;
       //mfp->xExt = 1;
       //mfp->yExt = 1;
       mfp->hMF  = (HMETAFILE) hMeta;
       ::GlobalUnlock(hmfp);

       if(::OpenClipboard(HWindow))
         {
          ::EmptyClipboard();
          hmfp = ::SetClipboardData(CF_METAFILEPICT, hmfp);
         if(hmfp)
            {
              ::MessageBox(NULL, "clipboard data set OK", "Good", MB_OK);
              enablePaste = 1;
            }
         else
            ::MessageBox(NULL, "clipboard data not set", "warning", MB_OK);
          ::CloseClipboard();
         }
      }
  }



My test Retrieve clipboard part

void  getClipboardData()
  {
 if(::OpenClipboard(HWindow))
   {
  HGLOBAL hmfp = ::GetClipboardData(CF_ENHMETAFILE);
//*****  the failure is hmfp is NULL *******
  METAFILEPICT far* mfp = (METAFILEPICT far*)::GlobalLock(hmfp);

  if (mfp) {
    //Mm = mfp->mm;
    //Extent = TSize(mfp->xExt, mfp->yExt);
    Handle = (HENHMETAFILE) mfp->hMF;
    ::GlobalUnlock(hmfp);
    }
    ::CloseClipboard();
  }
  else
    {
    Handle = 0;
    ::MessageBox(NULL, "warning", "No metafile in clipboard", MB_OK);
    }
    // Force redraw
    //
    Invalidate();
  //}
}
0
Comment
Question by:gaohong
  • 4
  • 3
7 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1408099
On a whim I decided to check back on this and I noticed something.  You set the clipboard data as MATAFILEPICT but try to obtain it back as ENHMETAFILE.  Does that fix the problem?
0
 
LVL 3

Author Comment

by:gaohong
ID: 1408100
Nietod:

I have tried set clipboard data as CF_ENHMETAFILE, then the return value tells me that it is not set. Only CF_METAFILEPICT
can be set to clipboard.

On get clipboard data, both CF_ENHMETAFILE and CF_METAFILEPICT
returns NULL handle.

If you look the documentation of CF format, it looks like there
is some conversion.  However, I suspect MS did messed up here
some how.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1408101
Not that this explains why you can't retrieve the data from the clipboard, but when you set the data using CF_ENHMETAFILE I suspect you are supposed to specify the handle to the metafile directly, that is, you don't specify a METAFILEPICT structure.  That may explain why you couldn't set the clipboard data.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 3

Author Comment

by:gaohong
ID: 1408102
In fact, you can not set clipboard data as CF_ENHMETAFILE. It
would not set.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1408103
Did you try that specifying a handle to an enhanced meta file, rather than specifying a handle to  a METAFILEPICT?  For example,

          hmfp = ::SetClipboardData(CF_ENHMETAFILE, hMeta);

instead of

          hmfp = ::SetClipboardData(CF_ENHMETAFILE, hmfp);
0
 
LVL 3

Author Comment

by:gaohong
ID: 1408104
Beatiful. It works. This way also removes the problem who takes
care of GlobalLocked handle, no such problem then.

Nice exchange with you Nietod. Could you mark the answer to get
grade ?
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1408105
This should fix the SetClipboardData().  But does it fix the GetClipboardData() problem?  make sure this works totally before accepting my answer.  I am ussually happy to help out after an answer has been accepted, but this is an area where I probably can't help out (I was lucky so far).
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question