在QQ中,我可以调节面板的透明度。如果我们自己编程,应该如何实现呢?
(一)半透明窗体
在windows2000以及后续产品中下增加了一些API,可以轻易的实现半透明的窗体,源程序如下:
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
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
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function SetLayeredWindowAttributes(hwnd:HWND; crKey:Longint; bAlpha:byte; dwFlags:longint ):longint; stdcall; external user32;//函数声明
var
Form1: TForm1;
implementation
{$R *.DFM}
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
end;
end.
(二) 透明窗体的实现
将一个FORM变成透明的实质性手段就是拦截CMEraseBkgnd消息。
unit Utransform; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
type TForm1 = class(TForm)
private { Private declarations }
public { Public declarations }
PROCEDURE CMEraseBkgnd(var Message:TWMEraseBkgnd);Message WM_ERASEBKGND;
end;
var Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.CMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
brush.style:=bsClear;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.Brush.Style := bsClear;//如果只拦截消息,没有这里的设置,窗内的标题栏一样会显示 Form1.BorderStyle := bsNone
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
close;
end;