Solved

Transparently Form?

Posted on 1998-12-02
9
249 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
A short film showing how OnPage and Connectwise integration works.

932 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

11 Experts available now in Live!

Get 1:1 Help Now