Dragon P.C. <dragonpc@21cn.com>
http://www.webking.com.cn
Windows 2000支持淡入淡出效果,大家都知道了。前两天研究FormContainer的Form显
示效果时,得高人告知,核心API函数就是SetLayeredWindowAttributes,以下的控件
代码是让你的窗口实现淡入淡出效果而设计,通过编程时动态改变AlphaValue值,您就
可以看到效果了。
unit TranForm; {DragonPC 2001.2.21 }
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TTranForm = class(TComponent)
private
FAlphaValue : integer ;
FHandle : HWND ;
procedure SetFAlphaValue(Alpha:integer) ;
protected
procedure UpdateDisplay ;
public
constructor Create(AOwner: TComponent); override;
published
property AlphaValue : integer read FAlphaValue write SetFAlphaValue ;
end;
procedure Register;
function SetLayeredWindowAttributes
(Handle : HWND; COLORKEY : COLORREF; Alpha : BYTE; Flags : DWORD) :
Boolean;
stdcall; external 'USER32.DLL';
implementation
procedure Register;
begin
RegisterComponents('Standard', [TTranForm]);
end;
{ TTranForm }
procedure TTranForm.SetFAlphaValue(Alpha: integer);
begin
if (Alpha >= 0) and (Alpha < 256) then begin
FAlphaValue := Alpha ;
UpdateDisplay() ;
end else
ShowMessage('请输入0~255之间的值!') ;
end;
procedure TTranForm.UpdateDisplay;
begin
if not (csDesigning in ComponentState) then
SetLayeredWindowAttributes(FHandle, 0, FAlphaValue, 2);
{我屏蔽了设计期的显示效果,如果你愿意可以改改,建议设计时最好不要看到效果}
end;
constructor TTranForm.Create(AOwner: TComponent);
begin
inherited;
FAlphaValue := 255 ;
FHandle := TForm(Owner).Handle ;
if not (csDesigning in ComponentState) then
SetWindowLong(FHandle,GWL_EXSTYLE, GetWindowLong(FHandle, GWL_EXSTYLE )
or $80000 );
{我屏蔽了设计期的显示效果,如果你愿意可以改改,建议设计时最好不要看到效果}
end;
end.
**************
const
WS_EX_LAYERED = $80000;
AC_SRC_OVER = $0;
AC_SRC_ALPHA = $1;
AC_SRC_NO_PREMULT_ALPHA = $1;
AC_SRC_NO_ALPHA = $2;
AC_DST_NO_PREMULT_ALPHA = $10;
AC_DST_NO_ALPHA = $20;
LWA_COLORKEY = $1;
LWA_ALPHA = $2;
ULW_COLORKEY = $1
ULW_ALPHA = $2
ULW_OPAQUE = $4
//新增加的常量定义
function SetLayeredWindowAttributes(hwnd:HWND; crKey:Longint; bAlpha:byte; dwFlags:longint ):longint; stdcall; external user32;//函数声明
procedure TForm1.FormCreate(Sender: TObject);
var l:longint;
begin
l:=getWindowLong(Handle, GWL_EXSTYLE);
l := l Or WS_EX_LAYERED;
SetWindowLong (handle, GWL_EXSTYLE, l);
SetLayeredWindowAttributes (handle, 0, 180, LWA_ALPHA);
//第二个参数是指定透明颜色
//第二个参数为0则使用第四个参数设置alpha值,从0到255,其他的我不太清楚,因为没有api帮助
end;