Link to home
Start Free TrialLog in
Avatar of rozanek
rozanek

asked on

Transparently Form?

How to make the Form1 transparent?
Avatar of erajoj
erajoj
Flag of Sweden image

Hi, is this what you want?:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Canvas.Brush.Style := bsClear;
end;

/// John
ASKER CERTIFIED SOLUTION
Avatar of bryanphillips
bryanphillips

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bryan, your code doesn't make the form transparent, it makes a BIG HOLE IN IT!

/// John
John's right y'all :)))

Cheers,
Viktor
Avatar of Thaddy
Thaddy

But this will make the border disappear as well:
procedure TForm1.FormCreate(Sender: TObject);
begin
  Form1.Canvas.Brush.Style := bsClear;
  Form1.Borderstyle:=bsNone;//Yup!, Gone
end;

C'mon Thaddy Bear :)
I guess I was confused as to what you wanted. Sorry :).
Use this to toggle transparent/normal


Drop a panel and 2 buttons to the form and use this:



            unit Unit1;

            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;
              Button1.Caption := 'Hide Form';
            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.

( don't forget to double click on the object inspector events for buttonclick and form create, resize and destroy )

bryan

Avatar of rozanek

ASKER

Thank you for help, friend.