Recent

Author Topic: Solved - Drawing to Dynamically Created Form  (Read 1921 times)

Wilko500

  • Full Member
  • ***
  • Posts: 180
Solved - Drawing to Dynamically Created Form
« on: December 24, 2020, 01:05:51 am »
I am recoding my VB6 chart drawing program to Lazarus(2.0.10)/Free Pascal on Windows 7.  I'm at the part where I want to create multiple forms (pages) on which I want to draw lines, text etc.  The number of Pages and their content is determined at runtime by processing a data file one line at a time. In the future the same code will be used to print the charts (and I am aware that some commands are either form or print specific). 

I am able to create an array of pages, albeit not in an optimal way and can draw to them but my drawing is not visible because I'm not using the on paint event maybe.  I'm open to alternative approaches.

After much searching I am unable to find a "simple" explanation of how to use the onpaint event.  If I place drawing commands in the OnPaint event of, say, Form1, then yes it works.  In my case the drawing commands are created dynamically eg on button click. I don't understand how to link the two.

Code fragment below is how I have attempted to create dynamic forms.  Two questions then, is there a better way of creating dynamic forms and how do I write to them?

Thanks Richard

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  6. type
  7.   { TForm1 }
  8.   TForm1 = class(TForm)
  9.     Button1: TButton;
  10.     Button2: TButton;
  11.     procedure Button1Click(Sender: TObject);
  12.     procedure Button2Click(Sender: TObject);
  13.     procedure FormCreate(Sender: TObject);
  14.   private
  15.   public
  16.   Page: Integer;
  17.   arForm: array[1..10] Of Tform; //Global array of forms
  18.   strPage:String;
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25. {$R *.lfm}
  26. { TForm1 }
  27. procedure TForm1.Button1Click(Sender: TObject);
  28. //var
  29. //  arForm: array[1..10] Of Tform;
  30. //  strPage:String;
  31. Begin
  32.   Page:=Page+1;
  33.   strPage:='Page-' + IntToStr(Page);
  34.   arForm[Page]:=TForm.Create(nil);
  35.   arForm[Page].Caption:=StrPage;
  36.   arForm[Page].SetBounds(0,0,850,1100);
  37.   arForm[Page].show;
  38.   //Here I want to draw on the created form
  39.   arForm[Page].Canvas.Line(0,0,Height,Width);
  40.   arForm[Page].Canvas.Line(0,Height,Width,0);
  41.   end;
  42.  
  43. procedure TForm1.Button2Click(Sender: TObject);
  44. var
  45.   i:      Integer;
  46. begin
  47.   for i:=1 to Page Do
  48.      Begin
  49.      arForm[i].Close;
  50.      end;
  51. end;
  52. procedure TForm1.FormCreate(Sender: TObject);
  53. begin
  54. Page:=0;
  55. end;
  56. end.
« Last Edit: December 24, 2020, 10:04:10 pm by Wilko500 »
MacBook Pro mid 2015 with OS Monterey 12.7.6
FPC 3.2.3 Lazarus 3.7
FPC 3.2.2 Lazarus 3.4

jamie

  • Hero Member
  • *****
  • Posts: 7490
Re: Drawing to Dynamically Created Form
« Reply #1 on: December 24, 2020, 02:15:51 am »
when creating forms or what ever, if they are based from the Tcomponent then you should assign an owner to the control so that if you free the owner all the owned controls will get free'd also ..

 Or could free them yourself and they will detach from the owner's list so its not an issue there but you really should assign an owner to them..
The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 5513
  • My goal: build my own game engine using Lazarus
Re: Drawing to Dynamically Created Form
« Reply #2 on: December 24, 2020, 07:03:58 am »
I'm at the part where I want to create multiple forms (pages) on which I want to draw lines, text etc.  The number of Pages and their content is determined at runtime by processing a data file one line at a time.

I think it will better and easier to use a TPageControl or TTabControl or TNotebook. They all are similar but work differently.

