Recent

Author Topic: Copy metafiles to clipboard?  (Read 4777 times)

ironphil

  • Jr. Member
  • **
  • Posts: 56
Copy metafiles to clipboard?
« on: September 30, 2014, 04:04:51 am »
Now that the metafile format is right, would that be difficult to have something like the existing procedure "Chart1.CopyToClipboardBitmap" but for metafiles? Something like "Chart1.CopyToClipboardMetafile". Even if it is specific to Windows.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Copy metafiles to clipboard?
« Reply #1 on: September 30, 2014, 08:35:27 pm »
Metafile are not supported offcially by Lazarus/FPC. What we have in in TAChart in a windows-only workaround for this package without full integration in the graphics infrastructure. On the other hand from what I saw with the other image formats today, I get the feeling that it should be possible to add clipboard support for meta files, at least for windows. Give me some time and some quite moments to understand how the clipboard formats work.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Copy metafiles to clipboard?
« Reply #2 on: October 01, 2014, 12:27:51 pm »
I found a solution - as always when you know it, it was not that difficult.

Before adding it to trunk I'd ask you to test it. Please copy the attached TADrawerCanvas unit over the original one (it is in (lazarus)\components\tachart); you may want to make a copy of the original one in case you want to switch back again. Recompile the TAChart package. That should be all.

In order to copy a chart to the clipboard as a metafile use the same code as for saving to file, but leave the file name empty:

Code: [Select]
procedure TForm1.btnCopyToClipboardClick(Sender: TObject);
begin
  with Chart1 do
    Draw(TWindowsMetafileDrawer.Create(''), Rect(0, 0, Width, Height));
    // Setting the file name to an empty string results in copying the chart to
    // the clipboard.
end;

Not really straightforward, but I cannot add a CopyToClipboardMetafile method (like CopyToClipboardBitmap) to TChart because the wmf stuff is in a separate package (for good reasons...)

Note that this code runs only under Windows.

ironphil

  • Jr. Member
  • **
  • Posts: 56
Re: Copy metafiles to clipboard?
« Reply #3 on: October 01, 2014, 10:31:49 pm »
It looks perfect to me. I tested it with my own project just like you instructed and it just worked as expected. Many thanks.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Copy metafiles to clipboard?
« Reply #4 on: October 01, 2014, 11:31:10 pm »
It's in trunk, r48396. The wmfdemo contains a button "Copy to clipboard" now. See also the wiki http://wiki.lazarus.freepascal.org/TAChart_Demos#wmf

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Copy metafiles to clipboard?
« Reply #5 on: October 02, 2014, 05:01:46 pm »
Quote
I cannot add a CopyToClipboardMetafile method (like CopyToClipboardBitmap) to TChart because the wmf stuff is in a separate package
But I did not think of the magnificent class helpers! Today I added "CopyToClipboardMetaFile" and "SaveAsMethod" to TChart by using a class helper implemented in the wmf unit (--> r46404). These identifiers are much easier to remember. Just have a look at the wmfdemo which shows the new syntax now:

Code: [Select]
  // copy a chart to clipboard as a windows metafile
  Chart.CopyToClipboardMetafile;

  // save a chart to a windows metafile
  Chart.SaveAsWMF('c:\temp\chart.wmf');

 

TinyPortal © 2005-2018