Recent

Author Topic: [WORKAROUND] Impossible transparent TCustomControl?  (Read 6439 times)

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
[WORKAROUND] Impossible transparent TCustomControl?
« on: March 12, 2017, 03:23:36 am »
Hi, how should I make a transparent TCustomControl?

So far I have this:

Code: Pascal  [Select][+][-]
  1. unit form_main;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   LCLType, LCLProc, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs;
  9.  
  10. type
  11.  
  12.   { TMyCustomControl }
  13.  
  14.   TMyCustomControl = class(TCustomControl)
  15.   private
  16.  
  17.   public
  18.     constructor Create(AOwner : TComponent);override;
  19.     procedure CreateParams(var Params: TCreateParams); override;
  20.     procedure Paint;override;
  21.   end;
  22.  
  23.  
  24.   { TForm1 }
  25.  
  26.   TForm1 = class(TForm)
  27.     procedure FormActivate(Sender: TObject);
  28.     procedure FormCreate(Sender: TObject);
  29.   private
  30.     FCustomControl : TMyCustomControl;
  31.   public
  32.     procedure Paint;override;
  33.   end;
  34.  
  35. var
  36.   Form1: TForm1;
  37.  
  38. implementation
  39.  
  40. {$R *.lfm}
  41.  
  42. { TForm1 }
  43.  
  44. procedure TForm1.FormActivate(Sender: TObject);
  45. begin
  46.   Invalidate;
  47.   FCustomControl.Invalidate;
  48. end;
  49.  
  50. procedure TForm1.FormCreate(Sender: TObject);
  51. begin
  52.   Canvas.Pen.Width := 10;
  53.   Canvas.Pen.Mode:=pmCopy;
  54.   Canvas.Pen.Color := clWhite;
  55.   Canvas.Brush.Style := bsClear;
  56.   FCustomControl := TMyCustomControl.Create(Self);
  57.   FCustomControl.ControlStyle:=[];
  58.   FCustomControl.Parent := Self;
  59. end;
  60.  
  61. procedure TForm1.Paint;
  62. begin
  63.   inherited Paint;
  64.   Canvas.Rectangle(50,50,100,100);
  65. end;
  66.  
  67. { TMyCustomControl }
  68.  
  69. constructor TMyCustomControl.Create(AOwner: TComponent);
  70. begin
  71.   inherited Create(AOwner);
  72.   Canvas.Brush.Style := bsClear;
  73.   Width:=75;
  74.   Height:=75;
  75. end;
  76.  
  77. procedure TMyCustomControl.CreateParams(var Params: TCreateParams);
  78. begin
  79.   inherited CreateParams(Params);
  80.   Params.ExStyle := WS_EX_TRANSPARENT;
  81. end;
  82.  
  83. procedure TMyCustomControl.Paint;
  84. begin
  85.   Canvas.Rectangle(0,0,75,75);
  86. end;
  87.  
  88. end.
  89.  
  90.  

No transparency though.
« Last Edit: February 13, 2019, 06:05:35 pm by cpicanco »
Be mindful and excellent with each other.
https://github.com/cpicanco/

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Transparent TCustomControl
« Reply #1 on: March 12, 2017, 03:37:36 am »
Is not possible. Only TGraphicControl can be transparent, because it shares the canvas with the parent control.

TCustomControl has own canvas, so it can't be transparent.

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Transparent TCustomControl
« Reply #2 on: March 12, 2017, 03:48:11 am »
Is there some base control that have input (key, mouse), focus and transparency?
Be mindful and excellent with each other.
https://github.com/cpicanco/

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Transparent TCustomControl
« Reply #3 on: March 12, 2017, 02:01:10 pm »
I've worked only with TCustomControl and TGraphicControl

