Solved

Transparently Form?

Posted on 1998-12-02
9
247 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
 
LVL 10

Expert Comment

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

Cheers,
Viktor
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now