用 GDI 操作 EMF 文件[8]- 绘制图元文件时改变画笔与画刷
本站寻求有缘人接手,详细了解请联系站长QQ1493399855
本例效果图:代码文件:
unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, ExtCtrls;typeTForm1 = class(TForm)ColorBox1: TColorBox;ColorBox2: TColorBox;ComboBox1: TComboBox;procedure FormCreate(Sender: TObject);procedure FormDestroy(Sender: TObject);procedure FormPaint(Sender: TObject);procedure ColorBox1Change(Sender: TObject);procedure ColorBox2Change(Sender: TObject);procedure ComboBox1Change(Sender: TObject);end;varForm1: TForm1;implementation{$R *.dfm}varHMetaFile: HENHMETAFILE; {EMF 文件句柄}p: TPen;b: TBrush;//EnumEnhMetaFile 的回调函数 function MyEnhMetaFunc(DisplaySurface: HDC; var MetafileTable: THandleTable;var MetafileRecord: TEnhMetaRecord; ObjectCount: Integer; var Data: Longint): Integer; stdcall; begincase MetafileRecord.iType ofEMR_CREATEPEN: SelectObject(DisplaySurface, p.Handle);EMR_CREATEBRUSHINDIRECT: SelectObject(DisplaySurface, b.Handle);elsePlayEnhMetaFileRecord(DisplaySurface, MetafileTable, MetafileRecord, ObjectCount);Result := 1; end; end;procedure TForm1.FormCreate(Sender: TObject); varMetaFileDC: HDC; begin{文件建立在内存}MetaFileDC := CreateEnhMetaFile(0, nil, nil, nil);p := TPen.Create;b := TBrush.Create;p.Color := clRed;b.Style := bsCross;b.Color := clSilver;SelectObject(MetaFileDC, p.Handle);SelectObject(MetaFileDC, b.Handle);MoveToEx(MetaFileDC, 0, 0, nil);LineTo(MetaFileDC, 80, 80);{矩形}Rectangle(MetaFileDC, 10, 10, 30, 50);{椭圆}Ellipse(MetaFileDC, 30, 30, 50, 70);HMetaFile := CloseEnhMetaFile(MetaFileDC);DeleteObject(MetaFileDC);ComboBox1.Items.CommaText :='bsSolid,bsClear,bsHorizontal,bsVertical,bsFDiagonal,bsBDiagonal,bsCross,bsDiagCross'; end;procedure TForm1.FormPaint(Sender: TObject); beginEnumEnhMetaFile(Canvas.Handle, HMetaFile, @MyEnhMetaFunc, nil, ClientRect); end;procedure TForm1.ColorBox1Change(Sender: TObject); beginp.Color := ColorBox1.Selected;Repaint; end;procedure TForm1.ColorBox2Change(Sender: TObject); beginb.Color := ColorBox2.Selected;Repaint; end;procedure TForm1.ComboBox1Change(Sender: TObject); beginb.Color := ColorBox2.Selected;case ComboBox1.ItemIndex of0: b.Style := bsSolid;1: b.Style := bsClear;2: b.Style := bsHorizontal;3: b.Style := bsVertical;4: b.Style := bsFDiagonal;5: b.Style := bsBDiagonal;6: b.Style := bsCross;7: b.Style := bsDiagCross; end;Repaint; end;procedure TForm1.FormDestroy(Sender: TObject); beginp.Free;b.Free;DeleteEnhMetaFile(HMetaFile); end;end.窗体文件:
object Form1: TForm1Left = 0Top = 0Caption = 'Form1'ClientHeight = 197ClientWidth = 324Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name = 'Tahoma'Font.Style = []OldCreateOrder = FalsePosition = poDesktopCenterOnCreate = FormCreateOnDestroy = FormDestroyOnPaint = FormPaintPixelsPerInch = 96TextHeight = 13object ColorBox1: TColorBoxLeft = 200Top = 16Width = 116Height = 22ItemHeight = 16TabOrder = 0OnChange = ColorBox1Changeendobject ColorBox2: TColorBoxLeft = 200Top = 44Width = 116Height = 22ItemHeight = 16TabOrder = 1OnChange = ColorBox2Changeendobject ComboBox1: TComboBoxLeft = 200Top = 72Width = 116Height = 21ItemHeight = 13TabOrder = 2Text = 'ComboBox1'OnChange = ComboBox1Changeend end