Delphi做异型窗体PNG透明

减小字体 增大字体 作者:佚名  来源:转载  发布时间:2011-06-01 11:36:35

{*******************************************************}
{                                                                                            }
{       异形窗口                                                                     }
{                                                                                            }
{       2009.12.4 王  锐                                                       }
{                                                                                            }
{*******************************************************}

unit UnitYXForm;

interface
uses
  Windows, Forms, Classes, Graphics;

//从文件加载PNG
procedure YXForm_FromFile(AForm : TForm; AFileName : String);
//从资源加载PNG
procedure YXForm_FromResource(AForm : TForm; ResName : String; ResType : PWideChar; Instance : HINST = 0);
//从图像对象加载
procedure YXForm_FromGraphic(AForm : TForm; AGraphic : TGraphic);

implementation

procedure YXForm_FromFile(AForm : TForm; AFileName : String);
var
  wic : TWICImage;
begin
  wic := TWICImage.Create;
  wic.LoadFromFile(AFileName);
  YXForm_FromGraphic(AForm, wic);
  wic.Free;
end;

procedure YXForm_FromResource(AForm : TForm; ResName : String;ResType : PWideChar; Instance : HINST);
var
  wic : TWICImage;
  r : TResourceStream;
begin
  if Instance = 0 then
    Instance := HInstance;
  r := TResourceStream.Create(Instance, ResName, ResType);
  wic := TWICImage.Create;
  wic.LoadFromStream(r);

  YXForm_FromGraphic(AForm, wic);
  wic.Free;
  r.Free;
end;

procedure YXForm_FromGraphic(AForm : TForm; AGraphic : TGraphic);
var
  ptDst, ptSrc: TPoint;
  Size: TSize;
  BlendFunction: TBlendFunction;
  bmp : TBitmap;
begin
  bmp := TBitmap.Create;
  bmp.Assign(AGraphic);
  ptDst := Point(AForm.Left, AForm.Top);
  ptSrc := Point(0, 0);
  Size.cx := AGraphic.Width;
  Size.cy := AGraphic.Height;

  BlendFunction.BlendOp := AC_SRC_OVER;
  BlendFunction.BlendFlags := 0;
  BlendFunction.SourceConstantAlpha := $FF; // 透明度
  BlendFunction.AlphaFormat := AC_SRC_ALPHA;

  SetWindowLong(AForm.Handle, GWL_EXSTYLE, GetWindowLong(AForm.Handle,
      GWL_EXSTYLE) or WS_EX_LAYERED);
  UpdateLayeredWindow(AForm.Handle,
     AForm.Canvas.Handle,
     @ptDst,
     @Size,
     bmp.Canvas.Handle,
     @ptSrc,
     0,
     @BlendFunction,
     ULW_ALPHA);
  bmp.Free();
end;

end.

想要用的时候很简单,举个例子:
  ff := TForm2.Create(Self);
  YXForm_FromFile(ff, 'c:\a.png');
  ff.Show;

实现动画也很容易.只要不停地YXForm_FromFile(ff, 'c:\a.png');调用一套动作PNG就可以了.


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wr960204/archive/2009/12/04/4941660.aspx

Tags:

作者:佚名
  • 好的评价 如果您觉得此文章好,就请您
      0%(0)
  • 差的评价 如果您觉得此文章差,就请您
      0%(0)

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论

广告位置B