Below is the simple demo showing how to use a TPageControl and runtimely add pages and generate some items on it and properly sets their owner and parent.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, StdCtrls, Spin, ComCtrls, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Label1: TLabel;
  17.     PageControl1: TPageControl;
  18.     SpinEdit1: TSpinEdit;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure RandomGenerate(anOwner: TWinControl);
  33. var
  34.   AControl: TControl;
  35. begin
  36.   case Random(2) of
  37.     0: begin // Generate texts
  38.       AControl         := TLabel.Create(anOwner);
  39.       AControl.Parent  := anOwner;
  40.       AControl.Left    := Random(anOwner.Width-10);
  41.       AControl.Top     := Random(anOwner.Height-10);
  42.       AControl.Caption := Random(999999).ToString;
  43.     end;
  44.     1: begin // Generate shapes
  45.       AControl                         := TShape.Create(anOwner);
  46.       AControl.Parent                  := anOwner;
  47.       AControl.Left                    := Random(anOwner.Width-100);
  48.       AControl.Top                     := Random(anOwner.Height-100);
  49.       AControl.Width                   := Random(200)+10;
  50.       AControl.Height                  := Random(200)+10;
  51.       (AControl as TShape).Brush.Color := Random(999999);
  52.       (AControl as TShape).Shape       := TShapeType(Random(ord(High(TShapeType))));
  53.     end;
  54.   end;
  55. end;
  56.  
  57. procedure TForm1.Button1Click(Sender: TObject);
  58. var
  59.   i, j: Integer;
  60. begin
  61.   case Button1.Caption = 'Generate' of
  62.     True:
  63.       begin
  64.         for i := 0 to SpinEdit1.Value-1 do
  65.         begin
  66.           PageControl1.AddTabSheet;
  67.           PageControl1.ActivePageIndex    := PageControl1.PageCount-1;
  68.           PageControl1.ActivePage.Caption := PageControl1.PageCount.ToString;
  69.           for j := 0 to Random(20) do
  70.             RandomGenerate(PageControl1.ActivePage);
  71.           Button1.Caption   := 'Clear';
  72.           SpinEdit1.Visible := False;
  73.           Label1.Visible    := False;
  74.         end;
  75.       end;
  76.     False:
  77.       begin
  78.         while PageControl1.PageCount > 0 do
  79.           PageControl1.ActivePage.Free;
  80.         Button1.Caption   := 'Generate';
  81.         SpinEdit1.Visible := True;
  82.         Label1.Visible    := True;
  83.       end;
  84.   end;
  85. end;
  86.  
  87. procedure TForm1.FormCreate(Sender: TObject);
  88. begin
  89.   PageControl1.Anchors := [akTop, akBottom, akLeft, akRight];
  90. end;
  91.  
  92. end.

speter

  • Sr. Member
  • ****
  • Posts: 475
Re: Drawing to Dynamically Created Form
« Reply #3 on: December 24, 2020, 08:03:41 am »
Have a look at the attached project, it creates some forms and draws different stuff on each...

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

alaa123456789

  • Sr. Member
  • ****
  • Posts: 261
  • Try your Best to learn & help others
    • youtube:
Re: Drawing to Dynamically Created Form
« Reply #4 on: December 24, 2020, 07:22:21 pm »

Wilko500

  • Full Member
  • ***
  • Posts: 180
Re: Drawing to Dynamically Created Form
« Reply #5 on: December 24, 2020, 10:03:49 pm »
Thank you all for your fast responses. You have all given me loads to digest and work with.  That might take a while though.

Special thanks to Speter & Handbook for demo projects.  I think there is quite a bit of learning for me in both.  Peter, your demo project is very close in principle to what I want to do.

I'm going to mark this post as solved because I now have working demos of what I needed.

Thanks
Richard


MacBook Pro mid 2015 with OS Monterey 12.7.6
FPC 3.2.3 Lazarus 3.7
FPC 3.2.2 Lazarus 3.4

 

TinyPortal © 2005-2018