让窗口透明。并支持鼠标穿过。

unit Unit2;

  {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
  TForm2 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    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
  Form2: TForm2;
  FullRgn, ClientRgn, CtlRgn: THandle;

implementation

{$R   *.DFM}

procedure TForm2.DoInvisible;
var
  AControl: TControl;
  A, Margin, X, Y, CtlX, CtlY: Integer;
  a1, a2: THandle;
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);
  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];
    //ShowMessage(AControl.ClassName);
    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 TForm2.FormDestroy(Sender: TObject);
begin
      //Clean   up   the   regions   we   created
  DeleteObject(ClientRgn);
  DeleteObject(FullRgn);
  DeleteObject(CtlRgn);
end;

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

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

end;

procedure TForm2.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 TForm2.Button2Click(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TForm2.FormResize(Sender: TObject);
begin

  if Button1.Caption = 'Show   Form' then
    DoInvisible
  else
    DoVisible;
end;

end.

posted on 2009-05-04 19:32  伊布 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/b2020b/archive/2009/05/04/1449017.html

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值