Recent

Author Topic: CreateWindowHandle  (Read 1819 times)

Paolo

  • Hero Member
  • *****
  • Posts: 542
CreateWindowHandle
« on: December 17, 2022, 12:15:08 am »
Helllo,

I'm porting some code from Delphi, and I have

Code: Pascal  [Select][+][-]
  1. TMyComponent = class(TPanel)
  2. ..
  3. protected
  4.   procedure CreateWindowHandle(const Params: TCreateParams); override;
  5. ..
  6. end;
  7.  
  8. implementation
  9. ..
  10. procedure TMyComponents.CreateWindowHandle(const Params: TCreateParams);
  11. begin
  12.   inherited CreateWindowHandle(Params);
  13.   SetButtons;
  14. end;
  15.  

it seems "CreateWindowHandle" doesn't exist in fpc, how can I translate it to fpc ?

win 10 pro/Laz 2.2.2/fpc 3.2.2

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: CreateWindowHandle
« Reply #1 on: December 17, 2022, 12:19:29 am »
You can put "SetButtons" in forms constructor, if there is none, create one.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

dsiders

  • Hero Member
  • *****
  • Posts: 1323
Re: CreateWindowHandle
« Reply #2 on: December 17, 2022, 12:20:52 am »
Helllo,

I'm porting some code from Delphi, and I have

Code: Pascal  [Select][+][-]
  1. TMyComponent = class(TPanel)
  2. ..
  3. protected
  4.   procedure CreateWindowHandle(const Params: TCreateParams); override;
  5. ..
  6. end;
  7.  
  8. implementation
  9. ..
  10. procedure TMyComponents.CreateWindowHandle(const Params: TCreateParams);
  11. begin
  12.   inherited CreateWindowHandle(Params);
  13.   SetButtons;
  14. end;
  15.  

it seems "CreateWindowHandle" doesn't exist in fpc, how can I translate it to fpc ?

win 10 pro/Laz 2.2.2/fpc 3.2.2

I believe the method is called CreateWnd in LCL.
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

Paolo

  • Hero Member
  • *****
  • Posts: 542
Re: CreateWindowHandle
« Reply #3 on: December 17, 2022, 12:29:17 am »
this is a component that has 4 buttons embedded (see attached image), and to create it, if I remeber well (I wrote this many years ago) I need the "container" handle available in advance to draw the buttons even at design time.
Each Button exposes the "click" event in the object inspector.

Paolo

  • Hero Member
  • *****
  • Posts: 542
Re: CreateWindowHandle
« Reply #4 on: December 17, 2022, 12:33:30 am »
tryed both CreateWnd and CreateHandle, but that doesn't solve the problem

jamie

  • Hero Member
  • *****
  • Posts: 6785
Re: CreateWindowHandle
« Reply #5 on: December 17, 2022, 02:05:33 am »
You should be doing that in the constructor.

Override the constructor of your control and first call the inherited constructor then process your buttons which will need a parent at min, which at that point can be the SELF parameter.

 You should being PARENT and OWNER instead of relying on Window handles at that time.
The only true wisdom is knowing you know nothing

Paolo

  • Hero Member
  • *****
  • Posts: 542
Re: CreateWindowHandle
« Reply #6 on: December 17, 2022, 07:56:55 pm »
@jamie, I am trying to work on a reduced example

but this doesn't work

Code: Pascal  [Select][+][-]
  1.  
  2.   TMyCreateTest = class(TPanel)
  3.   private
  4.     FButLeft : TButton;
  5.     FButRight : TButton;
  6.  public
  7.     constructor Create(AOwner: TComponent); override;
  8.   end;
  9. ...
  10. constructor TMyCreateTest.Create(AOwner: TComponent);
  11. begin
  12.   inherited Create(AOwner);
  13.   FButLeft:=TButton.Create(Self);
  14.   FButLeft.Parent:=Self;
  15.   FButLeft.Top:=0;
  16.   FButLeft.Left:=0;
  17.   FButLeft.Width:=(Width)div(2);
  18.   FButLeft.Height:=Height;
  19.   FButLeft.Caption:='LEFT';
  20.   FButRight:=TButton.Create(Self);
  21.   FButRight.Parent:=Self;
  22.   FButRight.Top:=0;
  23.   FButRight.Left:=(Width)div(2)+1;
  24.   FButRight.Width:=Width-((Width)div(2)+1);
  25.   FButRight.Height:=Height;
  26.   FButRight.Caption:='RIGHT';
  27. end;
  28.  

