zoukankan      html  css  js  c++  java
  • DELPHI中如何让FORM窗体透明,只显示控件?

    DELPHI中如何让FORM窗体透明,只显示控件?
    分享到:
    对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理 回复次数:7
    largewang
    largewang
    largewang
    等级:Blank
    #1 得分:5 回复于: 2002-12-17 13:58:37
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    BorderStyle := bsNone;
    Brush.Style := bsClear;
    end;


    //保证你只看的到控件!!!
    关注CSDN论坛微博 送CSDN积分大礼包对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理
    wzrlover
    wzrlover
    wzrlover
    等级:Blank
    #2 得分:12 回复于: 2002-12-17 13:59:32
    我想这是你需要的吧:

    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

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

    end.
    与她合影留念,致终将逝去的青春对我有用[1] 丢个板砖[0] 引用 | 举报 | 管理
    Rensun
    Rensun
    Rensun
    我试了,两种方法都可以实现,第一种比较简单,请问两种方法有什么区别呢?
    (技术上)
    用这个方法也可以
    在interface的private里加入

    PROCEDURE CMEraseBkgnd(var Message:TWMEraseBkgnd);Message WM_ERASEBKGND;

    在implemetation里加入
    PROCEDURE Tform1.CMEraseBkgnd(var Message:TWMEraseBkgnd);
    BEGIN
    brush.style:=bsClear;
    Inherited;
    END;

  • 相关阅读:
    [C# 基础知识系列]专题六:泛型基础篇——为什么引入泛型
    [C#基础知识系列]专题十七:深入理解动态类型
    [C# 基础知识系列]专题九: 深入理解泛型可变性
    C#网络编程系列文章索引
    [C#基础知识系列]专题十:全面解析可空类型
    [C# 基础知识系列]专题十一:匿名方法解析
    [C# 基础知识系列]专题十六:Linq介绍
    VSTO之旅系列(一):VSTO入门
    [C# 网络编程系列]专题七:UDP编程补充——UDP广播程序的实现
    [C# 网络编程系列]专题四:自定义Web浏览器
  • 原文地址:https://www.cnblogs.com/blogpro/p/11446663.html
Copyright © 2011-2022 走看看