TAChart: Implement a windows-only solution to copying a chart to the clipboard as windows metafile. Update wmf demo. (see: http://forum.lazarus.freepascal.org/index.php/topic,25985.msg158945/topicseen.html#new)

git-svn-id: trunk@46396 -
This commit is contained in:
wp 2014-10-01 21:13:10 +00:00
parent 368121fe76
commit e8a71eb006
5 changed files with 54 additions and 16 deletions

View File

@ -1,13 +1,13 @@
object Form1: TForm1
Left = 1317
Left = 896
Height = 330
Top = 168
Top = 186
Width = 403
Caption = 'Form1'
ClientHeight = 330
ClientWidth = 403
Position = poScreenCenter
LCLVersion = '0.9.31'
LCLVersion = '1.3'
object Chart1: TChart
Left = 0
Height = 292
@ -15,10 +15,12 @@ object Form1: TForm1
Width = 403
AxisList = <
item
Minors = <>
Title.LabelFont.Orientation = 900
end
item
Alignment = calBottom
Minors = <>
end>
Depth = 30
Foot.Brush.Color = clBtnFace
@ -55,11 +57,20 @@ object Form1: TForm1
Left = 8
Height = 25
Top = 8
Width = 88
Width = 104
Caption = 'Save To WMF'
OnClick = btnSaveToMetafileClick
TabOrder = 0
end
object btnCopyToClipboard: TButton
Left = 120
Height = 25
Top = 8
Width = 139
Caption = 'Copy to clipboard'
OnClick = btnCopyToClipboardClick
TabOrder = 1
end
end
object RandomChartSource1: TRandomChartSource
PointsNumber = 20

View File

@ -14,12 +14,14 @@ type
TForm1 = class(TForm)
btnSaveToMetafile: TButton;
btnCopyToClipboard: TButton;
Chart1: TChart;
Chart1BarSeries1: TBarSeries;
Chart1LineSeries1: TLineSeries;
Panel1: TPanel;
RandomChartSource1: TRandomChartSource;
procedure btnSaveToMetafileClick(Sender: TObject);
procedure btnCopyToClipboardClick(Sender: TObject);
end;
var
@ -40,5 +42,13 @@ begin
Draw(TWindowsMetafileDrawer.Create('test.wmf'), Rect(0, 0, Width, Height));
end;
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 as a windows metafile.
end;
end.

View File

@ -1,4 +1,4 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="9"/>
@ -48,19 +48,19 @@
<Unit0>
<Filename Value="wmfdemo.lpr"/>
<IsPartOfProject Value="True"/>
<UnitName Value="wmfdemo"/>
</Unit0>
<Unit1>
<Filename Value="main.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="Form1"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="Main"/>
</Unit1>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="10"/>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="wmfdemo"/>
@ -76,12 +76,6 @@
</Win32>
</Options>
</Linking>
<Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">

View File

@ -7,7 +7,7 @@ uses
cthreads,
{$ENDIF}{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, Main, tachartlazaruspkg, tachartwmf
Forms, Main, tachartlazaruspkg
{ you can add units after this };
{$R *.res}

View File

@ -52,6 +52,7 @@ type
public
procedure Assign(ASource: TPersistent); override;
procedure Clear; override;
procedure CopyToClipboard;
procedure LoadFromFile(const AFileName: String); override;
procedure LoadFromStream(AStream: TStream); override;
function ReleaseHandle: HENHMETAFILE;
@ -98,7 +99,7 @@ type
implementation
uses
SysUtils, TAChartUtils;
SysUtils, clipbrd, TAChartUtils;
{ TWindowsMetafileDrawer }
@ -129,7 +130,11 @@ end;
procedure TWindowsMetafileDrawer.DrawingEnd;
begin
FreeAndNil(FCanvas);
FMetafile.SaveToFile(FFileName);
if FFileName = '' then
// Clipboard.Assign(FMetaFile)
FMetafile.CopyToClipboard
else
FMetafile.SaveToFile(FFileName);
end;
function TWindowsMetafileDrawer.GetCanvas: TCanvas;
@ -329,6 +334,24 @@ begin
DeleteImage;
end;
procedure TMetafile.CopyToClipboard;
// http://www.olivierlanglois.net/metafile-clipboard.html
var
Format: Word;
Data: THandle;
begin
if FImageHandle = 0 then exit;
OpenClipboard(0);
try
EmptyClipboard;
Format := CF_ENHMETAFILE;
SetClipboardData(Format, FImageHandle); //Data);
finally
CloseClipboard;
end;
end;
procedure TMetafile.LoadFromFile(const AFileName: String);
begin
Unused(AFileName);