⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gditools.pas

📁 一个朋友写的delphi的漂亮时钟
💻 PAS
字号:
{*******************************************************}
{                                                       }
{       GDI+ Clock                                      }
{                                                       }
{       版权所有 (C) 2009 Ding Li Fleet                 }
{                                                       }
{       作者:樊升                                      }
{*******************************************************}

unit GDITools;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, GDIPAPI, GDIPOBJ, GDIPUTIL,
  GDIPlusCommon, Graphics, Math;

//计算一个图片旋转后的坐标
function CalcTransRect(Origin: TGpRectF; e: TMatrixArray): TGpRectF;
//在GDI+上画一个图片
procedure DrawPNGImage(AGPGraph: TGPGraphics; APNGImage: TPNGButton); overload;
//在GDI+上画一个图片,支持让字体垂直居中
procedure DrawPNGImage(AGPGraph: TGPGraphics; APNGImage: TPNGButton; AVert: Boolean); overload;
//获得GPFontStyle
function GetGPFontStyle(AFontStyles: TFontStyles): Integer;

implementation

function CalcTransRect(Origin: TGpRectF; e: TMatrixArray): TGpRectF;

  function GetTransPoint(x, y: Single): TGpPointF;
  begin
    Result.X := e[0] * x + e[2] * y;
    Result.Y := e[1] * x + e[3] * y;
  end;

var
  R: TGpRectF;
  pf: array[0..3] of TGpPointF;
  I: Integer;
begin
  R := Origin;
  // 分别计算四个角的相对坐标
  pf[0] := GetTransPoint(R.X, R.Y);
  pf[1] := GetTransPoint(R.Width + R.X, R.Y);
  pf[2] := GetTransPoint(R.X, R.Height + R.Y);
  pf[3] := GetTransPoint(R.Width + R.X, R.Height + R.Y);
  // 取得左上角和右下角的坐标点
  R.X := pf[0].X;
  R.Y := pf[0].Y;
  R.Width := 0;
  R.Height := 0;
  //R := PGPRectF(pf[0].X, pf[0].Y, 0.0, 0.0);
  for I := 0 to 3 do
  begin
    if R.X > pf[I].X then R.X := pf[I].X
    else if R.Width < pf[I].X then R.Width := pf[I].X;
    if R.Y > pf[I].Y then R.Y := pf[I].Y
    else if R.Height < pf[I].Y then R.Height := pf[I].Y;
  end;
  // 求出矩形尺寸
  R.Width := R.Width - R.X;
  R.Height := R.Height - R.Y;
  // 求得左上角实际坐标
  R.X := R.X + e[4];
  R.Y := R.Y + e[5];
  Result := R;
end;


//在GDI+上画一个图片
procedure DrawPNGImage(AGPGraph: TGPGraphics; APNGImage: TPNGButton);
var
  GPImage: TGPImage;
  GPMatrix: TGPMatrix;
  GPPointF: TGPPointF;
  GPFontFamily: TGPFontFamily;
  GPFont: TGPFont;
  GPSolidBrush: TGPSolidBrush;
  GPStringFormat: TGPStringFormat;
  GPRectF: TGPRectF;
  r, g, b: Word;
  AFont: TFont;
begin
  //if not APNGImage.Visible then Exit;
  GPMatrix := TGPMatrix.Create;
  try
    if APNGImage.Image <> '' then
    begin
      GPImage := TGPImage.Create(APNGImage.Image);
      try
        if APNGImage.Rotate = 0 then
          AGPGraph.DrawImage(GPImage, APNGImage.Left, APNGImage.Top, APNGImage.Width,
            APNGImage.Height)
        else
        begin
          GPPointF.X := APNGImage.Left+APNGImage.Width div 2;
          GPPointF.Y := APNGImage.Top+APNGImage.Height div 2;
          GPMatrix.RotateAt(APNGImage.Rotate, GPPointF);
          AGPGraph.SetTransform(GPMatrix);
          AGPGraph.DrawImage(GPImage, APNGImage.Left, APNGImage.Top, APNGImage.Width,
            APNGImage.Height);
          AGPGraph.ResetTransform;
        end;
      finally
        GPImage.Free;
      end;
    end;
    if APNGImage.Caption <> '' then
    begin
      AFont := APNGImage.GetFont;
      r := GetRValue(AFont.Color);
      g := GetGValue(AFont.Color);
      b := GetBValue(AFont.Color);
      GPFontFamily := TGPFontFamily.Create(AFont.Name);
      GPFont := TGPFont.Create(GPFontFamily, AFont.Size, GetGPFontStyle(AFont.Style));
      GPSolidBrush := TGPSolidBrush.Create(MakeColor(255, r, g, b));
      GPStringFormat := TGPStringFormat.Create;
      try
        GPRectF.X := APNGImage.Left;
        GPRectF.Y := APNGImage.Top;
        GPRectF.Width := APNGImage.Width;
        GPRectF.Height := APNGImage.Height;
        // Center-justify each line of text.
        GPStringFormat.SetAlignment(APNGImage.StringAlignment);
        // Center the block of text (top to bottom) in the rectangle.
        if APNGImage.StringAlignment = StringAlignmentCenter then
          GPStringFormat.SetLineAlignment(APNGImage.StringAlignment);
        AGPGraph.SetTextRenderingHint(TextRenderingHintAntiAlias);
        AGPGraph.DrawString(APNGImage.Caption, -1, GPFont, GPRectF, GPStringFormat, GPSolidBrush);
      finally
        GPFontFamily.Free;
        GPFont.Free;
        GPSolidBrush.Free;
        GPStringFormat.Free;
      end;
    end;
  finally
    GPMatrix.Free;
  end;
