欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WinAPI: ExtCreateRegion - 区域变换

程序员文章站 2022-05-30 09:22:02
...
相似函数: SetWorldTransform

本例效果图:

WinAPI: ExtCreateRegion - 区域变换

代码文件:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    RadioGroup1: TRadioGroup;
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  cvs: TCanvas;
  Rgn: HRGN;

procedure TForm1.FormCreate(Sender: TObject);
begin
  cvs := TCanvas.Create;
  cvs.Handle := GetDC(Handle);

  RadioGroup1.Items.CommaText :=
  '原始,横向移动,纵向移动,横向比例,纵向比例,放大,缩小,水平镜像,垂直镜像,横向斜切,纵向斜切,旋转';
  RadioGroup1.ItemIndex := 0;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  DeleteObject(Rgn);
  cvs.Free;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  {绘制}
  cvs.Brush.Color := clSilver;
  cvs.Brush.Style := bsDiagCross;
  FillRgn(cvs.Handle, Rgn, cvs.Brush.Handle);
  cvs.Brush.Color := clRed;
  cvs.Brush.Style := bsSolid;
  FrameRgn(cvs.Handle, Rgn, cvs.Brush.Handle, 2, 2);
end;


procedure TForm1.RadioGroup1Click(Sender: TObject);
var
  RgnDataSize: Integer; {区域数据缓冲区的大小}
  RgnData: PRgnData;    {区域数据指针}
  TXF: TXForm;          {坐标转换的矩阵结构}
begin
  cvs.Font.Size := 120;
  cvs.Font.Style := [fsBold];
  SetBkMode(cvs.Handle, TRANSPARENT);

  {建立路径}
  BeginPath(cvs.Handle);
  cvs.TextOut(100, 0, 'F');
  EndPath(cvs.Handle);

  {将路径转换为区域}
  Rgn := PathToRegion(cvs.Handle);

  {获取区域数据}
  RgnDataSize := GetRegionData(Rgn, 0, nil);
  GetMem(RgnData, RgnDataSize);
  GetRegionData(Rgn, RgnDataSize, RgnData);

  case RadioGroup1.ItemIndex of
    0: begin {原始效果, 无变换}
      TXF.eM11 := 1;
      TXF.eM12 := 0;
      TXF.eM21 := 0;
      TXF.eM22 := 1;
      TXF.eDx := 0;
      TXF.eDy := 0;
    end;
    1: begin {右移 100}
      TXF.eM11 := 1;
      TXF.eM12 := 0;
      TXF.eM21 := 0;
      TXF.eM22 := 1;
      TXF.eDx := 100;
      TXF.eDy := 0;
    end;
    2: begin {上移 40}
      TXF.eM11 := 1;
      TXF.eM12 := 0;
      TXF.eM21 := 0;
      TXF.eM22 := 1;
      TXF.eDx := 0;
      TXF.eDy := -40;
    end;
    3: begin {横向放大 1.5}
      TXF.eM11 := 1.5;
      TXF.eM12 := 0;
      TXF.eM21 := 0;
      TXF.eM22 := 1;
      TXF.eDx := 0;
      TXF.eDy := 0;
    end;
    4: begin {纵向放大 1.5}
      TXF.eM11 := 1;
      TXF.eM12 := 0;
      TXF.eM21 := 0;
      TXF.eM22 := 1.5;
      TXF.eDx := 0;
      TXF.eDy := 0;
    end;
    5: begin {整体放大 1.5 倍}
      TXF.eM11 := 1.5;
      TXF.eM12 := 0;
      TXF.eM21 := 0;
      TXF.eM22 := 1.5;
      TXF.eDx := 0;
      TXF.eDy := 0;
    end;
    6: begin {整体缩小一半}
      TXF.eM11 := 0.5;
      TXF.eM12 := 0;
      TXF.eM21 := 0;
      TXF.eM22 := 0.5;
      TXF.eDx := 0;
      TXF.eDy := 0;
    end;
    7: begin {水平镜像}
      TXF.eM11 := -1;
      TXF.eM12 := 0;
      TXF.eM21 := 0;
      TXF.eM22 := 1;
      TXF.eDx := 200; {需要右移一些才能看到}
      TXF.eDy := 0;
    end;
    8: begin {垂直镜像}
      TXF.eM11 := 1;
      TXF.eM12 := 0;
      TXF.eM21 := 0;
      TXF.eM22 := -1;
      TXF.eDx := 0;
      TXF.eDy := 200; {需要下移一些才能看到}
    end;
    9: begin {横向斜切}
      TXF.eM11 := 1;
      TXF.eM12 := 0.75;
      TXF.eM21 := 0;
      TXF.eM22 := 1;
      TXF.eDx := 0;
      TXF.eDy := 0;
    end;
    10: begin {纵向斜切}
      TXF.eM11 := 1;
      TXF.eM12 := 0;
      TXF.eM21 := 0.75;
      TXF.eM22 := 1;
      TXF.eDx := 0;
      TXF.eDy := 0;
    end;
    11: begin {旋转 30 度}
      TXF.eM11 := Cos(30*PI/180);
      TXF.eM12 := Sin(30*PI/180);
      TXF.eM21 := -Sin(30*PI/180);
      TXF.eM22 := Cos(30*PI/180);
      TXF.eDx := 0;
      TXF.eDy := 0;
    end;
  end;

  {执行变换并重绘}
  Rgn := ExtCreateRegion(@TXF, RgnDataSize, RgnData^);
  Repaint;

  FreeMem(RgnData);
end;

end.

 
 
 
 
 

 

 

  
窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 259
  ClientWidth = 433
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesktopCenter
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  OnPaint = FormPaint
  PixelsPerInch = 96
  TextHeight = 13
  object RadioGroup1: TRadioGroup
    Left = 320
    Top = 8
    Width = 103
    Height = 243
    Caption = 'RadioGroup1'
    TabOrder = 0
    OnClick = RadioGroup1Click
  end
end

 
 
 
 
 

 

 

  

转载于:https://my.oschina.net/hermer/blog/320767