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. 
  

解决方案 »

  1.   

    to daifei(daifei) 
    你好!
    我这里有一个例子:
    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.
    我要的就是这种效果,但是它不能时时刷新,你可以拖动它试试。
    这个例子很简单。
      

  2.   

    win2k下可以的呀:setwindowlong     EX_LAYEREDsetlayeredwindowattrib 好像是这样,记不太清了效果就象是 foxmail 和 flashget  
      

  3.   

    你的那个函数不是透明,只是把屏幕的画面映射到form上。
    你新建一个程序,在上面放button1,button2,和Panel1.
    然后把代码完全覆盖unit1.pas试试看。
    真正的透明是点空白处就是点到form以外啦。
      

  4.   

    这样使一般的FORM窗体实现透明!,但是怎样才能使ACTIVEFORM
      

  5.   

    这样使一般的FORM窗体实现透明!,但是怎样才能使ACTIVEFORM
      

  6.   

    这样使一般的FORM窗体实现透明!,但是怎样才能使ACTIVEFORM窗体也能背景透明化!!!
    加分!!!!
    加分!!!!!!!!
    要求源码!!!!!!!!
      

  7.   

    wk,破csdn连防止重复提交的也没!!!
    加分!!!!
      

  8.   

    PROCEDURE Tform1.CMEraseBkgnd(var Message:TWMEraseBkgnd);BEGINbrush.style:=bsClear;Inherited;END;
    实现的是真正的透明吗???我在ActiveForm中试过,好象如daifei所说的那样,只是把屏幕的画面映射到ActiveForm上,到底如何实现真正的透明呢(像Label的Transparent属性一样)????
      

  9.   

    最快﹐最有效﹐可能是最好的方法﹐ 使用CoolForm第三方控件
      

  10.   

    用API函数吧,
    SetWindowLong(Self.Handle,GWL_EXSTYLE,GWL_WNDPROC);
    这样就可以使窗体透明了。
      

  11.   

    to HarryZhang(Harry)
    你好!
    你的方法和原来我的方法一样,是映射屏幕的。
    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窗体中使用时,刷新的时候有些问题,还有待解决。希望和大家一起讨论。
      

  12.   

    看看csdn的技术文档,那里有。
      

  13.   

    抱歉,俺解决不了。那段代码俺也是copy来的,没有仔细研究过。
      

  14.   

    用这个SetLayeredWindowAttributes函数,可惜只支持Win2000:unit Unit1;interfaceuses
      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.
      

  15.   

    to zork(我是一条鱼……)
    你好!
    其实我贴的过程对于原生WINDOW程序是一点问题也没有的,但是对于在网页上实现就有些问题。
    所以想和大家一起来解决。我在网页的WINDOW_ONLOAD事件中调用ACTIVEFORM的方法来实现窗体的透明,一装载时,并不透明,但刷新一次就可以了。
    另外
    我想问一问对于窗体上的控件可不可以使其透明?
    我要再加分!!!!
    欢迎大家灌水,但最好是与本话题有关!!!!
      

  16.   

    呵呵,给你一个提示,刚看到书的:)
    用CreateRectRgn、CombinRgn、SetWindowRgn这些函数。
      

  17.   

    BUPackage里面有控件。很好用。
      

  18.   

    to zork(我是一条鱼……):你的方法在2000中实现了透明,但你的代码我看不打懂,如何再去除透明呢? 
      

  19.   

    哎.....
    csdn的人呢?????
    没有办法!
    看来只有再加分了!!!!
      

  20.   

    procedure TForm1.FormCreate(Sender: TObject); 
    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;
      

  21.   

    我在前面已经贴过了!!!
    一、使用这个方法对于原生WINDOW程序是一点问题也没有的,但是对于在网页上实现就有些问题。我在网页的WINDOW_ONLOAD事件中调用ACTIVEFORM的方法来实现窗体的透明,一装载时,并不透明,但刷新一次就可以了。为什么会这样???怎么解决????
    二、可不可以使窗体上的控件透明????我想很可能要使用API函数,但是是什么函数呢?
    因为我要在网页上使用ACTIVEFORM,但是他的背景不和网页背景一致,并且控件的背景色也很难看,所以我想让他们都变成透明的。怎么解决??????????
    我只能加到这么多了,不能再加了!!!
      

  22.   

    HarryZhang(Harry)已经说了,我就不用多说了,给分吧
      

  23.   


    大哥,我自己的法子已经可以了!!至于刷新问题,还是有的,没有解决啊!
    算了!
    我再问一个问题:可不可以使窗体上的控件透明????我想很可能要使用API函数,但是是什么函数呢?
    因为我要在网页上使用ACTIVEFORM,但是他的背景不和网页背景一致,并且控件的背景色也很难看,所以我想让他们都变成透明的。怎么解决??????????
    解决了,有高分啊!