end;

procedure DrawPNGImage(AGPGraph: TGPGraphics; APNGImage: TPNGButton; AVert: Boolean); overload;
var
  GPImage: TGPImage;
  GPMatrix: TGPMatrix;
  GPPointF: TGPPointF;
  GPFontFamily: TGPFontFamily;
  GPFont: TGPFont;
  GPSolidBrush: TGPSolidBrush;
  GPStringFormat: TGPStringFormat;
  GPRectF: TGPRectF;
  r, g, b: Word;
  AFont: TFont;
begin
  //if not APNGImage.Visible then Exit;
  GPMatrix := TGPMatrix.Create;
  try
    if APNGImage.Image <> '' then
    begin
      GPImage := TGPImage.Create(APNGImage.Image);
      try
        if APNGImage.Rotate = 0 then
          AGPGraph.DrawImage(GPImage, APNGImage.Left, APNGImage.Top, APNGImage.Width,
            APNGImage.Height)
        else
        begin
          GPPointF.X := APNGImage.Left+APNGImage.Width div 2;
          GPPointF.Y := APNGImage.Top+APNGImage.Height div 2;
          GPMatrix.RotateAt(APNGImage.Rotate, GPPointF);
          AGPGraph.SetTransform(GPMatrix);
          AGPGraph.DrawImage(GPImage, APNGImage.Left, APNGImage.Top, APNGImage.Width,
            APNGImage.Height);
          AGPGraph.ResetTransform;
        end;
      finally
        GPImage.Free;
      end;
    end;
    if APNGImage.Caption <> '' then
    begin
      AFont := APNGImage.GetFont;
      r := GetRValue(AFont.Color);
      g := GetGValue(AFont.Color);
      b := GetBValue(AFont.Color);
      GPFontFamily := TGPFontFamily.Create(AFont.Name);
      GPFont := TGPFont.Create(GPFontFamily, AFont.Size, GetGPFontStyle(AFont.Style));
      GPSolidBrush := TGPSolidBrush.Create(MakeColor(255, r, g, b));
      GPStringFormat := TGPStringFormat.Create;
      try
        GPRectF.X := APNGImage.Left;
        GPRectF.Y := APNGImage.Top;
        GPRectF.Width := APNGImage.Width;
        GPRectF.Height := APNGImage.Height;
        // Center-justify each line of text.
        GPStringFormat.SetAlignment(APNGImage.StringAlignment);
        // Center the block of text (top to bottom) in the rectangle.
        if AVert then
          GPStringFormat.SetLineAlignment(StringAlignmentCenter);
        AGPGraph.SetTextRenderingHint(TextRenderingHintAntiAlias);
        AGPGraph.DrawString(APNGImage.Caption, -1, GPFont, GPRectF, GPStringFormat, GPSolidBrush);
      finally
        GPFontFamily.Free;
        GPFont.Free;
        GPSolidBrush.Free;
        GPStringFormat.Free;
      end;
    end;
  finally
    GPMatrix.Free;
  end;
end;

function GetGPFontStyle(AFontStyles: TFontStyles): Integer;
begin
  if fsStrikeOut in AFontStyles then
    Result := 8
  else if fsUnderline in AFontStyles then
    Result := 4
  else if (fsBold in AFontStyles) and (fsItalic in AFontStyles) then
    Result := 3
  else if fsItalic in AFontStyles then
    Result := 2
  else if fsBold in AFontStyles then
    Result := 1
  else
    Result := 0;
  {FontStyleRegular    = Integer(0);
   FontStyleBold       = Integer(1);
   FontStyleItalic     = Integer(2);
   FontStyleBoldItalic = Integer(3);
   FontStyleUnderline  = Integer(4);
   FontStyleStrikeout  = Integer(8);}
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -