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.
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.
解决方案 »
- 分辨率不同,图片的位置发生了变化怎么回事
- 如何从几张表中取出满足条件的条目显示到DBGrid中
- delphi7打开和关闭时报"I/O error 103"为什么啊?
- 怎样把serversocket放在一个.pas(没有form)文件中,并且把事件也相应写在里面(做成公用的)
- 如何屏蔽activex控件中的右键菜单
- Delphi开发的MIS应用程序在WIN 98下如何配置才能运行??请各位大侠帮忙!!
- 如何将chart中显示的图形导入到word中??
- 窗口问题?
- 问一个简单的问题,如何才能使工具栏上的按钮靠右排列.
- 请问如何安装delphi的控件!
- 关于连接access 数据库?
- 同志们,干革命吧!一起来吧!
你好!
我这里有一个例子:
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);BEGINbrush.style:=bsClear;Inherited;END;end.
我要的就是这种效果,但是它不能时时刷新,你可以拖动它试试。
这个例子很简单。
你新建一个程序,在上面放button1,button2,和Panel1.
然后把代码完全覆盖unit1.pas试试看。
真正的透明是点空白处就是点到form以外啦。
加分!!!!
加分!!!!!!!!
要求源码!!!!!!!!
加分!!!!
实现的是真正的透明吗???我在ActiveForm中试过,好象如daifei所说的那样,只是把屏幕的画面映射到ActiveForm上,到底如何实现真正的透明呢(像Label的Transparent属性一样)????
SetWindowLong(Self.Handle,GWL_EXSTYLE,GWL_WNDPROC);
这样就可以使窗体透明了。
你好!
你的方法和原来我的方法一样,是映射屏幕的。
to daifei(daifei)
你好!
你的方法太复杂!
请看下面方法!
Procedure TForm1.FormCreate (Sender: TObject);
Var
FullRgn, ClientRgn, ButtonRgn: THandle;
LeftMargin, TopMargin, Margin, X, i: integer;
Begin
Margin: = (Width - ClientWidth) div 2;
FullRgn: = CreateRectRgn (0, 0, Width, Height);
LeftMargin: = Margin;
TopMargin: = Height - ClientHeight - Margin;
ClientRgn: = CreateRectRgn (LeftMargin, TopMargin, LeftMargin + ClientWidth, TopMargin + ClientHeight);
CombineRgn (FullRgn, FullRgn, ClientRgn, RGN_DIFF);
For i: = 0 to ComponentCount-1 do
Begin
If not (Components [i] is TWinControl) then continue;
If (Components [i] as TWinControl). Parent<>self then continue;
X: = LeftMargin + (Components [i] as TWinControl). Left;
Y: = TopMargin + (Components [i] as TWinControl). Top;
ButtonRgn: = CreateRectRgn (X, Y, X + (Components [i] as TWinControl). Width, Y + (Components [i] as TWinControl). Height);
CombineRgn (FullRgn, FullRgn, ButtonRgn, RGN_OR);
End;
SetWindowRgn (Handle, FullRgn, True);
End;
注:对于ActiveForm窗体不太管用(可以将这个过程作为ActiveForm窗体的一个方法,然后对外输出,以便在脚本中调用)
这个方法如果在ActiveForm窗体中使用时,刷新的时候有些问题,还有待解决。希望和大家一起讨论。
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
GroupBox1: TGroupBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function FindFirstFileExA(lpFileName: PAnsiChar; fInfoLevelId: TFindexInfoLevels;
lpFindFileData: Pointer; fSearchOp: TFindexSearchOps; lpSearchFilter: Pointer;
dwAdditionalFlags: DWORD): BOOL; stdcall;function SetLayeredWindowAttributes(
hwnd: HWND; // handle to the layered window
crKey: COLORREF; // specifies the color key
bAlpha: BYTE; // value for the blend function
dwFlags: DWORD // action
): BOOL; stdcall;var
Form1: TForm1;
const
LWA_ALPHA = $2;
LWA_COLORKEY = $1;
WS_EX_LAYERED = $80000; implementationfunction SetLayeredWindowAttributes; external user32 name 'SetLayeredWindowAttributes';
function FindFirstFileExA; external kernel32 name 'FindFirstFileExA';{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
begin
SetWindowLong(handle, GWL_EXSTYLE, GetWindowLong(handle, GWL_EXSTYLE) or WS_EX_LAYERED);
SetLayeredWindowAttributes(handle,RGB(255,0,0),128,LWA_ALPHA);
end;end.
你好!
其实我贴的过程对于原生WINDOW程序是一点问题也没有的,但是对于在网页上实现就有些问题。
所以想和大家一起来解决。我在网页的WINDOW_ONLOAD事件中调用ACTIVEFORM的方法来实现窗体的透明,一装载时,并不透明,但刷新一次就可以了。
另外
我想问一问对于窗体上的控件可不可以使其透明?
我要再加分!!!!
欢迎大家灌水,但最好是与本话题有关!!!!
用CreateRectRgn、CombinRgn、SetWindowRgn这些函数。
csdn的人呢?????
没有办法!
看来只有再加分了!!!!
var
I: Integer;
FullRgn,
ClientRgn,
ControlRgn: THandle;
Margin,
MarginX,
MarginY,
X,
Y: Integer;
begin
Margin := (Width - ClientWidth) div 2;
FullRgn := CreateRectRgn(0, 0, Width, Height);
MarginX := Margin;
MarginY := Height - ClientHeight - Margin;
ClientRgn := CreateRectRgn(MarginX, MarginY, MarginX + ClientWidth, MarginY + ClientHeight);
CombineRgn(FullRgn, FullRgn, ClientRgn, RGN_DIFF);
for I:=0 to ControlCount-1 do
begin
X := MarginX + Controls[I].Left;
Y := MarginY + Controls[I].Top;
ControlRgn := CreateRectRgn(X, Y, X + Controls[I].Width, Y + Controls[I].Height);
CombineRgn(FullRgn, FullRgn, ControlRgn, RGN_OR);
end; SetWindowRgn(Handle, FullRgn, True); end;
一、使用这个方法对于原生WINDOW程序是一点问题也没有的,但是对于在网页上实现就有些问题。我在网页的WINDOW_ONLOAD事件中调用ACTIVEFORM的方法来实现窗体的透明,一装载时,并不透明,但刷新一次就可以了。为什么会这样???怎么解决????
二、可不可以使窗体上的控件透明????我想很可能要使用API函数,但是是什么函数呢?
因为我要在网页上使用ACTIVEFORM,但是他的背景不和网页背景一致,并且控件的背景色也很难看,所以我想让他们都变成透明的。怎么解决??????????
我只能加到这么多了,不能再加了!!!
大哥,我自己的法子已经可以了!!至于刷新问题,还是有的,没有解决啊!
算了!
我再问一个问题:可不可以使窗体上的控件透明????我想很可能要使用API函数,但是是什么函数呢?
因为我要在网页上使用ACTIVEFORM,但是他的背景不和网页背景一致,并且控件的背景色也很难看,所以我想让他们都变成透明的。怎么解决??????????
解决了,有高分啊!