Recent

Author Topic: CreateWindowHandle  (Read 1747 times)

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: CreateWindowHandle
« Reply #15 on: December 18, 2022, 12:04:36 am »
delphi works directly with window handles etc, where as the LCL does not so you need to use the LCL method of child controls within the parents. This same method will also work in Delphi. It's just that Delphi like I said is directly a Windows UI to start with.

 Using handles will work there like that will work there. You can also use Handles here to but it involves using the Windows Unit and API calls, like SetParent etc.
The only true wisdom is knowing you know nothing

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: CreateWindowHandle
« Reply #16 on: December 18, 2022, 04:07:33 am »
Here is a small try from me, have not tested if that way it works as design-time component, only a little bit tested at run-time.
Code: Pascal  [Select][+][-]
  1. unit kz.ButtonBar;
  2.  
  3. (*
  4. Project: ButtonBar, basicly a Panel with 4 different autosized buttons on
  5. Autor: KodeZwerg
  6. Created with: Lazarus 2.2.4 (rev lazarus_2_2_4) FPC 3.2.2 x86_64-win64-win32/win64
  7. Tested under: Windows 10 64bit / 64bit compile
  8. Version: 1.1
  9. Licence: TheUnlicence
  10. History: 1.0, stable first release
  11.          1.1, added Font property
  12. *)
  13.  
  14. {$mode ObjFPC}{$H+}
  15.  
  16. interface
  17.  
  18. uses
  19.   Classes , SysUtils,
  20.   Forms, Graphics,
  21.   Controls, ExtCtrls, StdCtrls;
  22.  
  23. type
  24.    TkzButtonBar = class(TWinControl)
  25.      strict private
  26.        FOwner: TComponent;
  27.        FParent: TControl;
  28.        FWinParent: TWinControl;
  29.        FPanel: TPanel;
  30.        FAlign: TAlign;
  31.        FButtonClick1,
  32.        FButtonClick2,
  33.        FButtonClick3,
  34.        FButtonClick4,
  35.        FOnResize: TNotifyEvent;
  36.        FButtonCaption1,
  37.        FButtonCaption2,
  38.        FButtonCaption3,
  39.        FButtonCaption4: TCaption;
  40.        FButton1,
  41.        FButton2,
  42.        FButton3,
  43.        FButton4: TButton;
  44.        FFont: TFont;
  45.        FLeft,
  46.        FTop,
  47.        FWidth,
  48.        FHeight: Integer;
  49.        FBorderSpacing: TControlBorderSpacing;
  50.      private
  51.        procedure SetAlign(const AValue: TAlign);
  52.        procedure SetLeft(const AValue: Integer);
  53.        procedure SetTop(const AValue: Integer);
  54.        procedure SetWidth(const AValue: Integer);
  55.        procedure SetHeight(const AValue: Integer);
  56.        procedure SetFont(const AValue: TFont);
  57.        procedure SetButtonCaption1(const AValue: TCaption);
  58.        procedure SetButtonCaption2(const AValue: TCaption);
  59.        procedure SetButtonCaption3(const AValue: TCaption);
  60.        procedure SetButtonCaption4(const AValue: TCaption);
  61.        procedure SetBorderSpacing(const AValue: TControlBorderSpacing);
  62.        procedure SetDefaults;
  63.        procedure CreatePanel;
  64.        procedure Resize;
  65.        procedure OnResize(ASender: TObject);
  66.        procedure OnButtonClick1(ASender: TObject);
  67.        procedure OnButtonClick2(ASender: TObject);
  68.        procedure OnButtonClick3(ASender: TObject);
  69.        procedure OnButtonClick4(ASender: TObject);
  70.      public
  71.        constructor Create(const AOwner: TComponent; const AParent: TControl); overload;
  72.        constructor Create(const AOwner: TComponent; const AParent: TWinControl); overload;
  73.      public
  74.        property Align: TAlign read FAlign write SetAlign default alNone;
  75.        property Left: Integer read FLeft write SetLeft default 0;
  76.        property Top: Integer read FTop write SetTop default 0;
  77.        property Width: Integer read FWidth write SetWidth default 200;
  78.        property Height: Integer read FHeight write SetHeight default 19;
  79.        property Font: TFont read FFont write SetFont;
  80.        property ButtonClick1: TNotifyEvent read FButtonClick1 write FButtonClick1;
  81.        property ButtonClick2: TNotifyEvent read FButtonClick2 write FButtonClick2;
  82.        property ButtonClick3: TNotifyEvent read FButtonClick3 write FButtonClick3;
  83.        property ButtonClick4: TNotifyEvent read FButtonClick4 write FButtonClick4;
  84.        property ButtonCaption1: TCaption read FButtonCaption1 write SetButtonCaption1;
  85.        property ButtonCaption2: TCaption read FButtonCaption2 write SetButtonCaption2;
  86.        property ButtonCaption3: TCaption read FButtonCaption3 write SetButtonCaption3;
  87.        property ButtonCaption4: TCaption read FButtonCaption4 write SetButtonCaption4;
  88.        property BorderSpacing: TControlBorderSpacing read FBorderSpacing write SetBorderSpacing;
  89.    end;
  90.  
  91. procedure Register;
  92.  
  93. implementation
  94.  
  95. procedure Register;
  96. begin
  97.   RegisterComponents('KodeZwerg', [TkzButtonBar]);
  98. end;
  99.  
  100. constructor TkzButtonBar.Create(const AOwner: TComponent; const AParent: TControl);
  101. begin
  102.   inherited Create(AOwner);
  103.   FOwner := AOwner;
  104.   FParent := AParent;
  105.   FWinParent := nil;
  106.   FBorderSpacing := nil;
  107.   FPanel := nil;
  108.   FButton1 := nil;
  109.   FButton2 := nil;
  110.   FButton3 := nil;
  111.   FButton4 := nil;
  112.   FFont := nil;
  113.   FButtonClick1 := nil;
  114.   FButtonClick2 := nil;
  115.   FButtonClick3 := nil;
  116.   FButtonClick4 := nil;
  117.   FOnResize := @OnResize;
  118.   SetDefaults;
  119.   CreatePanel;
  120. end;
  121.  
  122. constructor TkzButtonBar.Create(const AOwner: TComponent; const AParent: TWinControl);
  123. begin
  124.   inherited Create(AOwner);
  125.   FOwner := AOwner;
  126.   FParent := nil;
  127.   FWinParent := AParent;
  128.   FBorderSpacing := nil;
  129.   FPanel := nil;
  130.   FButton1 := nil;
  131.   FButton2 := nil;
  132.   FButton3 := nil;
  133.   FButton4 := nil;
  134.   FFont := nil;
  135.   FButtonClick1 := nil;
  136.   FButtonClick2 := nil;
  137.   FButtonClick3 := nil;
  138.   FButtonClick4 := nil;
  139.   FOnResize := @OnResize;
  140.   SetDefaults;
  141.   CreatePanel;
  142. end;
  143.  
  144. procedure TkzButtonBar.OnResize(ASender: TObject);
  145. begin
  146.   Resize;
  147. end;
  148.  
  149. procedure TkzButtonBar.CreatePanel;
  150. begin
  151.   FPanel := TPanel.Create(FOwner);
  152.   try
  153.     if (FParent <> nil) then
  154.       FPanel.Parent := TWinControl(FParent)
  155.     else
  156.     if (FWinParent <> nil) then
  157.       FPanel.Parent := FWinParent
  158.     else
  159.       FPanel.Parent := nil;
  160.     FPanel.Left := FLeft;
  161.     FPanel.Top := FTop;
  162.     FPanel.Width := FWidth;
  163.     FPanel.Height := FHeight;
  164.     FPanel.Align := FAlign;
  165.     FPanel.DoubleBuffered := True;
  166.     FPanel.BevelInner := bvNone;
  167.     FPanel.BevelOuter := bvNone;
  168.     FPanel.OnResize := FOnResize;
  169.     FPanel.ParentFont := True;
  170.     FFont := TFont.Create;
  171.     try
  172.       FFont := Screen.SystemFont;
  173.     finally
  174.       FPanel.ParentFont := False;
  175.       FPanel.Font := FFont;
  176.     end;
  177.     FBorderSpacing := TControlBorderSpacing.Create(FPanel, nil);
  178.     try
  179.       FBorderSpacing.Around := 5;
  180.       FBorderSpacing.CellAlignHorizontal := ccaFill;
  181.       FBorderSpacing.CellAlignVertical := ccaFill;
  182.       FBorderSpacing.InnerBorder := 0;
  183.       FBorderSpacing.Left := 0;
  184.       FBorderSpacing.Right := 0;
  185.       FBorderSpacing.Top := 0;
  186.       FBorderSpacing.Bottom := 0;
  187.     finally
  188.       FPanel.BorderSpacing := FBorderSpacing;
  189.     end;
  190.     FButton1 := TButton.Create(FPanel);
  191.     try
  192.       FButton1.Parent := FPanel;
  193.       FButton1.Align := alLeft;
  194.       FButton1.ParentFont := True;
  195.       FButton1.BorderSpacing.Left := FBorderSpacing.Around;
  196.       FButton1.BorderSpacing.Right := FBorderSpacing.Around;
  197.     finally
  198.       FButton1.Caption := FButtonCaption1;
  199.       FButton1.OnClick := @OnButtonClick1;
  200.     end;
  201.     FButton2 := TButton.Create(FPanel);
  202.     try
  203.       FButton2.Parent := FPanel;
  204.       FButton2.Align := alLeft;
  205.       FButton2.ParentFont := True;
  206.       FButton2.BorderSpacing.Left := FBorderSpacing.Around;
  207.       FButton2.BorderSpacing.Right := FBorderSpacing.Around;
  208.     finally
  209.       FButton2.Caption := FButtonCaption2;
  210.       FButton2.OnClick := @OnButtonClick2;
  211.     end;
  212.     FButton3 := TButton.Create(FPanel);
  213.     try
  214.       FButton3.Parent := FPanel;
  215.       FButton3.Align := alLeft;
  216.       FButton3.ParentFont := True;
  217.       FButton3.BorderSpacing.Left := FBorderSpacing.Around;
  218.       FButton3.BorderSpacing.Right := FBorderSpacing.Around;
  219.     finally
  220.       FButton3.Caption := FButtonCaption3;
  221.       FButton3.OnClick := @OnButtonClick3;
  222.     end;
  223.     FButton4 := TButton.Create(FPanel);
  224.     try
  225.       FButton4.Parent := FPanel;
  226.       FButton4.Align := alClient;
  227.       FButton4.ParentFont := True;
  228.       FButton4.BorderSpacing.Left := FBorderSpacing.Around;
  229.       FButton4.BorderSpacing.Right := FBorderSpacing.Around;
  230.     finally
  231.       FButton4.Caption := FButtonCaption4;
  232.       FButton4.OnClick := @OnButtonClick4;
  233.     end;
  234.   finally
  235.     FPanel.Caption := '';
  236.     Resize;
  237.   end;
  238. end;
  239.  
  240. procedure TkzButtonBar.Resize;
  241. begin
  242.   FLeft := FPanel.Left;
  243.   FTop := FPanel.Top;
  244.   FWidth := FPanel.Width;
  245.   FHeight := FPanel.Height;
  246.   if (FButton1 <> nil) then
  247.     begin
  248.       FButton1.Width := (FPanel.Width div 4) - (1 * FBorderSpacing.Around);
  249.       FButton1.Left := 1;
  250.     end;
  251.   if (FButton2 <> nil) then
  252.     begin
  253.       FButton2.Width := (FPanel.Width div 4) - (1 * FBorderSpacing.Around);
  254.       FButton2.Left := 2;
  255.     end;
  256.   if (FButton3 <> nil) then
  257.     begin
  258.       FButton3.Width := (FPanel.Width div 4) - (1 * FBorderSpacing.Around);
  259.       FButton3.Left := 3;
  260.     end;
  261.   if (FButton4 <> nil) then
  262.     begin
  263.       FButton4.Width := (FPanel.Width div 4) - (1 * FBorderSpacing.Around);
  264.       FButton4.Left := 4;
  265.     end;
  266. end;
  267.  
  268. procedure TkzButtonBar.SetFont(const AValue: TFont);
  269. begin
  270.   FFont := AValue;
  271.   if (FPanel <> nil) then
  272.     FPanel.Font := FFont;
  273. end;
  274.  
  275. procedure TkzButtonBar.SetBorderSpacing(const AValue: TControlBorderSpacing);
  276. begin
  277.   if (FBorderSpacing = nil) then
  278.     if (FPanel <> nil) then
  279.       try
  280.         FBorderSpacing := TControlBorderSpacing.Create(FPanel, nil);
  281.       finally
  282.       end
  283.     else
  284.       FBorderSpacing := TControlBorderSpacing.Create(nil, nil);
  285.   FBorderSpacing := AValue;
  286.   if (FPanel <> nil) then
  287.     FPanel.BorderSpacing := FBorderSpacing;
  288.   if (FButton1 <> nil) then
  289.     FButton1.BorderSpacing := FBorderSpacing;
  290.   if (FButton2 <> nil) then
  291.     FButton2.BorderSpacing := FBorderSpacing;
  292.   if (FButton3 <> nil) then
  293.     FButton3.BorderSpacing := FBorderSpacing;
  294.   if (FButton4 <> nil) then
  295.     FButton4.BorderSpacing := FBorderSpacing;
  296. end;
  297.  
  298. procedure TkzButtonBar.SetDefaults;
  299. begin
  300.   FAlign := alNone;
  301.   FLeft := 0;
  302.   FTop := 0;
  303.   FWidth := 200;
  304.   FHeight := 19;
  305.   FButtonCaption1 := '<<';
  306.   FButtonCaption2 := '<';
  307.   FButtonCaption3 := '>';
  308.   FButtonCaption4 := '>>';
  309. end;
  310.  
  311. procedure TkzButtonBar.SetAlign(const AValue: TAlign);
  312. begin
  313.   FAlign := AValue;
  314.   FPanel.Align := FAlign;
  315.   Resize;
  316. end;
  317.  
  318. procedure TkzButtonBar.SetLeft(const AValue: Integer);
  319. begin
  320.   FLeft := AValue;
  321.   FPanel.Left := FLeft;
  322. end;
  323.  
  324. procedure TkzButtonBar.SetTop(const AValue: Integer);
  325. begin
  326.   FTop := AValue;
  327.   FPanel.Top := FTop;
  328. end;
  329.  
  330. procedure TkzButtonBar.SetWidth(const AValue: Integer);
  331. begin
  332.   FWidth := AValue;
  333.   FPanel.Width := FWidth;
  334.   Resize;
  335. end;
  336.  
  337. procedure TkzButtonBar.SetHeight(const AValue: Integer);
  338. begin
  339.   FHeight := AValue;
  340.   FPanel.Height := FHeight;
  341.   Resize;
  342. end;
  343.  
  344. procedure TkzButtonBar.SetButtonCaption1(const AValue: TCaption);
  345. begin
  346.   FButtonCaption1 := AValue;
  347.   FButton1.Caption := FButtonCaption1;
  348. end;
  349.  
  350. procedure TkzButtonBar.SetButtonCaption2(const AValue: TCaption);
  351. begin
  352.   FButtonCaption2 := AValue;
  353.   FButton2.Caption := FButtonCaption2;
  354. end;
  355.  
  356. procedure TkzButtonBar.SetButtonCaption3(const AValue: TCaption);
  357. begin
  358.   FButtonCaption3 := AValue;
  359.   FButton3.Caption := FButtonCaption3;
  360. end;
  361.  
  362. procedure TkzButtonBar.SetButtonCaption4(const AValue: TCaption);
  363. begin
  364.   FButtonCaption4 := AValue;
  365.   FButton4.Caption := FButtonCaption4;
  366. end;
  367.  
  368. procedure TkzButtonBar.OnButtonClick1(ASender: TObject);
  369. begin
  370.   if Assigned(FButtonClick1) then
  371.     FButtonClick1(Self);
  372. end;
  373.  
  374. procedure TkzButtonBar.OnButtonClick2(ASender: TObject);
  375. begin
  376.   if Assigned(FButtonClick2) then
  377.     FButtonClick2(Self);
  378. end;
  379.  
  380. procedure TkzButtonBar.OnButtonClick3(ASender: TObject);
  381. begin
  382.   if Assigned(FButtonClick3) then
  383.     FButtonClick3(Self);
  384. end;
  385.  
  386. procedure TkzButtonBar.OnButtonClick4(ASender: TObject);
  387. begin
  388.   if Assigned(FButtonClick4) then
  389.     FButtonClick4(Self);
  390. end;
  391.  
  392. end.
