如何使窗体变透明???(100分)

  • 主题发起人 主题发起人 igand
  • 开始时间 开始时间
I

igand

Unregistered / Unconfirmed
GUEST, unregistred user!
如何使窗体变透明?用一个右键弹出菜单,可以选择透明或不透明。请告诉我,谢谢!!!![:D][:D]
 
制作透明窗体只要将form.cavas.brush.style设置成bsclear就可以了。
而制作不规则窗体就需要用你所说的三个函数,setwindowrgn(handle,
CreateEllipticRgn(0,0,400,300),true)将建立一个椭圆窗体。

实现窗体透明,主要是拦截CMEraseBkgnd消息,并将窗体画刷的Style属性设为bsClear。
下为简单的例子:
Procedure CMEraseBkgnd(var Message:TWMEraseBkgnd);Message WM_ERASEBKGND;
begin
brush.style := bsClear;
Inherited;
end;

SetWindowRgn,CombineRgn和CreateRectRgn是用于创建任意形状的窗口,
不是用于使窗口透明的。
{Add a button to a form and try this:}
procedure TForm1.FormCreate(Sender: TObject);
var
FullRgn, ClientRgn, ButtonRgn: THandle;
Margin, X, Y: Integer;
begin
Margin := (Width - ClientWidth) div 2;
FullRgn := CreateRectRgn(0, 0, Width, Height);
X := Margin;
Y := Height - ClientHeight - Margin;
ClientRgn := CreateRectRgn(X, Y, X + ClientWidth, Y + ClientHeight);
CombineRgn(FullRgn, FullRgn, ClientRgn, RGN_DIFF);
X := X + Button1.Left;
Y := Y + Button1.Top;
ButtonRgn := CreateRectRgn(X, Y, X + Button1.Width, Y + Button1.Height);
CombineRgn(FullRgn, FullRgn, ButtonRgn, RGN_OR);
SetWindowRgn(Handle, FullRgn, True);
end;


 
参考以下代码:
unit Unit1;

{The transparent form effect is done with Regions.
First create a region that encompasses the entire form.
Then, find the client area of the form (Client vs. non-Client) and
combine with the full region with RGN_DIFF to make the borders
and title bar visible. Then create a region for each of the
controls and combine them with the original (FullRgn) region.}

{From various posts in the newsgroups - based on some famous
author I'm sure, but I first saw the post by Kerstin Thaler...}

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Panel1: TPanel;
Button2: TButton;
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormResize(Sender: TObject);
private
{ Private declarations }
procedure DoVisible;
procedure DoInvisible;
public
{ Public declarations }
end;

var
Form1: TForm1;
FullRgn, ClientRgn, CtlRgn : THandle;

implementation

{$R *.DFM}

procedure TForm1.DoInvisible;
var
AControl : TControl;
A, Margin, X, Y, CtlX, CtlY : Integer;
begin
Margin := ( Width - ClientWidth ) div 2;
//First, get form region
FullRgn := CreateRectRgn(0, 0, Width, Height);
//Find client area region
X := Margin;
Y := Height - ClientHeight - Margin;
ClientRgn := CreateRectRgn( X, Y, X + ClientWidth, Y + ClientHeight );
//'Mask' out all but non-client areas
CombineRgn( FullRgn, FullRgn, ClientRgn, RGN_DIFF );

//Now, walk through all the controls on the form and 'OR' them
// into the existing Full region.
for A := 0 to ControlCount - 1 do begin
AControl := Controls[A];
if ( AControl is TWinControl ) or ( AControl is TGraphicControl )
then with AControl do begin
if Visible then begin
CtlX := X + Left;
CtlY := Y + Top;
CtlRgn := CreateRectRgn( CtlX, CtlY, CtlX + Width, CtlY + Height );
CombineRgn( FullRgn, FullRgn, CtlRgn, RGN_OR );
end;
end;
end;
//When the region is all ready, put it into effect:
SetWindowRgn(Handle, FullRgn, TRUE);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
//Clean up the regions we created
DeleteObject(ClientRgn);
DeleteObject(FullRgn);
DeleteObject(CtlRgn);
end;

procedure TForm1.DoVisible;
begin
//To restore complete visibility:
FullRgn := CreateRectRgn(0, 0, Width, Height);
CombineRgn(FullRgn, FullRgn, FullRgn, RGN_COPY);
SetWindowRgn(Handle, FullRgn, TRUE);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
//We start out as a transparent form....
DoInvisible;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
//This button just toggles between transparent and not trans..
if Button1.Caption = 'Show Form' then begin
DoVisible;
Button1.Caption := 'Hide Form';
end
else begin
DoInvisible;
Button1.Caption := 'Show Form';
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Application.Terminate;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
//Need to address the transparency if the form gets re-sized.
//Also, note that Form1 scroll bars are set to VISIBLE/FALSE.
//I did that to save a little coding here....
if Button1.Caption = 'Show Form' then
DoInvisible
else
DoVisible;
end;

end.
 
这样例子DFW里已经多了,自己搜一下就可省点银子[:)
 
2000或XP支持半透明,更简单,一个属性就搞定
 
alphablendvalue和alphablend
 
有没有半透明的?鸡蛋兄的那个是全透明的............
 
如果你用的是D6的话,表单有一个属性就是变透明的,透明程度随你自己调.属性名我忘了,
你如果用D6可以试一下.
 
就一句。加在窗口打开里
Alphablend:=true;
呵呵,这个就是让窗口变的透明的办法了
 
Delphi 6的Form中有个属性只需要简单地设置即可。
 
在XP or 2000 下就简单多了
 
用delphi 6 的属性:
alphablend=true
alphablendvalue=0
通过调alphablendvalue的值即可.alphablendvalue的值最大为:255
 
alphablend=true
alphablendvalue=0
 
问题处理越来越简单,我的技术是越来越水了!!![:(][:(!]
 
我会了,谢谢咯~
 
后退
顶部