1 //1.单元内容 2 unit UnitAlienForm; 3 interface 4 uses 5 Windows, Forms, Classes, Graphics; 6 //从文件加载PNG 7 procedure AlienForm_PNGFromFile(AForm : TForm; AFileName : String); 8 //从资源加载PNG 9 procedure AlienForm_PNGFromResource(AForm : TForm; ResName : String; ResType : PWideChar; Instance : HINST = 0); 10 //从图像对象加载 11 procedure AlienForm_PNGFromGraphic(AForm : TForm; AGraphic : TGraphic); 12 implementation 13 procedure AlienForm_PNGFromFile(AForm : TForm; AFileName : String); 14 var 15 wic : TWICImage; 16 begin 17 wic := TWICImage.Create; 18 wic.LoadFromFile(AFileName); 19 AlienForm_PNGFromGraphic(AForm, wic); 20 wic.Free; 21 end; 22 procedure AlienForm_PNGFromResource(AForm : TForm; ResName : String;ResType : PWideChar; Instance : HINST); 23 var 24 wic : TWICImage; 25 r : TResourceStream; 26 begin 27 if Instance = 0 then 28 Instance := HInstance; 29 r := TResourceStream.Create(Instance, ResName, ResType); 30 wic := TWICImage.Create; 31 wic.LoadFromStream(r); 32 AlienForm_PNGFromGraphic(AForm, wic); 33 wic.Free; 34 r.Free; 35 end; 36 procedure AlienForm_PNGFromGraphic(AForm : TForm; AGraphic : TGraphic); 37 var 38 ptDst, ptSrc: TPoint; 39 Size: TSize; 40 BlendFunction: TBlendFunction; 41 bmp : TBitmap; 42 begin 43 bmp := TBitmap.Create; 44 bmp.Assign(AGraphic); 45 ptDst := Point(AForm.Left, AForm.Top); 46 ptSrc := Point(0, 0); 47 Size.cx := AGraphic.Width; 48 Size.cy := AGraphic.Height; 49 BlendFunction.BlendOp := AC_SRC_OVER; 50 BlendFunction.BlendFlags := 0; 51 BlendFunction.SourceConstantAlpha := $FF; // 透明度 52 BlendFunction.AlphaFormat := AC_SRC_ALPHA; 53 SetWindowLong(AForm.Handle, GWL_EXSTYLE, GetWindowLong(AForm.Handle, 54 GWL_EXSTYLE) or WS_EX_LAYERED); 55 UpdateLayeredWindow(AForm.Handle, 56 AForm.Canvas.Handle, 57 @ptDst, 58 @Size, 59 bmp.Canvas.Handle, 60 @ptSrc, 61 0, 62 @BlendFunction, 63 ULW_ALPHA); 64 bmp.Free(); 65 end; 66 end. 67 68 //2.调用方法 69 procedure TForm2.FormCreate(Sender: TObject); 70 begin 71 AlienForm_PNGFromFile(Form2, 'E:未标题.png'); 72 end;