Test Application:
Code: Pascal  [Select][+][-]
  1. unit uMain;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   kz.ButtonBar, Forms, Dialogs, Controls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormCreate(Sender: TObject);
  16.   private
  17.     kzBB: TkzButtonBar;
  18.     procedure kzClick1(ASender: TObject);
  19.     procedure kzClick2(ASender: TObject);
  20.     procedure kzClick3(ASender: TObject);
  21.     procedure kzClick4(ASender: TObject);
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.FormCreate(Sender: TObject);
  36. begin
  37.   kzBB := TkzButtonBar.Create(self, self);
  38.   try
  39.     kzBB.Align := alClient;
  40.     kzBB.ButtonClick1 := @kzClick1;
  41.     kzBB.ButtonClick2 := @kzClick2;
  42.     kzBB.ButtonClick3 := @kzClick3;
  43.     kzBB.ButtonClick4 := @kzClick4;
  44.   finally
  45.   end;
  46. end;
  47.  
  48. procedure TForm1.kzClick1(ASender: TObject);
  49. begin
  50.   ShowMessage('Button1 was clicked');
  51. end;
  52.  
  53. procedure TForm1.kzClick2(ASender: TObject);
  54. begin
  55.   ShowMessage('Button2 was clicked');
  56. end;
  57.  
  58. procedure TForm1.kzClick3(ASender: TObject);
  59. begin
  60.   ShowMessage('Button3 was clicked');
  61. end;
  62.  
  63. procedure TForm1.kzClick4(ASender: TObject);
  64. begin
  65.   ShowMessage('Button4 was clicked');
  66. end;
  67.  
  68. end.
I hope its useful.
« Last Edit: December 18, 2022, 02:45:49 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Paolo

  • Hero Member
  • *****
  • Posts: 538
Re: CreateWindowHandle
« Reply #17 on: December 18, 2022, 10:51:19 am »
Thanks both for suggestion.

I'll check as soon as possible.


Paolo

  • Hero Member
  • *****
  • Posts: 538
Re: CreateWindowHandle
« Reply #18 on: October 01, 2023, 11:24:00 pm »
@Kodezwerg,
It seems that if used at design time your implementation doesn't work.
 

Paolo

  • Hero Member
  • *****
  • Posts: 538
Re: CreateWindowHandle
« Reply #19 on: October 01, 2023, 11:27:03 pm »
I understood "where" my code doesn't in FPC, it is in the first call to Resize during components creation, if I can skip it, all works fine.

 

TinyPortal © 2005-2018