📄 createenhmetau.pas
字号:
unit CreateEnhMetaU;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Image1: TImage;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
{these hold important screen dimension information used when creating
the reference rectangle}
WidthInMM,
HeightInMM,
WidthInPixels,
HeightInPixels: Integer;
{holds millimeter per pixel ratios}
MMPerPixelHorz,
MMPerPixelVer: Integer;
{the reference rectangle}
ReferenceRect: TRect;
{a handle to the metafile device context}
MetafileDC: HDC;
{the handle to a metafile}
TheMetafile: HENHMETAFILE;
{a handle to a brush used in drawing on the metafile}
TheBrush: HBRUSH;
OldBrush: HBRUSH;
begin
{the CreateEnhMetaFile function assumes that the dimensions in the reference
rectangle are in terms of .01 millimeter units (i.e. a 1 equals .01
millimeters, 2 equals .02 millimeters, etc.). therefore, the following
lines are required to obtain a millimeters per pixel ratio. this can then
be used to create a reference rectangle with the appropriate dimensions.}
{retrieve the size of the screen in millimeters}
WidthInMM:=GetDeviceCaps(Form1.Canvas.Handle, HORZSIZE);
HeightInMM:=GetDeviceCaps(Form1.Canvas.Handle, VERTSIZE);
{retrieve the size of the screen in pixels}
WidthInPixels:=GetDeviceCaps(Form1.Canvas.Handle, HORZRES);
HeightInPixels:=GetDeviceCaps(Form1.Canvas.Handle, VERTRES);
{compute a millimeter per pixel ratio. the millimeter measurements must be
multiplied by 100 to get the appropriate unit measurement that the
CreateEnhMetaFile is expecting (where a 1 equals .01 millimeters)}
MMPerPixelHorz:=(WidthInMM * 100) div WidthInPixels;
MMPerPixelVer:=(HeightInMM * 100) div HeightInPixels;
{create our reference rectangle for the metafile}
ReferenceRect.Top:=0;
ReferenceRect.Left:=0;
ReferenceRect.Right:=Image1.Width * MMPerPixelHorz;
ReferenceRect.Bottom:=Image1.Height * MMPerPixelVer;
{create a metafile that will be saved to disk}
MetafileDC:=CreateEnhMetaFile(Form1.Canvas.Handle, 'Example.emf',
@ReferenceRect,
'CreateEnhMetaFile Example Program'+Chr(0)+
'Example Metafile'+Chr(0)+Chr(0));
{display some text in the metafile}
TextOut(MetafileDC,15,15,'This is an enhanced metafile.',29);
{create a diagonal hatched brush and select it into the metafile}
TheBrush:=CreateHatchBrush(HS_DIAGCROSS, clRed);
OldBrush:=SelectObject(MetafileDC, TheBrush);
{draw a filled rectangle}
Rectangle(MetafileDC, 15, 50, 250, 250);
{delete the current brush}
SelectObject(MetafileDC, OldBrush);
DeleteObject(TheBrush);
{create a horizontal hatched brush and select it into the metafile}
TheBrush:=CreateHatchBrush(HS_CROSS, clBlue);
OldBrush:=SelectObject(MetafileDC, TheBrush);
{draw a filled ellipse}
Ellipse(MetafileDC, 15, 50, 250, 250);
{delete the current brush}
SelectObject(MetafileDC, OldBrush);
DeleteObject(TheBrush);
{close the metafile, saving it to disk and retrieving a handle}
TheMetafile:=CloseEnhMetaFile(MetafileDC);
{draw the metafile into the Image1 canvas}
PlayEnhMetaFile(Image1.Canvas.Handle, TheMetafile, Image1.Canvas.Cliprect);
{we are done with the metafile, so delete its handle}
DeleteEnhMetaFile(TheMetafile);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -