Search code examples
delphidelphi-xetransparenttform

Prevent Click-thru on transparent form


I want to plot a function in a form without the forms background being visible. Created a TForm with TransparentColor set to True and TransParentColorValue set to clWhite. Just put a TImage on it, draw a function on a bitmap, assign it to the image, works great. There is one problem. I cannot click the form anymore. Any click on the form's caption and clientarea lead to a click in the underlying application. This is caused by setting TransparentColor to True. How can I prevent this "clicking-thru"?

Update I tried Sertac's suggestions and I got some remarkable results. I prepare the bitmap as follows:

   Bitmap.Canvas.Brush.Color := clFuchsia;
   Bitmap.Canvas.FillRect (Rect (0, 0, Bitmap.Width, Bitmap.Height));

When Color is set to clWhite and the Forms TransparentColorValue as well, the form is completely click-thru enabled.

Both set to clBlack, the form can be moved, but borders can't be resized and system buttons don't work

Both set to clFuchsia it behaves as a normal Form. Well, it works now but maybe someone got an explanation?


Solution

  • It would seem the API acts weird when some specific colors is used. White is an obvious one, black is a little less problematic. I've tried with yellow, gray, button face, fuchsia etc.. and they look fine.

    If anyone wants to duplicate the problem without using Delphi's 'Transparent...' properties, here's a little snippet:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Color: DWORD;
      DC: HDC;
    begin
      Color := $00FFFFFF;
      SetWindowLong(Handle, GWL_EXSTYLE,
          GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED );
      SetLayeredWindowAttributes(Handle, Color, 255, LWA_COLORKEY);
    
      DC := GetWindowDC(Handle);
      SetDCBrushColor(DC, Color);
      FillRect(DC, Rect(10, 10, 100, 80), GetStockObject(DC_BRUSH));
      ReleaseDC(Handle, DC);
    end;
    


    Note: I'd gladly remove this answer if anyone provides an answer with a definitive explanation.