at design time I see the buttons without Captions (picture 1), when I run the code only I see only the panel (picture 2).
If I create the componet at run time with code below, I see the panel and buttons as expected (picture 3).

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Tmp : TMyCreateTest;
  4. begin
  5.   Tmp:=TMyCreateTest.Create(Self);
  6.   Tmp.Parent:=Self;
  7. end;
  8.  
  9.  

I think that not all the things I need are initialized inside mycomponent constructor Create.

suggestions ?

Paolo

  • Hero Member
  • *****
  • Posts: 542
Re: CreateWindowHandle
« Reply #7 on: December 17, 2022, 08:06:02 pm »
A correction : when in design time I see the button correctly with captions... so picture 1 is as below.
I forgot to refresh the component package, sorry. The other things are confirmed.

jamie

  • Hero Member
  • *****
  • Posts: 6785
Re: CreateWindowHandle
« Reply #8 on: December 17, 2022, 08:32:36 pm »
Visible and Enable property?
The only true wisdom is knowing you know nothing

Paolo

  • Hero Member
  • *****
  • Posts: 542
Re: CreateWindowHandle
« Reply #9 on: December 17, 2022, 09:02:05 pm »
adding enabled and visible in create crashed the whole IDE !

Code: Pascal  [Select][+][-]
  1. constructor TMyCreateTest.Create(AOwner: TComponent);
  2. begin
  3.   inherited Create(AOwner);
  4.   FButLeft:=TButton.Create(Self);
  5.   FButLeft.Parent:=Self;
  6.   FButLeft.Top:=0;
  7.   FButLeft.Left:=0;
  8.   FButLeft.Width:=(Width)div(2);
  9.   FButLeft.Height:=Height;
  10.   FButLeft.Caption:='LEFT';
  11.   FButRight:=TButton.Create(Self);
  12.   FButRight.Parent:=Self;
  13.   FButRight.Top:=0;
  14.   FButRight.Left:=(Width)div(2)+1;
  15.   FButRight.Width:=Width-((Width)div(2)+1);
  16.   FButRight.Height:=Height;
  17.   FButRight.Caption:='RIGHT';
  18. //  FButRight.Visible:=true;
  19. //  FButRight.Enabled:=true;
  20. end;    
  21.  

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: CreateWindowHandle
« Reply #10 on: December 17, 2022, 09:25:08 pm »
Can you provide an image made with Delphi App, so we see what at the end you are trying to do.
I mean what else beside 4 buttons must be present?
Maybe there are better ways to do.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

jamie

  • Hero Member
  • *****
  • Posts: 6785
