一个老外写的程序,请仔细看!!!
对你肯定有帮助:
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Spin, ExtCtrls;
type
TForm1 = class(TForm)
ImgBackground1: TImage;
ImgTransparent1: TImage;
SpinEdit1: TSpinEdit;
SpinEdit2: TSpinEdit;
Label1: TLabel;
Label2: TLabel;
ImgSprite1: TImage;
ImgMask1: TImage;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
imgDrawn1: TImage;
Label7: TLabel;
Button1: TButton;
procedure SpinEdit1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
initialized: boolean; // used to indicate when the sprite and mask are made
implementation
{$R *.DFM}
procedure initialize;
const TranspColour=clBlack; // the colour of pixels that will be made invisible
begin // creates the mask and sprite
with form1 do
begin
imgMask1.Height:=imgTransparent1.Height;
imgMask1.Width:=imgTransparent1.Width;
imgSprite1.Height:=imgTransparent1.Height;
imgSprite1.Width:=imgTransparent1.Width;
// now the images have their dimensions equal
// Now, start creating the Mask
imgMask1.Canvas.Draw(0,0,imgTransparent1.Picture.Bitmap);// copy the image
imgMask1.Picture.Bitmap.Mask(TranspColour);
// The mask is now created.
//Now, start creating the sprite
imgSprite1.Canvas.Draw(0,0,imgMask1.Picture.Bitmap);
imgSprite1.Canvas.Pen.Mode:=pmnot; // not mode for inverting the colours of the mask
imgSprite1.Canvas.Rectangle(-1,-1,imgSprite1.Width+1,imgSprite1.Height+1);
// invert colours of the mask
imgSprite1.Canvas.CopyMode:=cmSrcAnd; // and mode
imgSprite1.Canvas.Draw(0,0,imgTransparent1.Picture.Bitmap);
// Sprite is now created.
imgDrawn1.Height:=imgBackground1.Height;
imgDrawn1.Width:=imgBackground1.Width;
// the background now has the dimensions of the background
end;
initialized:=true;
// indicate that the mask, and sprite has been created
end;
procedure TForm1.SpinEdit1Change(Sender: TObject);
// Draw the transparent bitmap
var
x,y: integer; // top,left coordinates of the transparent graphic
begin
if not initialized then
initialize; // Create the mask and sprite, if hasn't been done yet.
x:=spinedit1.value;
y:=spinedit2.value; // get the input coordinates
imgDrawn1.Canvas.Draw(0,0,imgBackground1.Picture.Bitmap); // copy the background into the image
imgDrawn1.Canvas.CopyMode:=cmSrcAnd; // and mode
imgDrawn1.Canvas.Draw(x,y,imgMask1.Picture.Bitmap);
// Mask drawn
imgDrawn1.Canvas.CopyMode:=cmSrcPaint; // or mode
imgDrawn1.Canvas.Draw(x,y,imgSprite1.Picture.Bitmap);
imgDrawn1.Canvas.CopyMode:=cmSrcCopy; // the normal mode
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
initialized:=false;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin // initialize
initialize;
end;