Recent

Author Topic: Image creation at runtime (SOLVED!)  (Read 834 times)

Middlecope

  • Jr. Member
  • **
  • Posts: 65
Image creation at runtime (SOLVED!)
« on: December 08, 2019, 03:44:00 pm »
Can I show an image on a form, that was created at runtime?
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6. uses
  7.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  8.   ExtCtrls; {ExtCtrls for TImage definition}
  9. type
  10.   { TForm1 }
  11.   TForm1 = class(TForm)
  12.     procedure FormCreate(Sender: TObject);
  13.   private
  14.   public
  15.   end;
  16. var
  17.   Form1: TForm1;
  18.   Plaat : TImage;
  19. implementation
  20. {$R *.lfm}
  21. { TForm1 }
  22.  
  23. procedure TForm1.FormCreate(Sender: TObject);
  24. begin
  25.   Plaat:= TImage.Create(Form1);
  26.   Plaat.Color:= clWhite;
  27.   Plaat.Width:= 200; Plaat.Height:= 100;
  28.   Plaat.Top:= 10; Plaat.Left:= 30;
  29.   Plaat.Canvas.Pen.Width:= 3;
  30.   Plaat.Canvas.Font.Size:= 18;
  31.   Plaat.Canvas.TextOut(10,10,'Te lezen?');
  32.   Plaat.Visible:= TRUE;
  33. end;
  34. end.
If I start the program the image doesn't show up.
Any idea how to show the image on the Form????
Thanks for attention
« Last Edit: December 09, 2019, 07:59:19 pm by Middlecope »

zeljko

  • Hero Member
  • *****
  • Posts: 1144
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Image creation at runtime
« Reply #1 on: December 08, 2019, 03:57:52 pm »
You're missing Plaat.Parent := Form1;

Middlecope

  • Jr. Member
  • **
  • Posts: 65
Re: Image creation at runtime
« Reply #2 on: December 08, 2019, 04:26:59 pm »
Thanks for the quick reply.
Where should I put it in the program?

winni

  • Hero Member
  • *****
  • Posts: 1602
Re: Image creation at runtime
« Reply #3 on: December 08, 2019, 04:36:10 pm »
Hi!

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Plaat:= TImage.Create(Form1);
  4.   Plaat.Parent := Form1; // <==== here
  5.   Plaat.Color:= clWhite;
  6.   Plaat.Width:= 200; Plaat.Height:= 100;
  7.   Plaat.Top:= 10; Plaat.Left:= 30;
  8.   Plaat.Canvas.Pen.Width:= 3;
  9.   Plaat.Canvas.Font.Size:= 18;
  10.   Plaat.Canvas.Brush.Color := clWhite; //<=====
  11.   Plaat.Canvas.FillRect(0,0,Plaat.Width, Plaat.height);  // <=======
  12.   Plaat.Canvas.TextOut(10,10,'Te lezen?');
  13.   Plaat.Visible:= TRUE;
  14. end;
  15.  

And you should initialize the canvas with white (or whatever) color as show above.

Winni

Middlecope

  • Jr. Member
  • **
  • Posts: 65
Re: Image creation at runtime
« Reply #4 on: December 08, 2019, 09:10:22 pm »
Thanks for the advice but still nothing to see.
I like to implement a lot of images on 1 form.
Code: Pascal  [Select][+][-]
  1. TYPE
  2. TPlaat = ARRAY[1..10] OF TImage;
The code in the first post was to find out the basics.
Any suggestions how to do it are welcomeare welcome.

wp

  • Hero Member
  • *****
  • Posts: 7356
Re: Image creation at runtime
« Reply #5 on: December 08, 2019, 09:42:35 pm »
Thanks for the advice but still nothing to see.
winni's code is working perfectly (although I highly discourage using the form variable inside the form's code - better use "Self" instead of "Form1"). Is the FormCreate method assigned to the OnCreate event of the form?

I like to implement a lot of images on 1 form.
Code: Pascal  [Select][+][-]
  1. TYPE
  2. TPlaat = ARRAY[1..10] OF TImage;
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;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormCreate(Sender: TObject);
  16.   private
  17.     Plaat: array[1..10] of TImage;
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.FormCreate(Sender: TObject);
  33. const
  34.   W = 200;
  35.   H = 100;
  36.   DIST = 10;
  37. var
  38.   i: Integer;
  39.   x, y: Integer;
  40. begin
  41.   x := DIST;
  42.   y := DIST;
  43.   for i := 1 to 10 do
  44.   begin
  45.     Plaat[i] := TImage.Create(self);
  46.     Plaat[i].Parent := Self;
  47.     Plaat[i].Color:= clWhite;
  48.     Plaat[i].Width:= W;
  49.     Plaat[i].Height:= H;
  50.     Plaat[i].Top:= y;
  51.     Plaat[i].Left:= x;
  52.     Plaat[i].Canvas.Pen.Width:= 3;
  53.     Plaat[i].Canvas.Font.Size:= 18;
  54.     Plaat[i].Canvas.Brush.Color := clWhite;
  55.     Plaat[i].Canvas.FillRect(0,0,Plaat[i].Width, Plaat[i].Height);
  56.     Plaat[i].Canvas.TextOut(10,10,'Te lezen?');
  57.     x := x + W + DIST;
  58.     if x > 2 *(W+DIST) then  // two images per row
  59.     begin
  60.       y := y + H + DIST;
  61.       x := DIST;
  62.     end;
  63.   end;
  64.   Width := 3*DIST + 2*W;
  65.   Height := 6*DIST + 5*H;
  66. end;
  67.  
  68. end.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Middlecope

  • Jr. Member
  • **
  • Posts: 65
Re: Image creation at runtime
« Reply #6 on: December 09, 2019, 02:46:20 pm »
Thanks for all the work.
I imported your unit and gave it a try
Lazarus 1.8.2 FPC 3.04 on Linux (OpenSUSE Leap)  I see an empty form
On WIN 10 I also only see an empty form
How to debug?

wp

  • Hero Member
  • *****
  • Posts: 7356
Re: Image creation at runtime
« Reply #7 on: December 09, 2019, 05:17:21 pm »
[...] I imported your unit [...]
What does this mean? I already asked: Did you assign the FormCreate event handler to the OnCreate event of the form? This means: after you pasted the code from the forum into the unit you must go to the object inspector, find the OnCreate event on page "Events" and click on the '...' button - then the code editor should jump to the FormCreate code, and "FormCreate" should appear in the object inspector next to "OnCreate" to indicate that the method is linked to the event - see screenshot. If this has not been made the FormCreate method is not called - you can check this by adding a breakpoint to any line of this procedure (click on the editor gutter of the line to get the breakpoint): program execution must stop at this line.

Try also the attached complete project. It is working - if not, something is very wrong on your system. I checked it in Win 10 with Laz trunk/fpc 3.0.4 and Laz 1.8.4 / fpc 3.04, and in Linux Mint 18.1 with Laz trunk / fpc3.0.4.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Middlecope

  • Jr. Member
  • **
  • Posts: 65
Re: Image creation at runtime
« Reply #8 on: December 09, 2019, 07:58:41 pm »
Yes That did the work.
It works fine now. :D
Thanks to all the hero members I can go on with my work
Thanks Teunis

 

TinyPortal © 2005-2018