Recent

Author Topic: Make form or window transparent and keep all the control visible  (Read 6291 times)

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 459
  • Programming is FUN only when it works :)
    • Cool Technology
Make form or window transparent and keep all the control visible
« on: November 01, 2023, 07:53:32 pm »
Hi,

Using latest Lazarus and fpc on LINUX operating system.

I want to make my form or window TRANSPARENT and all the controls like TLabel VISIBLE.

I found the solution to make the window TRANSPARENT by using the following code.

Code: Pascal  [Select][+][-]
  1. Form1.AlphaBlend:=true;
  2. Form1.AlphaBlendValue:=0;

But this code completely makes EVERYTHING on the Window and Window itself TRANSPARENT.

I ONLY want the form or window to be TRANSPARENT.

I have gone through ton of related questions and postings on this subject on this forum and all over the internet. None of the suggested solution I found seems to work or I may have missed the answer I am looking for.

Anyone have any idea? If not, could someone please point me in the right direction. Thanks. :)

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Make form or window transparent and keep all the control visible
« Reply #1 on: November 02, 2023, 10:07:51 am »
I just know a solution for Windows that does do what you wrote.
Since I do not know what Linux can and can not, there are 2 simple ways how you can reach your goal.
Method 1: you can give your form a special shape (cut out unwanted parts)
Method 2: you can create each element as its own form
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 459
  • Programming is FUN only when it works :)
    • Cool Technology
Re: Make form or window transparent and keep all the control visible
« Reply #2 on: November 02, 2023, 04:58:24 pm »
I just know a solution for Windows that does do what you wrote.
Since I do not know what Linux can and can not, there are 2 simple ways how you can reach your goal.
Method 1: you can give your form a special shape (cut out unwanted parts)
Method 2: you can create each element as its own form

Essentially, what I want to do is display a TLabel with Transparent form or window. Can it be done? I don't work how I do it or what I use to reach my goal. Just as long as it produces the desired effect that I am looking for.

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 459
  • Programming is FUN only when it works :)
    • Cool Technology
Re: Make form or window transparent and keep all the control visible
« Reply #3 on: November 03, 2023, 03:30:07 pm »
I just know a solution for Windows that does do what you wrote.
Since I do not know what Linux can and can not, there are 2 simple ways how you can reach your goal.
Method 1: you can give your form a special shape (cut out unwanted parts)
Method 2: you can create each element as its own form

How would you achieve Method 1?

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2595
Re: Make form or window transparent and keep all the control visible
« Reply #4 on: November 05, 2023, 02:22:25 pm »
How would you achieve Method 1?

Have a peek at SetWindowRgn and how to create a rgn

Marc
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Make form or window transparent and keep all the control visible
« Reply #5 on: November 05, 2023, 06:09:38 pm »
How would you achieve Method 1?
Like this: (uses Windows....)
Code: Pascal  [Select][+][-]
  1. procedure CutForm(const AForm: TForm);
  2. var
  3.   i: Integer;
  4.   Rgn, CompRgn: HRGN;
  5. begin
  6.   AForm.BorderStyle := bsNone;
  7.   Rgn := CreateRectRgn(AForm.Left, AForm.Top, AForm.Width, AForm.Height);
  8.   for i := 0 to Pred(AForm.ControlCount) do
  9.     begin
  10.       CompRgn := CreateRectRgn(AForm.Controls[i].Left, AForm.Controls[i].Top,
  11.                                AForm.Controls[i].Left + AForm.Controls[i].Width,
  12.                                AForm.Controls[i].Top + AForm.Controls[i].Height);
  13.       CombineRgn(Rgn, Rgn, CompRgn, RGN_OR);
  14.       DeleteObject(CompRgn);
  15.     end;
  16.   SetWindowRgn(AForm.Handle, Rgn, True);
  17.   DeleteObject(Rgn);
  18. end;
Now your form is gone and just the controls are left, you can finetune it to just react on visible controls etc.... just written in a hurry for you
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

ASerge

  • Hero Member
  • *****
  • Posts: 2288
