Transparent window ...

Hi !

May be somebody tried to make window transparent ?
The main problem is to get image of desktop under this
window.
My way is :
  In WM_PAINT event handler:
   ShowWindow(Handle, SW_HIDE);
   SetActiveWindow(0);
    ....
   BitBlt(...);
   //get desktop image
   ...
   Delay ("xxx" miliseconds) // :(:(:(:(:(:(:( sux
   BitBlt(..);
  // paint it to form canvas (using DC)
  ...

 The rest code is not so importent ...

May be somebody knows better way ?
softshakeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MadshiCommented:
Does this help?

            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:= 'Show 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.

Regards, Madshi.
0
WiseGuyCommented:
looking in
0
RBertoraCommented:
Listening..
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

JaymolCommented:
Here.
0
LischkeCommented:
Madhsi, the problem with your code is that it does not allow to draw on the clipped out portions.

softshake, generally Windows' support for transparent windows is very limited. Instead of waitng a specific time you could try to force the desktop to repaint itself immediately by using:

In WM_PAINT event handler:
   ShowWindow(Handle, SW_HIDE);
   UpdateWindow(GetDesktopWindow);
   GDIFlush; // only important for WinNT
   ...
   BitBlt(..);
  // paint it to form canvas (using DC)

Instead of UpdateWindow you can also try RedrawWindow with the internal paint flag.

Ciao, Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jeurkCommented:
Hello,
they are good components around there that does the job.
try http://www.lawrenz.com/coolform/ that is easy to use.

Some of you may have noticed, that I often provide
component links in my components. Let's say that here
Madshi gave the technic. Sometimes the technics involved
are really hard to set up. Or there is a lot of work needed.
That's why I give out component links.

CU guys.
0
MadshiCommented:
Yes, you're right - and that's the sense of my code...  :-)
It depends on what softshake really wants. With my code the user can click "through" the transparent parts and so even click on e.g. desktop icons that are under the transparent parts. I thought, this was, what softshake wants.

Regards, Madshi.
0
LischkeCommented:
Totally correct, Madshi, as "transparent" is not clear enough to describe what's needed. One should perhaps use the term "holes" for what you provided and "transparent" for "parts shining through".

Ciao, Mike
0
MadshiCommented:
:-)   Yes, Mike, exactly...   :-)

jeurk, yep, sometimes components are better (easier to use), but sometimes the questioner wants to understand how it works, then some short sources are better... Now softshake has both - that's perfect for him...   :-)
0
sburckCommented:
Listening...
0
jeurkCommented:
I agree. And that's the approach I like most. Understand
how it works. We already add such conversation. I remember.
Anyway, most of the time I only make comments. I do
not really care about the points. Even if I think it's a cool
system. It happends that I add comments on question already answered.
Just to be kind ;)

I'm interessed in that hole stuff. I think that you guys follow
at least what's going on on torry ? So you know the components
that exists.
so they are at least 3 components that handle holes in your
forms. Something called CoolForm, something called FRFull
another one (can't remember the name). And the last is
called formRegion a component that is no longer supported
and the author gave me out the sources.
Good. The point is that all those components are not that easy
to use. You need a mask, a picture and a transparent color.

Would you guys, help me out to build a nice component that
would do the following. I describe what I need.
To be easy to use I only see 3 functions.
The main functions would work very easily. You simply
drop the component on your form. It takes a transparent color
and makes it's parent transparent without any further question.
You can start hole mode
you can stop hole mode

Those functions should also be used at runtime.
You pass those function a handle. And it does it's stuff.
what do you think ? I'm not that tough, but with your help.

0
MadshiCommented:
Perhaps this helps you starting...   :-)

interface

  { TSkinImage }
  TSkinImage = class(TImage)
  private
  protected
    { Protected declarations }
    function BitmapToRegion(bmp: TBitmap) : dword;
    procedure Loaded; override;
  public
    constructor Create(AOwner : TComponent); override;
  published
    { Published declarations }
  end;

implementation


{ TSkinImage }

constructor TSkinImage.Create(AOwner: TComponent);
begin
  if (AOwner = nil) or (not AOwner.InheritsFrom(TCustomForm)) then
    raise Exception.Create('Invalid Owner!');
  inherited Create(AOwner);
  if not (csDesigning in ComponentState) then
    with TCustomForm(AOwner) do begin
      BorderStyle := bsNone;
      Self.Top := 0;
      Self.Left := 0;
    end;
end;

function TSkinImage.BitmapToRegion(bmp: TBitmap) : dword;
var ix,iy : integer;    // loop variables
    tc    : TColor;     // transparentColor
    b1    : boolean;    // am looking through "real" pixels (no transparent pixels)
    c1    : cardinal;   // region helper variable
    i1    : integer;    // first position of real pixel
