Recent

Author Topic: [SOLVED] Component question  (Read 2980 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 742
[SOLVED] Component question
« on: October 16, 2021, 07:53:10 am »
Consider this.

I have a form and I place a component (derived from TComponent) on that form.
The component has a boolean property HideOnShow.

How do I make the component hide the main form on app startup?

Where and how do I implement this?

Code: Pascal  [Select][+][-]
  1. unit MyTest;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, LResources, LCLIntf, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   StdCtrls, ComCtrls;
  10.  
  11. type
  12.  
  13.   { TMyTest}
  14.  
  15.   TMyTest = class(TComponent)
  16.   private
  17.     fHideOnStart : Boolean;
  18.         function GetHideOnStart : Boolean;
  19.         procedure SetHideOnStart(aValue : Boolean);
  20.   protected
  21.    
  22.   public
  23.     constructor Create(AOwner : TComponent); override;
  24.     destructor Destroy; override;
  25.   published
  26.     property HideOnStart : Boolean read GetHideOnStart write SetHideOnStart;
  27.   end;
  28.  
  29. procedure Register;
  30.  
  31. implementation
  32.  
  33. constructor TMyTest.Create(AOwner : TComponent);
  34. begin
  35.   inherited Create(AOwner);
  36. end;
  37.  
  38. destructor TMyTest.Destroy;
  39. begin
  40.   inherited;
  41. end;
  42.  
  43. function TMyTest.GetHideOnStart : Boolean;
  44. begin
  45.   Result := fHideOnStart;
  46. end;
  47.  
  48. procedure TMyTest.SetHideOnStart(aValue : Boolean);
  49. begin
  50.   if aValue <> fHideOnStart then
  51.     begin
  52.           fHideOnStart := aValue;
  53.         end;
  54. end;
  55.  
  56. procedure Register;
  57.  
  58. begin
  59.   RegisterComponents('Misc',[TMyTest]);
  60. end;
  61.  
  62. end.
  63.  
  64.  

« Last Edit: October 16, 2021, 02:12:11 pm by pcurtis »
Windows 10
Laz 2.0.10
FPC 3.2.0

pcurtis

  • Hero Member
  • *****
  • Posts: 742
Re: Component question
« Reply #1 on: October 16, 2021, 10:10:55 am »
I tried this

Code: Pascal  [Select][+][-]
  1. constructor TMyTest.Create(AOwner : TComponent);
  2. begin
  3.   inherited Create(AOwner);
  4.  
  5.   if not (csDesigning in ComponentState) then
  6.     begin
  7.       fTimer := TTimer.Create(Self);
  8.       fTimer.Interval := 1;
  9.       fTimer.OnTimer := @OnTimer;
  10.     end;
  11. end;
  12.  
  13.  
  14. procedure TMyTest.OnTimer(Sender : TObject);
  15. begin
  16.   if (Self.Owner as TForm).Showing then
  17.     begin
  18.       (Self.Owner as TForm).Hide;
  19.       fTimer.Enabled := False;
  20.     end;
  21. end;  
  22.  

but the main form still appears for a short time.
Windows 10
Laz 2.0.10
FPC 3.2.0

GetMem

  • Hero Member
  • *****
  • Posts: 3510
Re: Component question
« Reply #2 on: October 16, 2021, 10:29:37 am »
Hi pcurtis,

Try the same thing with Application.Mainform:
Code: Pascal  [Select][+][-]
  1. procedure TMyTest.OnTimer(Sender : TObject);
  2. begin
  3.   if Application.Mainform  <> nil then
  4.   begin
  5.     //...
  6.   end;
  7. end;  

pcurtis

  • Hero Member
  • *****
  • Posts: 742
Re: Component question
« Reply #3 on: October 16, 2021, 10:44:48 am »
The form still appears briefly
Windows 10
Laz 2.0.10
FPC 3.2.0

howardpc

  • Hero Member
  • *****
  • Posts: 3830
Re: Component question
« Reply #4 on: October 16, 2021, 12:37:40 pm »
How do I make the component hide the main form on app startup?
App startup happens before your component exists.
You can only hide the main form at startup using Application.ShowMainForm := False in your .lpr

GetMem

  • Hero Member
  • *****
  • Posts: 3510
Re: Component question
« Reply #5 on: October 16, 2021, 01:00:19 pm »
@howardpc
Quote
App startup happens before your component exists.
That's not true.
Code: Pascal  [Select][+][-]
  1.  Application.CreateForm(TForm1, Form1); //components are created here
  2.  Application.Run; //app. start    

The attached package will hide the main form, but a timer is needed because the component must wait until Application.Mainform is assigned. Another possible solution is  to override TApplication's Notification method:
Code: Pascal  [Select][+][-]
  1. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
In this case a timer is not needed, because we can catch the creation of mainform.

howardpc

  • Hero Member
  • *****
  • Posts: 3830
Re: Component question
« Reply #6 on: October 16, 2021, 01:56:30 pm »
Thanks for the correction.
Apologies for spreading disinformation.

pcurtis

  • Hero Member
  • *****
  • Posts: 742
Re: Component question
« Reply #7 on: October 16, 2021, 02:11:07 pm »
OK. Thanks it works.
Windows 10
Laz 2.0.10
FPC 3.2.0

 

TinyPortal © 2005-2018