Re: Make form or window transparent and keep all the control visible
« Reply #6 on: November 06, 2023, 05:22:38 am »
Like this: (uses Windows....)
Did you test your code when the form is in the upper left corner?
Corrected version:
Code: Pascal  [Select][+][-]
  1. uses Windows;
  2.  
  3. procedure CutForm(AForm: TForm);
  4. var
  5.   Rgn, CompRgn: HRGN;
  6.   C: TControl;
  7. begin
  8.   AForm.BorderStyle := bsNone;
  9.   Rgn := CreateRectRgn(0, 0, 0, 0);
  10.   for C in AForm.GetEnumeratorControls do
  11.   begin
  12.     CompRgn := CreateRectRgnIndirect(C.BoundsRect);
  13.     CombineRgn(Rgn, Rgn, CompRgn, RGN_OR);
  14.     DeleteObject(CompRgn);
  15.   end;
  16.   SetWindowRgn(AForm.Handle, Rgn, True);
  17.   DeleteObject(Rgn);
  18. end;

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Make form or window transparent and keep all the control visible
« Reply #7 on: November 06, 2023, 09:42:16 am »
just written in a hurry for you
Did you test your code when the form is in the upper left corner?
No, I just run it, pushed a button to check and posted it here, thank you for testing all possible situations and correcting it.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

paweld

  • Hero Member
  • *****
  • Posts: 1090
Re: Make form or window transparent and keep all the control visible
« Reply #8 on: November 06, 2023, 02:44:03 pm »
SetWindowRgn is multiplatform, just instead of the Windows unit add LCLType, LCLIntf     
@KodeZwerg's example runs on linux without a problem
Best regards / Pozdrawiam
paweld

r.lukasiak

  • Full Member
  • ***
  • Posts: 143
Re: Make form or window transparent and keep all the control visible
« Reply #9 on: May 12, 2024, 06:40:24 am »
Hi everyone,
I'm sorry for reheating an old post but I've been looking for something that's been discussed here.

I wanna make a form of a modal window transparent but still keep all controls on it visible.
I tried both @ASerge's and @KodeZwerg's solutions, taking into account @paweld's hint on uses LCLType, LCLIntf (as I use Linux) but all I got is the title bar to disappear, the body is still visible.
I see that @paweld uses XFCE/GTK but I'm KDE/Qt5, may it make the difference or I did something wrong?
At what point shall I call procedure CutForm(AForm: TForm)?? I tried it before MyModalShowModal from the main form and within its OnCreate, both with no expected result.

any hint?

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Make form or window transparent and keep all the control visible
« Reply #10 on: May 12, 2024, 11:42:26 am »
Hi everyone,
I'm sorry for reheating an old post but I've been looking for something that's been discussed here.

I wanna make a form of a modal window transparent but still keep all controls on it visible.
I tried both @ASerge's and @KodeZwerg's solutions, taking into account @paweld's hint on uses LCLType, LCLIntf (as I use Linux) but all I got is the title bar to disappear, the body is still visible.
I see that @paweld uses XFCE/GTK but I'm KDE/Qt5, may it make the difference or I did something wrong?
At what point shall I call procedure CutForm(AForm: TForm)?? I tried it before MyModalShowModal from the main form and within its OnCreate, both with no expected result.

any hint?
At first, simply test it within a button click if it does do what you want to achieve. When it works, put code into "OnShow"-Event.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

r.lukasiak

  • Full Member
  • ***
  • Posts: 143
Re: Make form or window transparent and keep all the control visible
« Reply #11 on: May 12, 2024, 01:48:00 pm »
it works, thanks!

One of the controls that's left is a TImage, even though I loaded a transparent PNG it doesn't seem to be transparent, it seems to "see" the form. Is there any way to remove the form leftovers from beneath TImage?

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Make form or window transparent and keep all the control visible
« Reply #12 on: May 12, 2024, 02:17:55 pm »
it works, thanks!
Good news!
it works, thanks!One of the controls that's left is a TImage, even though I loaded a transparent PNG it doesn't seem to be transparent, it seems to "see" the form. Is there any way to remove the form leftovers from beneath TImage?
No, due to the nature of my approach it does do exact what I have told, it cuts everything away where no control is drawn but controls are still bound on the canvas of the created TForm, including its settings.
When you want that an image takes the background of the screen you will need either to create that image on the desktop or you "fake" it on the canvas of the TForm/TImage by first hardcopy the rectangle behind the form/image, draw that hardcopy either to TForm canvas or assign it to the image and finally alphablend your actual image on the image control. I can not help more on that matter.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

r.lukasiak

  • Full Member
  • ***
  • Posts: 143
Re: Make form or window transparent and keep all the control visible
« Reply #13 on: May 12, 2024, 02:32:03 pm »
well, that's what I thought but it didn't hurt to ask  :D

Thank you very much!

 

TinyPortal © 2005-2018