1

我有一个用 Delphi 7 编写的绘图软件的源代码。代码很漂亮而且很大。使用该软件绘制的所有内容都是实心的,没有透明度。我尝试阅读大部分代码,发现用于绘图的主要组件是 TCanvas。是否有任何简单的方法来设置 TCanvas 以便允许透明度?谢谢。

4

1 回答 1

3

透明度不是画布的问题,而是绘画的问题。如果这是您的问题,那么除了分层窗口之外,没有其他方法可以使 wincontrol 真正透明,但您可以在每个画布上以透明方式进行绘制。例如

Canvas.Draw(0,0,png1);
Canvas.Draw(0,0,png2); 

如果你想做透明的简单绘画,你可以使用 GDI+,可以从http://www.progdigy.com/?page_id=7获得

这将是一个示例,如何在没有附加库的情况下绘制透明选择

unit Unit3;
// 20121108 by Thomas Wassermann
interface

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

type
  TForm3 = class(TForm)
    Image1: TImage;
    PaintBox1: TPaintBox;
    procedure FormCreate(Sender: TObject);
    procedure PaintBox1Paint(Sender: TObject);
    procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  private
    { Private-Deklarationen }
    FDownPoint, FCurrentPoint: TPoint;
  public
    { Public-Deklarationen }
  end;

var
  Form3: TForm3;

implementation

uses Math;
{$R *.dfm}

procedure TForm3.FormCreate(Sender: TObject);
begin
  PaintBox1.BringToFront;
end;

type
  pRGBQuadArray = ^TRGBQuadArray;
  TRGBQuadArray = ARRAY [0 .. $EFFFFFF] OF TRGBQuad;

Procedure SetAlpha(bmp: TBitMap; Alpha: Byte; R: TRect);
var
  pscanLine32: pRGBQuadArray;
  i, j: Integer;
begin
  bmp.PixelFormat := pf32Bit;
  bmp.HandleType := bmDIB;
  bmp.ignorepalette := true;
  bmp.alphaformat := afDefined;
  for i := 0 to bmp.Height - 1 do
  begin
    pscanLine32 := bmp.Scanline[i];
    for j := 0 to bmp.Width - 1 do
    begin
      if (j >= R.Left) and (j <= R.Right) and (i >= R.Top) and (i <= R.Bottom) then
      begin
        pscanLine32[j].rgbReserved := 0;
        pscanLine32[j].rgbBlue := 0;
        pscanLine32[j].rgbRed := 0;
        pscanLine32[j].rgbGreen := 0;
      end
      else
      begin
        pscanLine32[j].rgbReserved := Alpha;
        pscanLine32[j].rgbBlue := Alpha;
        pscanLine32[j].rgbRed := Alpha;
        pscanLine32[j].rgbGreen := Alpha;
      end;
    end;
  end;
end;

procedure TForm3.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FDownPoint.X := X;
  FDownPoint.Y := Y;
  FCurrentPoint := FDownPoint;
  PaintBox1.Invalidate;
end;

procedure TForm3.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  if ssLeft in Shift then
  begin
    FCurrentPoint.X := X;
    FCurrentPoint.Y := Y;
    PaintBox1.Invalidate;
  end;
end;

procedure TForm3.PaintBox1Paint(Sender: TObject);
var
  bmp: TBitMap;
  SelRect: TRect;
begin
  bmp := TBitMap.Create;
  try
    bmp.Width := PaintBox1.Width;
    bmp.Height := PaintBox1.Height;
    if (FCurrentPoint.X = FDownPoint.X) and (FCurrentPoint.Y = FDownPoint.Y) then
      SelRect := PaintBox1.BoundsRect
    else
    begin
      SelRect.Left := Min(FCurrentPoint.X, FDownPoint.X);
      SelRect.Top := Min(FCurrentPoint.Y, FDownPoint.Y);
      SelRect.Right := Max(FCurrentPoint.X, FDownPoint.X);
      SelRect.Bottom := Max(FCurrentPoint.Y, FDownPoint.Y);
    end;
    SetAlpha(bmp, 140, SelRect);
    PaintBox1.Canvas.Draw(0, 0, bmp);
  finally
    bmp.Free;
  end;
end;

end.
于 2012-11-14T07:28:12.053 回答