Recent

Author Topic: Form transparent by regions  (Read 7020 times)

pmesquita

  • Jr. Member
  • **
  • Posts: 58
Form transparent by regions
« on: December 26, 2021, 02:53:13 pm »
Folks,
I need to create a form that has transparency but except for a part that will be displayed a TPanel with several objects in it and this cannot be applied to transparency...

Does anyone have any idea if there is a way to do this?

Here's an example using SetLayeredWindowAttributes that I believe is the solution...

GetMem

  • Hero Member
  • *****
  • Posts: 3741
Re: Form transparent by regions
« Reply #1 on: December 26, 2021, 03:36:13 pm »
Folks,
I need to create a form that has transparency but except for a part that will be displayed a TPanel with several objects in it and this cannot be applied to transparency...

Does anyone have any idea if there is a way to do this?

Here's an example using SetLayeredWindowAttributes that I believe is the solution...

Try something like this:
Code: Pascal  [Select][+][-]
  1. procedure CreateTransparentForm(AForm: TForm; APanel: TPanel);
  2. var
  3.   FormRegion, PanelRegion: HRGN;
  4. begin
  5.   FormRegion := CreateRectRgn(0, 0, AForm.Width, AForm.Height);
  6.   try
  7.     PanelRegion := CreateRectRgn(APanel.Left, APanel.Top, APanel.Left + APanel.Width, APanel.Top + APanel.Height);
  8.     try
  9.       CombineRgn(FormRegion, FormRegion, PanelRegion, RGN_AND);
  10.     finally
  11.       DeleteObject(PanelRegion);
  12.     end;
  13.     SetWindowRgn(AForm.Handle, FormRegion, True);
  14.   finally
  15.     DeleteObject(FormRegion);
  16.   end;
  17. end;
  18.  
  19. procedure TForm1.FormCreate(Sender: TObject);
  20. begin
  21.   CreateTransparentForm(Form1, Panel1);
  22. end;
  23.  
  24. procedure TForm1.CreateParams(var Params: TCreateParams);
  25. begin
  26.   inherited CreateParams(Params);
  27.   Self.BorderStyle := bsNone;
  28. end;

pmesquita

  • Jr. Member
  • **
  • Posts: 58
Re: Form transparent by regions
« Reply #2 on: December 26, 2021, 04:05:00 pm »
GetMem thanks for the reply..

This is not quite what I need, I've been looking at this alternative but without success so far...

But my idea is: the entire area around Panel1, as in my example, is 60% transparent (AlphaBlend) and only Panel1 and its objects are opaque...

Because I need to "see" Panel1 but at the same time "see" the content of the form in which it is contained... style as android messagebox where the background appears blurred...

This is what I need...
I'll use your example to see if I can create a region under the panel where windows ignore the transparency in it and its objects...

jamie

  • Hero Member
  • *****
  • Posts: 4582
Re: Form transparent by regions
« Reply #3 on: December 26, 2021, 05:50:16 pm »
Use the standard facilities of the form with alphablend and set the shade to 128 for example, in the oncreateform event or somewhere at some point, set the form's brush.color  := clBlack.

 Use a Dialog form that you can create as a secondary form and show it over the form1 when in the state that you need this.

 Populate the Dialog with all the needed controls etc.
 
 of course this will direct all input to the dialog form when doing so.
The only true wisdom is knowing you know nothing

pmesquita

  • Jr. Member
  • **
  • Posts: 58
Re: Form transparent by regions
« Reply #4 on: December 26, 2021, 09:56:10 pm »
I even thought about it and so far it is the only reasonable solution to the problem.

I was trying to avoid this way of creating a Form just to get the "transparent background" effect but all the effects I managed to do as you said...

I will add improvements to the TODO for this situation and if there is any solution posted here for the staff

Thanks...

mike_p

  • New Member
  • *
  • Posts: 19
Re: Form transparent by regions
« Reply #5 on: December 27, 2021, 09:40:23 am »
Would you be happy to have some parts of your form transparent and other part opaque?

If so, use chromakey (ie replace a specified colour with transparency)

A while back, working in Delphi 5, I created a component that could be dropped on a form that would provide Alphablend and Chromakey that worked in design time and runtime.

If you're interested I'll dig out some code samples.


pmesquita

  • Jr. Member
  • **
  • Posts: 58