Re: CreateWindowHandle
« Reply #11 on: December 17, 2022, 09:26:40 pm »
You should not be working on a component like that while it's installed.

 Please do it dynamically and then install the final product.
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6785
Re: CreateWindowHandle
« Reply #12 on: December 17, 2022, 11:18:53 pm »
Give you an idea.
You can copy and paste this or just read it, but it has two child buttons inside a panel and with a couple of buttons you can move it around and resize it, etc.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs,ExtCtrls,StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13. TMyControl = Class(TPanel)
  14.   FLeft, FRight:TButton;
  15.   Constructor Create(Aowner:TComponent); Override;
  16.    procedure DoOnResize; Override;
  17.   End;
  18.   TForm1 = class(TForm)
  19.     Button1: TButton;
  20.     Button2: TButton;
  21.     procedure Button1Click(Sender: TObject);
  22.     procedure Button2Click(Sender: TObject);
  23.     procedure FormCreate(Sender: TObject);
  24.   private
  25.   public
  26.    FMyControl:TMyControl;
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.FormCreate(Sender: TObject);
  39. begin
  40.   fMyControl := TMyControl.Create(Self);
  41.   fMyControl.Parent := Self;
  42. end;
  43.  
  44. procedure TForm1.Button1Click(Sender: TObject);
  45. begin
  46.   FMyControl.Left := Random(50);
  47.   FMyControl.Top  := Random(50);
  48. end;
  49.  
  50. procedure TForm1.Button2Click(Sender: TObject);
  51. begin
  52.   FMyControl.Height := 20+Random(40);
  53.   FMyControl.Width := 100+Random(80);
  54. end;
  55.  
  56. procedure TMyControl.DoOnResize;
  57. Begin
  58.   Inherited DoOnResize;
  59.   fLeft.BoundsRect := Rect(0,0,Width div 2,Height);
  60.   fRight.BoundsRect := Rect(Width Div 2,0,Width,Height);
  61. End;
  62. Constructor TMyControl.Create(Aowner:TComponent);
  63.  
  64. Begin
  65.   Inherited Create(AOwner);
  66.   fLeft := Tbutton.Create(Self);
  67.   fLeft.Parent := Self;
  68.   fLeft.Caption := 'Left';
  69.   fRight := Tbutton.Create(Self);
  70.   fRight.Parent := Self;
  71.   fRight.Caption := 'Right';
  72.  
  73. end;
  74.  
  75. end.
  76.  
  77.  
The only true wisdom is knowing you know nothing

Paolo

  • Hero Member
  • *****
  • Posts: 542
Re: CreateWindowHandle
« Reply #13 on: December 17, 2022, 11:19:46 pm »
@KodeZwerg. I did it very likely with delphi 5 or 7 many years ago. It was an attempt to do a component with a group of buttons that works at design time, allowing to set the button click events. See my first post the attached picture.
Now I am in the phase of porting an application of mine to Lazarus-FPC that makes use of this component, so I re-created a lot of my components but I wasn't able to translate this one to FPC code, I am not looking at similar components (now in delphi there is a sort of button panel o something similar).
here below the whole code so you can test; please note that also the buttons on the panel are custom allowing keep it pressed to fire multiple clicks (but you can put simple buttons).

