其实最简单的方法是:
在FormCreate中,添加代码:
Brush.Style:=bsClear;
即可,注意,不是Canvas.Brush.Style!
procedure TForm1.FormCreate(Sender: TObject);
var p:longint;
begin
p:=GetWindowLong(handle,GWL_STYLE);//获取窗体属性
SetWindowLong(handle,GWL_EXSTYLE,p+WS_EX_TRANSPARENT);//设置新属性
form1.Refresh;
end;
***************************
To make a form transparent you should put these 2 lines in form's creation procedure:
procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.Brush.Style:=bsClear;
Form1.BorderStyle:=bsNone;
end;
If you use only that you will notice that the form is transparent but if you put something
over it, it will not clear its own background and traces of that object will be left on the
form. To solve that you need to make sure that the transparent form's Paint procedure (WM_PAINT)
will be called last. To do that you need to override the TWinControl (TForm's ancestor)
CreateParams procedure and set the Form's extended style (ExStyle) to WS_EX_TRANSPARENT.
Here's the full code for making a form transparent:
type
TForm1 = class(TForm)
procedure CreateParams(var Params:TCreateParams); override;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.CreateParams(var Params:TCreateParams);
Begin
inherited CreateParams(Params);
Params.ExStyle:=WS_EX_TRANSPARENT;
End;
procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.Brush.Style:=bsClear;
Form1.BorderStyle:=bsNone;
end;