Re: Form transparent by regions
« Reply #6 on: December 28, 2021, 12:15:07 pm »
Basically this is what I need, that the whole area around a TPanel is semi transparent, if you have any ideas or code to do this I thank you..

ASerge

  • Hero Member
  • *****
  • Posts: 1916
Re: Form transparent by regions
« Reply #7 on: December 28, 2021, 06:31:36 pm »
...if you have any ideas or code to do this I thank you..
@jamie idea.
Example. Create a new application. Create a new panel in the form with any controls and two buttons: btnFocusOnPanel outside the panel and btnLeavePanel inside the panel.
Add the code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   AlphaBlendValue := 200;
  4. end;
  5.  
  6. procedure TForm1.btnFocusOnPanelClick(Sender: TObject);
  7. begin
  8.   if not AlphaBlend then
  9.   begin
  10.     AlphaBlend := True;
  11.     PanelFly(Panel1);
  12.   end;
  13. end;
  14.  
  15. procedure TForm1.btnLeavePanelClick(Sender: TObject);
  16. begin
  17.   if AlphaBlend then
  18.   begin
  19.     AlphaBlend := False;
  20.     PanelLand(Panel1);
  21.   end;
  22. end;

and the code for the "flying" panel in front of the code above:
Code: Pascal  [Select][+][-]
  1. procedure PanelFly(Panel: TPanel);
  2. var
  3.   PanelPos: TPoint;
  4.   FlyForm: TForm;
  5.   FlyFormRect: TRect;
  6. begin
  7.   PanelPos := Panel.BoundsRect.TopLeft;
  8.   Panel.Hide;
  9.   FlyForm := TForm.Create(Application);
  10.   FlyForm.BorderStyle := bsNone;
  11.   FlyForm.FormStyle := fsStayOnTop;
  12.   FlyFormRect := Panel.ClientRect;
  13.   FlyFormRect.Offset(Panel.Parent.ClientToScreen(PanelPos));
  14.   FlyForm.BoundsRect := FlyFormRect;
  15.   Panel.Left := 0;
  16.   Panel.Top := 0;
  17.   Panel.Parent := FlyForm;
  18.   Panel.Show;
  19.   FlyForm.ShowModal;
  20. end;
  21.  
  22. procedure PanelLand(Panel: TPanel);
  23. var
  24.   PanelPos: TPoint;
  25.   FlyForm: TForm;
  26.   OwnerForm: TForm;
  27. begin
  28.   FlyForm := Panel.Parent as TForm;
  29.   FlyForm.ModalResult := mrCancel;
  30.   OwnerForm := Panel.Owner as TForm;
  31.   PanelPos := OwnerForm.ScreenToClient(FlyForm.BoundsRect.TopLeft);
  32.   Panel.Hide;
  33.   Panel.Parent := OwnerForm;
  34.   Panel.Left := PanelPos.X;
  35.   Panel.Top := PanelPos.Y;
  36.   Panel.Show;
  37.   FlyForm.Release;
  38. end;

Tony Stone

  • Full Member
  • ***
  • Posts: 143
Re: Form transparent by regions
« Reply #8 on: December 29, 2021, 01:24:51 pm »
I would take a look at some functions in the Cross Codebot library here.  I think I saw something for odd shaped forms.

https://github.com/sysrpl/Cross.Codebot

pcurtis

  • Hero Member
  • *****
  • Posts: 935
Re: Form transparent by regions
« Reply #9 on: December 29, 2021, 06:47:43 pm »
What OS?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

pmesquita

  • Jr. Member
  • **
  • Posts: 58
Re: Form transparent by regions
« Reply #10 on: December 29, 2021, 06:56:13 pm »

pmesquita

  • Jr. Member
  • **
  • Posts: 58
Re: Form transparent by regions
« Reply #11 on: December 29, 2021, 06:56:49 pm »
I would take a look at some functions in the Cross Codebot library here.  I think I saw something for odd shaped forms.

https://github.com/sysrpl/Cross.Codebot

I'll take a calm look later...

pcurtis

  • Hero Member
  • *****
  • Posts: 935
Re: Form transparent by regions
« Reply #12 on: December 29, 2021, 07:09:22 pm »
Try this (attachment)
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

 

TinyPortal © 2005-2018