How can I make the code below working in Laz-fpc ?

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyComp = class(TPanel)
  3.     ButLeftFast: TRepeatBtn;           //bottone '<<'
  4.     ButLeft: TRepeatBtn;               //"       ' <'
  5.     ButRight: TRepeatBtn;              //"       ' >'
  6.     ButRightFast: TRepeatBtn;          //"       '>>'
  7.   private
  8.     FEnabled : boolean;             //Se abilitato o no
  9.     FOnClick_ButLeftFast: TNotifyEvent;    //Click '<<'
  10.     FOnClick_ButLeft: TNotifyEvent;        //Click ' <'
  11.     FOnClick_ButRight: TNotifyEvent;       //Click ' >'
  12.     FOnClick_ButRightFast: TNotifyEvent;   //Click '>>'
  13.     procedure Click_ButLeftFastTrans(Sender : TObject);  //Click '<<'
  14.     procedure Click_ButLeftTrans(Sender : TObject);      //Click ' <'
  15.     procedure Click_ButRightTrans(Sender : TObject);     //Click ' >'
  16.     procedure Click_ButRightFastTrans(Sender : TObject); //Click '>>'
  17.  
  18.     procedure SetCaption_ButLeftFast(newValue: TCaption);//"
  19.     function GetCaption_ButLeftFast: TCaption;           //"
  20.     procedure SetCaption_ButLeft(newValue: TCaption);    //Caption 'Set'
  21.     function GetCaption_ButLeft: TCaption;               //Caption 'Get'
  22.     procedure SetCaption_ButRight(newValue: TCaption);   //"
  23.     function GetCaption_ButRight: TCaption;              //"
  24.     procedure SetCaption_ButRightFast(newValue: TCaption);//"
  25.     function GetCaption_ButRightFast: TCaption;           //"
  26.  
  27.     procedure SetButtons;
  28.   protected
  29.     procedure SetEnabled(newValue : boolean); override;
  30.     procedure CreateWindowHandle(const Params: TCreateParams); override;
  31.     procedure Resize; override;
  32.   public
  33.     constructor Create(AOwner: TComponent); override;
  34.     destructor Destroy; override;
  35.   published
  36.     property Caption_ButLeftFast: TCaption read GetCaption_ButLeftFast write SetCaption_ButLeftFast;
  37.     property Caption_ButLeft: TCaption read GetCaption_ButLeft write SetCaption_ButLeft;
  38.     property Caption_ButRight: TCaption read GetCaption_ButRight write SetCaption_ButRight;
  39.     property Caption_ButRightFast: TCaption read GetCaption_ButRightFast write SetCaption_ButRightFast;
  40.     property OnClick_ButLeftFast: TNotifyEvent read FOnClick_ButLeftFast write FOnClick_ButLeftFast;
  41.     property OnClick_ButLeft: TNotifyEvent read FOnClick_ButLeft write FOnClick_ButLeft;
  42.     property OnClick_ButRight: TNotifyEvent read FOnClick_ButRight write FOnClick_ButRight;
  43.     property OnClick_ButRightFast: TNotifyEvent read FOnClick_ButRightFast write FOnClick_ButRightFast;
  44.     property Enabled : boolean read FEnabled write SetEnabled;
  45.   end;
  46.  
  47.   procedure Register;
  48.  
  49. implementation
  50.  
  51. destructor TMyComp.Destroy;
  52. begin
  53.   ButLeft.Free;
  54.   ButRight.Free;
  55.   ButLeftFast.Free;
  56.   ButRightFast.Free;
  57.   inherited Destroy;
  58. end;
  59.  
  60. procedure TMyComp.SetEnabled(newValue : boolean);
  61. begin
  62.   inherited;
  63.   FEnabled:=newValue;
  64.   ButLeft.Enabled:=newValue;
  65.   ButRight.Enabled:=newValue;
  66.   ButRightFast.Enabled:=newValue;
  67.   ButLeftFast.Enabled:=newValue;
  68. end;
  69.  
  70. procedure TMyComp.Click_ButLeftFastTrans(Sender : TObject);
  71. begin
  72.   if Assigned(FOnClick_ButLeftFast) then begin
  73.     FOnClick_ButLeftFast(Self);
  74.   end;
  75. end;
  76.  
  77. procedure TMyComp.Click_ButRightFastTrans(Sender : TObject);
  78. begin
  79.   if Assigned(FOnClick_ButRightFast) then begin
  80.     FOnClick_ButRightFast(Self);
  81.   end;
  82. end;
  83.  
  84. procedure TMyComp.Click_ButLeftTrans(Sender : TObject);
  85. begin
  86.   if Assigned(FOnClick_ButLeft) then begin
  87.     FOnClick_ButLeft(Self);
  88.   end;
  89. end;
  90.  
  91. procedure TMyComp.Click_ButRightTrans(Sender : TObject);
  92. begin
  93.   if Assigned(FOnClick_ButRight) then begin
  94.     FOnClick_ButRight(Self);
  95.   end;
  96. end;
  97.  
  98. procedure TMyComp.Resize;
  99. begin
  100.   inherited Resize;
  101.   SetButtons;
  102. end;
  103.  
  104. constructor TMyComp.Create(AOwner: TComponent);
  105. begin
  106.   inherited Create(AOwner);
  107.   Width:=60;
  108.   Height:=18;
  109.   ControlStyle:=ControlStyle-[csSetCaption];
  110.  
  111.   with Constraints do begin
  112.     minHeight:=18;
  113.     minWidth:=60;
  114.   end;
  115.   TabOrder:=0;
  116.  
  117.   ButLeftFast:=TRepeatBtn.Create(Self);
  118.   ButLeftFast.Parent:=Self;
  119.  
  120.   ButLeft:=TRepeatBtn.Create(Self);
  121.   ButLeft.Parent:=Self;
  122.  
  123.   ButRight:=TRepeatBtn.Create(Self);
  124.   ButRight.Parent:=Self;
  125.  
  126.   ButRightFast:=TRepeatBtn.Create(Self);
  127.   ButRightFast.Parent:=Self;
  128. end;
  129.  
  130. procedure TMyComp.SetCaption_ButLeft(newValue: TCaption);
  131. begin
  132.   ButLeft.Caption:=newValue;
  133. end;
  134.  
  135. function TMyComp.GetCaption_ButLeft: TCaption;
  136. begin
  137.   Result:= ButLeft.Caption;
  138. end;
  139.  
  140. procedure TMyComp.SetCaption_ButLeftFast(newValue: TCaption);
  141. begin
  142.   ButLeftFast.Caption:=newValue;
  143. end;
  144.  
  145. function TMyComp.GetCaption_ButLeftFast: TCaption;
  146. begin
  147.   Result:=ButLeftFast.Caption;
  148. end;
  149.  
  150. procedure TMyComp.SetCaption_ButRight(newValue: TCaption);
  151. begin
  152.   ButRight.Caption:=newValue;
  153. end;
  154.  
  155. function TMyComp.GetCaption_ButRight: TCaption;
  156. begin
  157.   Result:= ButRight.Caption;
  158. end;
  159.  
  160. procedure TMyComp.SetCaption_ButRightFast(newValue: TCaption);
  161. begin
  162.   ButRightFast.Caption:=newValue;
  163. end;
  164.  
  165. function TMyComp.GetCaption_ButRightFast: TCaption;
  166. begin
  167.   Result:=ButRightFast.Caption;
  168. end;
  169.  
  170. procedure TMyComp.SetButtons;
  171. begin
  172.   with ButLeftFast do begin
  173.     Left:=0;
  174.     Top:=0;
  175.     Width:=(Self.Width)div(4);
  176.     Height:=Self.Height;
  177.     TabOrder:=0;
  178.     OnClick:=Click_ButLeftFastTrans;
  179.   end;
  180.  
  181.   with ButLeft do begin
  182.     Left:=(Self.Width)div(4);
  183.     Top:=0;
  184.     Width:=(Self.Width)div(4);
  185.     Height:=Self.Height;
  186.     TabOrder:=1;
  187.     OnClick:=Click_ButLeftTrans;
  188.   end;
  189.  
  190.   with ButRight do begin
  191.     Left:=(Self.Width)div(2);
  192.     Top:=0;
  193.     Width:=(Self.Width)div(4);
  194.     Height:=Self.Height;
  195.     TabOrder:=2;
  196.     OnClick:=Click_ButRightTrans;
  197.   end;
  198.  
  199.   with ButRightFast do begin
  200.     Left:=3*(Self.Width)div(4);
  201.     Top:=0;
  202.     Width:=(Self.Width)div(4);
  203.     Height:=Self.Height;
  204.     TabOrder:=4;
  205.     OnClick:=Click_ButRightFastTrans;
  206.   end;
  207. end;
  208.  
  209. procedure TMyComp.CreateWindowHandle(const Params: TCreateParams);
  210. begin
  211.   inherited CreateWindowHandle(Params);
  212.   SetButtons;
  213. end;
  214.  
  215. procedure Register;
  216. begin
  217.   RegisterComponents('Testing Library',[TMyComp]);
  218. end;
  219.  
  220. end.
  221.  
  222.  
« Last Edit: December 17, 2022, 11:27:58 pm by Paolo »

Paolo

  • Hero Member
  • *****
  • Posts: 542
Re: CreateWindowHandle
« Reply #14 on: December 17, 2022, 11:24:25 pm »
thanks jamie, I'll check.

But my attempt is to have something that works at design time.

In the testing app the component created at run time seems working (See picture.3 of previous post, but the one created at design time when the app runs seems does not working see picture.2 in revious post)
« Last Edit: December 17, 2022, 11:28:14 pm by Paolo »

 

TinyPortal © 2005-2018