begin
  Result := 0;
  i1 := 0;
  // memory transparent color
  tc := bmp.transparentColor and $FFFFFF;
  with bmp.canvas do
    // scan through all lines
    for iy := 0 to bmp.height - 1 do
    begin
      b1 := False;
      // scan through all pixels in this line
      for ix:=0 to bmp.Width - 1 do
        // did we find the first/last real pixel in a row
        if (pixels[ix, iy] and $FFFFFF <> tc) <> b1 then begin
          // yes, and it was the last pixel,
          //so we can add a line style region...
          if b1 then begin
            c1:=CreateRectRgn(i1,iy,ix,iy+1);
            if result<>0 then
              begin
                // it's not the first region
                CombineRgn(Result, Result, c1, RGN_OR);
                DeleteObject(c1);
                // it's the first region
              end
            else
              Result := c1;
          end else i1 := ix;
          // change mode, looking for the first or last real pixel?
          b1:=not b1;
        end;
      // was the last pixel in this row a real pixel?
      if b1 then begin
        c1:=CreateRectRgn(i1, iy, bmp.width, iy+1);
        if (Result <> 0) then
          begin
            CombineRgn(Result, Result, c1, RGN_OR);
            DeleteObject(c1);
          end
        else
          Result := c1;
      end;
    end;
end;

procedure TSkinImage.Loaded;
var Region : dword;
begin
  Region := BitmapToRegion(Picture.Bitmap);
  SetWindowRgn(TCustomForm(Owner).Handle, Region, True);
  DeleteObject(Region);
  inherited;
end;

procedure Register;
begin
  RegisterComponents('Madshi', [TSkinImage]);
end;

Regards, Madshi.
0
BoRiSCommented:
softshake

I think Madshi is onto something there, also you can download a really cool component on torry's page www.torry.ru under the vcl/forms section it allows you to set a form transparent aswell as semi transparent or skin the form.

Later
BoRiS
0
softshakeAuthor Commented:
Hi !
Thank's to all.

Yes , I understand that "transparent" window is only
the trik and I want to use this in half-tranparent windows
(color-glass effect, but this is not for answers, I want
try make this myself :)))).

To Madshi: thanks, very interesting approach :)
To Lischke: works fine, but only then any (except my)
                 forms owned by other applications are Invisible ...
                 I try to use following code in WM_PAINT and
                 in WM_MOVE events hadlers:
                 // -- cut --
                // try to update default DCs of all windows
                // before dispatching WM_PAINT
                // (and after form resize-move)
                PostMessage(HWND_BROADCAST,WM_PAINT,0,0);
                // ...
                :( no results ...

Thanks.
Best wishes
0
LischkeCommented:
softshake, I know that you will ignore this, but I'll try anyway:

Give it up! The support under Windows for transparent windows of any kind is so limited, it is actually not existent. There's no way to make sure all visible windows are completely updated at any time you need it nor is there a way to get at least notified if all windows have finished their painting. I also tried this out for the same reason as you and failed because of the lack of support from Windows.

Anyway, good luck and

Ciao, Mike
0
jeurkCommented:
To Jeuk : Try again ;)

About the transparent windows support. May I add the fact
that windows 2000 should come with some nice functions
to help us a litle. Like transparent or semi transparent
menus, or windows,etc...
Anyway, they are tricks that microsoft don't tells us. yes
you already know that. Like their internal drawing functions,
or their region building stuff. they use internal function that
are fast as hell (I don't know them).

Madshi : I'll come back to you in some day. We'll speak
about holes in windows and hard disk artitions enum ;)

CU
0
LischkeCommented:
Thank you softshake :-)

Jeurk, good points (and I forgot to mention the W2K support), but I honestly believe we will earlier get a 3D shell than one which supports partial transparency correctly...

Ciao, Mike
0
MadshiCommented:
Mike and Jeurk, read this about win2000 and partial transparency:

"Layered Windows
[This is preliminary documentation and subject to change.]

Creating a layered window can significantly improve performance and visual effects for a window that has a complex shape, animates its shape frequently, or uses alpha blending effects. Using layered windows in these situations also improves overall system performance, because the system stores the layered window image, whereas other methods require reloading the image. Visually, the system automatically composes and repaints layered windows and the windows of underlying applications. As a result, layered windows are rendered smoothly, without the flickering typical of complex window regions. In addition, layered windows can be partially translucent through the use of alpha blending.

To create a layered window, specify the WS_EX_LAYERED flag when calling the CreateWindowEx function. The resulting window is not visible, because it has no associated shape or content. To specify the shape, content, and additional properties of a layered window, call either SetLayeredWindowAttributes or UpdateLayeredWindow. The system makes the layered window visible after either of these function calls.

[...]"

:-))

Yes, Jeurk, come back to me...  :-)

Regards, Madshi.
0
LischkeCommented:
Yes Madshi, I know that (I have searched through my MSDN CDs too a while ago :-)), but you know also that M$ often promises to implement a cool feature and what came out...(in particular under consideration that translucency is a very new feature)?

Ciao, Mike
0
MadshiCommented:
Well, at least it *looks* promising, we'll see what we'll get in the end...   :-)
0
jeurkCommented:
About the shell and transparency  you are talking :
http://www.chunkymunky.com/outsider99/img/sshots/ss80_2big.jpg
A sceenshot of the next beta. Seems nice.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.