TGraphicControl supports mouse events, but not keyboard.

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Transparent TCustomControl
« Reply #4 on: March 12, 2017, 04:28:54 pm »
Thank you lainz. Anyone else??? Please, I am blocked by this issue
Be mindful and excellent with each other.
https://github.com/cpicanco/

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Transparent TCustomControl
« Reply #5 on: March 12, 2017, 04:45:07 pm »
Any recommendation on using a TGraphicControl that will hook up the keyboard events of its TCustomControl parent (A form, for example) ??

I am re-designing a modular tool box for making psychological experiments, link for the project below.

https://github.com/cpicanco/stimulus_control
« Last Edit: March 12, 2017, 04:57:00 pm by cpicanco »
Be mindful and excellent with each other.
https://github.com/cpicanco/

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Transparent TCustomControl
« Reply #6 on: March 12, 2017, 04:51:56 pm »
Anyone else???
On Windows with themes and using parent/child. Example of transparency for controls:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   TV: TTreeView;
  4.   RG: TRadioGroup;
  5.   i: Integer;
  6. begin
  7.   TV := TTreeView.Create(Self);
  8.   TV.Parent := Self;
  9.   TV.Align := alLeft;
  10.   for i := 0 to 9 do
  11.     TV.Items.AddChild(nil, Format('TV%d', [i]));
  12.   RG := TRadioGroup.Create(Self);
  13.   RG.Parent := TV;
  14.   RG.Align := alLeft;
  15.   for i := 0 to 3 do
  16.     RG.Items.Append(Format('RG%d', [i]));
  17.   RG.ItemIndex := 0;
  18.   ActiveControl := RG.Controls[0] as TWinControl;
  19. //  RG.ControlStyle := RG.ControlStyle + [csOpaque];
  20. end;
Uncomment the line 19 to emulate the behaviour without a theme

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Transparent TCustomControl
« Reply #7 on: March 12, 2017, 05:08:40 pm »
Hi ASerge, thank you for your suggestion. I am afraid I need a cross platform solution, but my priority right now is work on linux.
Be mindful and excellent with each other.
https://github.com/cpicanco/

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Transparent TCustomControl
« Reply #8 on: March 12, 2017, 08:12:04 pm »
So, I choose to use TGraphicControl hooked to Parents/Owners key events. It is working.  :)
Be mindful and excellent with each other.
https://github.com/cpicanco/

papelhigienico

  • New Member
  • *
  • Posts: 18
    • PascalSCADA
Re: [SOLVED] Impossible transparent TCustomControl?
« Reply #9 on: February 11, 2019, 08:38:03 pm »
Please don't mark this as solved, since the proposed solution isn't a TCustomControl descendant, but a TGraphicControl descendant and these classes aren't correlated.

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: [WORKAROUND] Impossible transparent TCustomControl?
« Reply #10 on: February 13, 2019, 06:07:06 pm »
You are right, I have changed to "work around".
Be mindful and excellent with each other.
https://github.com/cpicanco/

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: [WORKAROUND] Impossible transparent TCustomControl?
« Reply #11 on: February 14, 2019, 01:12:21 am »
Code: Pascal  [Select][+][-]
  1. procedure TMyCustomControl.CreateParams(var Params: TCreateParams);
  2. begin
  3.   inherited CreateParams(Params);
  4.   Params.ExStyle := WS_EX_TRANSPARENT;
  5. end;

You need to add a new flag to Params, not rewrite it:

Code: Pascal  [Select][+][-]
  1. Params.ExStyle := Params.ExStyle or WS_EX_TRANSPARENT;

And yes, it is possible to make transparent control, which inherits from TCustomControl. All you need is to capture the parent's background and store this image in an auxiliary buffer that you must use in the Paint method. However, the solution may not be portable.

By the way, look here — https://forum.lazarus.freepascal.org/index.php?topic=36778.0
« Last Edit: February 14, 2019, 03:17:36 am by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: [WORKAROUND] Impossible transparent TCustomControl?
« Reply #12 on: February 14, 2019, 01:02:07 pm »
Thank you very much for your comments furious programming.
Be mindful and excellent with each other.
https://github.com/cpicanco/

 

TinyPortal © 2005-2018