Solved

Transparently Form?

Posted on 1998-12-02
9
255 Views
Last Modified: 2010-04-04
How to make the Form1 transparent?
0
Comment
Question by:rozanek
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 4

Expert Comment

by:erajoj
ID: 1348901
Hi, is this what you want?:

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

/// John
0
 

Accepted Solution

by:
bryanphillips earned 200 total points
ID: 1348902
I don't remember where I got this information. I have tried to locate information about the author but it is not included in the comments. I appologize to whomever it was and hope that he/she gets credit but everyone should include their name in any publicly available code if they want credit.

That aside, here is one way of doing it:

1) Add a new function to your form definition called Disappear:

procedure TForm1.Disappear;
 Var
   frmRegion, tempRegion: HRGN;
   i: Integer;
   Arect: TRect;
 Begin
   frmRegion := 0;
   For I:= 0 To ControlCount - 1 Do Begin
     // create a region for the control
     aRect := Controls[i].BoundsRect;
     // coordinates have to be window-relative, not client area
     // relative
     OffsetRect( aRect, clientorigin.x - left, clientorigin.y - top );
     tempRegion := CreateRectRgnIndirect( aRect );
     // merge the region with the "summary" region we are
     // building
     If frmRegion = 0 Then
       frmRegion := tempRegion
     Else Begin
       CombineRgn( frmRegion, frmRegion, tempRegion, RGN_OR );
       DeleteObject( tempRegion );
     End;
   End;
   // To create a region for the caption and menu bar and add it
   // to the summary, uncomment this section of code.
{   tempregion :=
     CreateRectRgn( 0, 0, Width,
                    GetSystemMetrics( SM_CYCAPTION )+
                    GetSystemMetrics( SM_CYSIZEFRAME )+
                    GetSystemMetrics( SM_CYMENU ));

   CombineRgn( frmRegion, frmRegion, tempRegion, RGN_OR );
   DeleteObject( tempRegion );
}
   SetWindowRgn( handle, frmRegion, true );
 End;


2) Call this function whenever you want the form to DISAPPEAR! All that's left behind are the visible components.
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1348903
Bryan, your code doesn't make the form transparent, it makes a BIG HOLE IN IT!

/// John
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:viktornet
ID: 1348904
John's right y'all :)))

Cheers,
Viktor
0
 
LVL 2

Expert Comment

by:Thaddy
ID: 1348905
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;

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1348906
C'mon Thaddy Bear :)
0
 

Expert Comment

by:bryanphillips
ID: 1348907
I guess I was confused as to what you wanted. Sorry :).
0
 
LVL 3

Expert Comment

by:bryan7
ID: 1348908
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

0
 

Author Comment

by:rozanek
ID: 1348909
Thank you for help, friend.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question