Author Topic: Multi-Form Referencing  (Read 855 times)

cdbc

  • Hero Member
  • *****
  • Posts: 2931
    • http://www.cdbc.dk
Re: Multi-Form Referencing
« Reply #15 on: August 02, 2026, 04:49:28 pm »
Hi J-G
I'll bet you didn't see this one coming... \o/\ö/\o/
Have a 'LookSee' at THIS repo. (the zip is just a smidgen over 500Kb.)
Regards Benny

edit: Added a screeshot...
« Last Edit: August 02, 2026, 05:00:11 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

J-G

  • Hero Member
  • *****
  • Posts: 1216
Re: Multi-Form Referencing
« Reply #16 on: August 02, 2026, 06:43:13 pm »
Hi J-G
I'll bet you didn't see this one coming... \o/\ö/\o/
Have a 'LookSee' at THIS repo. (the zip is just a smidgen over 500Kb.)
Regards Benny

edit: Added a screeshot...

You're not wrong there Benny 🤪

There are many (MANY) lessons to be learned from a study of that small program - some of which have already found a home 😄   -   whether they will still be with me tomorrow is another story!  -  I hope so but (as I'm sure you will appreciate) many new questions are already being formed  -  even though the embedded comments are very useful.

It has long puzzled me as to how there can be many procedures/functions with ostensibly the same name - -  Reading through your code I came to realize that the ARGUMENTS can (do?) differentiate them. 

To force a program to close I've generally used Application.Terminate - specifically at the end of a Form 'OnClose' event (which does any 'tidying up') so seeing your call to simply 'Close' is interesting - - -  stepping through, I see that BeforeDestruction does the tidying up (Clear/Free) but I cannot see such an 'event' in the object inspector. Just as I cannot see AfterConstruction.

None of this advances my current project (yet) - I have much work to do before that comes to fruition - but it's well worthwhile nonetheless and I am already looking at the TBitmap 'thing'.  As an example though, your showing the images at their natural size after a [Form.ReSize] is not what I need. if the Form is enlarged, then my image must also be enlarged in the same proportion. Likewise with a reduction.  I'm sure that is just a matter of code adjustment  -  My 'Plot' already handles that, I just need to change the 'Canvas' to a TBitmap.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

cdbc

  • Hero Member
  • *****
  • Posts: 2931
    • http://www.cdbc.dk
Re: Multi-Form Referencing
« Reply #17 on: August 02, 2026, 07:47:43 pm »
Hi
1)
Quote
It has long puzzled me as to how there can be many procedures/functions with ostensibly the same name - -  Reading through your code I came to realize that the ARGUMENTS can (do?) differentiate them.
   A: Yes the argumentlists set them apart, FPC is smart \o/

2)
Quote
I see that BeforeDestruction does the tidying up (Clear/Free) but I cannot see such an 'event' in the object inspector. Just as I cannot see AfterConstruction.
   A: They're -not- events, but virtual methods of 'TObject', we just override
   them, quicker than events...

3)
Quote
I just need to change the 'Canvas' to a TBitmap.
   A: Ahum, what's wrong with 'TBitmap.Canvas'? It'll do mighty fine...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

J-G

  • Hero Member
  • *****
  • Posts: 1216
Re: Multi-Form Referencing
« Reply #18 on: August 02, 2026, 09:45:11 pm »
Well!  -   I've given up for today  :(

I've tried myriad different options :   drawing the TBitmap on the TPanel.Canvas, on a  TImage.Canvas, on a TPaintBox.Canvas etc. all without any benefit  -  ie. Form.ReSize ALWAYS clears the canvas at the end of the draw (such as Output.PaintBox.Canvas.Draw(0,0,D_Bmp); )

I do see a brief 'flash' of the image on occasion just prior to the process completing, which tells me that the image HAS been drawn.

I haven't fully investigated the behaviour of Form.ReSize when using a TPaintBox but that did seem to interfere with the ReSize (restricting it ?), and the TImage backgound is always black even though I've set its brush to clMoneyGreen.

The lesson learned seems to be that even using TBitmap - the image doesn't persist after a Form.ReSize. :o

There must be something that I've still not understood (I still haven't invoked an 'OnPaint' Event ? could that be the 🗝️ ?)  but I'll give my brain a rest and see if I can fathom what needs to be done tomorrow.

It seems that I don't have a virtual Uncle Bob 🤣🤣
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

jamie

  • Hero Member
  • *****
  • Posts: 7902
Re: Multi-Form Referencing
« Reply #19 on: August 02, 2026, 11:25:05 pm »
The OnSize Event should not be used to modify the form it came from, that will cause a loop and most likely generate a flashing form with artifacts on it.
years ago, the OnSize Event used to work correctly because it's supposed to be generated when the user resizes the form, however, during Delphi early years, they decided to have that triggered during any method of resizing and that causes issues.

 If you are trying to maintain an aspect ratio there are ways to do this with smoothly operating effects and that is a specific windows message you can process that allows you to make changes to values that are getting ready to go into effect, sort of intercepting the size change.

 Let me know if you are interested, otherwise I'll go back to sleep;

Btw, using the OnPaint even should be the correct time to paint your updates.

Jamie

The only true wisdom is knowing you know nothing

cdbc

  • Hero Member
  • *****
  • Posts: 2931
    • http://www.cdbc.dk
Re: Multi-Form Referencing
« Reply #20 on: August 03, 2026, 09:17:42 am »
Hi
· Draw on the internal bitmap
· In the O N P A I N T-handler the
  canvas now handles the scaling, when drawing the bitmap onto itself \o/
· It doesn't matter if you paint a photo or a technical drawing, it scales...
:: I changed 1 line in the O N P A I N T-handler, to achieve this!!!

Stop trying "useless" and do what you're shown and told!
Code: Pascal  [Select][+][-]
  1. unit view.main;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6. (* Copyright ©2026 Benny Christensen a.k.a. cdbc,    *)
  7. (* released as PUBLIC DOMAIN - Use at your own risk! *)
  8. uses
  9.   Classes, ExtCtrls,StdCtrls,SysUtils, Forms, Controls, Graphics, Dialogs,
  10.   cdbc.istreams;
  11.  
  12. type
  13.   { TfrmMain }
  14.   TfrmMain = class(TForm)
  15.     Bevel1: TBevel;
  16.     btnClose: TButton;
  17.     btnMagic: TButton;
  18.     gbxResult: TGroupBox;
  19.     pnlImg: TPanel;
  20.     pnlTop: TPanel;
  21.     procedure btnCloseClick(Sender: TObject);
  22.     procedure btnMagicClick(Sender: TObject);
  23.     procedure pnlImgPaint(Sender: TObject);
  24.   private
  25.     fArrayImg: array[0..2] of IMemoryStream;
  26.     fBmp: TBitmap;
  27.     fDrawing: boolean;
  28.     procedure DrawByIndex(const anIdx: ptrint);
  29.   public
  30.     procedure AfterConstruction; override;
  31.     procedure BeforeDestruction; override;
  32.   end;
  33.  
  34. var
  35.   frmMain: TfrmMain;
  36.  
  37. implementation
  38.  
  39. {$R *.lfm}
  40. resourcestring
  41.   rsCaption = '(O)ff (S)creen (P)ainting';
  42.  
  43. { TfrmMain }
  44. procedure TfrmMain.btnCloseClick(Sender: TObject);
  45. begin
  46.   Close;
  47. end;
  48.  
  49. procedure TfrmMain.btnMagicClick(Sender: TObject);
  50. const lidx: ptrint = 0; { static variable }
  51. begin
  52.   DrawByIndex(lidx);
  53.   inc(lidx);
  54.   if lidx > 2 then lidx:= 0;
  55. end;
  56.  
  57. procedure TfrmMain.pnlImgPaint(Sender: TObject);
  58. begin
  59.   if fDrawing then exit; { first we check our /lock/, a missed beat doesn't kill us }
  60.   pnlImg.Canvas.CopyMode:= cmSrcCopy; { actually the default behaviour }
  61. ///  pnlImg.Canvas.StretchDraw(rect(0,0,315,234),fBmp); { fixed image-sizes regardsless of form-size }
  62.   { if the user resizes the form, we scale with the new size :o) }
  63.   pnlImg.Canvas.StretchDraw(rect(0,0,pnlImg.Width,pnlImg.Height),fBmp);
  64. end;
  65.  
  66. procedure TfrmMain.DrawByIndex(const anIdx: ptrint);
  67. begin
  68.   fDrawing:= true;     { we don't want the canvas blitting while we're drawing }
  69.   fBmp.Clear;               { not entirely sure, this is necessary, but hey... }
  70.   fBmp.LoadFromStream(fArrayImg[anIdx].AsTStream);  { preloaded to streamarray }
  71.   fArrayImg[anIdx].Position:= 0;  { reset after use, or we'll see a black rect }
  72.   fDrawing:= false;  { release our lock and notify panel about a redraw needed }
  73.   pnlImg.Invalidate;                   { sends a WM_PAINT message to our panel }
  74. end;
  75.  
  76. procedure TfrmMain.AfterConstruction;
  77. begin
  78.   inherited AfterConstruction; { here we'll preload the images - for speed :o) }
  79.   fArrayImg[0]:= CreMemStreamFromFile('img'+PathDelim+'316x235_Novigrad_Castle.bmp');
  80.   fArrayImg[0].Position:= 0; { muy importante! }
  81.   fArrayImg[1]:= CreMemStreamFromFile('img'+PathDelim+'316x235_Novigrad_Sunset.bmp');
  82.   fArrayImg[1].Position:= 0; { muy importante! }        
  83.   fArrayImg[2]:= CreMemStreamFromFile('img'+PathDelim+'316x235_Novigrad_Wauw.bmp');
  84.   fArrayImg[2].Position:= 0; { muy importante! }
  85.   fBmp:= TBitmap.Create; { create once and reuse :: -much- quicker \o/ }
  86.   fBmp.SetSize(316,235); { i've created the photos, so i know :: otherwise... }
  87.   Caption:= rsCaption;
  88. end;
  89.  
  90. procedure TfrmMain.BeforeDestruction;
  91. begin
  92.   fArrayImg[0]:= nil; fArrayImg[1]:= nil; fArrayImg[2]:= nil; { com-objects }
  93.   fBmp.Clear; fBmp.Free; { class }
  94.   inherited BeforeDestruction;
  95. end;
  96.  
  97. end.
  98.  
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

J-G

  • Hero Member
  • *****
  • Posts: 1216
Re: Multi-Form Referencing
« Reply #21 on: August 03, 2026, 09:25:42 am »
Morning All, 

It is always challenging to comunicate one intentions (as far as what is envisaged by the programmer wishes to achieve). It's also something of an issue to provide a fully compileable code when there are 'personal' Units used.

It maybe that all I need to do to make a Project Compilable is to [Publish] it but add the various Unit .PAS files to the .ZIP  - but no one has ever confirmed this thought. There is nothing 'confidential' in my Personal Units, they are simply collections of procedures/function which I've found over time to be useful in multiple projects, giving me a commonallity. I suspect that is a 'normal' process for most of us.

These thought gave rise to me considering posting a fully compiled (ie. working) program (as a .ZIP since .EXE files cannot be attached) so that anyone could at least see what I'm attempting.

Currently the code is in a state of 'test' where I have multiple options as to the component on which the output is displayed, so I'd first strip it back to the working state before I tried to solve the 'no-show' on ReSize.

Which method would be the most effective?  Published + Unit .PAS files or a zipped .EXE ?

!!  SORRY @Benny  !!  -  I've just seen your new post  :-[
I'll read that properly to see if I get a better understanding.

I'll also respond to @Jamie

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

cdbc

  • Hero Member
  • *****
  • Posts: 2931
    • http://www.cdbc.dk
Re: Multi-Form Referencing
« Reply #22 on: August 03, 2026, 09:40:55 am »
Good morning J-G
Q: "Which method would be the most effective?  Published + Unit .PAS files or a zipped .EXE ?"
A: Published + Unit .PAS files, just make a copy of your units in a subfolder under 'published' and zip the lot...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

J-G

  • Hero Member
  • *****
  • Posts: 1216
Re: Multi-Form Referencing
« Reply #23 on: August 03, 2026, 09:44:43 am »
The OnSize Event should not be used to modify the form it came from, that will cause a loop and most likely generate a flashing form with artifacts on it.
years ago, the OnSize Event used to work correctly because it's supposed to be generated when the user resizes the form, however, during Delphi early years, they decided to have that triggered during any method of resizing and that causes issues.

 If you are trying to maintain an aspect ratio there are ways to do this with smoothly operating effects and that is a specific windows message you can process that allows you to make changes to values that are getting ready to go into effect, sort of intercepting the size change.

 Let me know if you are interested, otherwise I'll go back to sleep;

Btw, using the OnPaint even should be the correct time to paint your updates.

Jamie

Thanks for your input @Jamie, I'm always interested in anything anyone has to offer.

The Event I'm using is 'OnReSize' (rather than 'OnSize' which I don't see in the OI) this seems to me to be reasonable, but I take your point that things have evolved over time.

The aspect ratio is no longer an issue, that part is fully operational; including the control of Max/Mix/Screen position - and I now also understand that I can probably (certainly?) achieve my goal without needing to call the 'Plot' proc after a Form ReSize. (Re: @Benny)

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 1216
Re: Multi-Form Referencing
« Reply #24 on: August 03, 2026, 09:46:38 am »
Good morning J-G
Q: "Which method would be the most effective?  Published + Unit .PAS files or a zipped .EXE ?"
A: Published + Unit .PAS files, just make a copy of your units in a subfolder under 'published' and zip the lot...
Regards Benny

Thanks @Benny  -  I'll do the clean up first   -  and maybe do the corrections you've previously suggested  ::)
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 1216
Re: Multi-Form Referencing
« Reply #25 on: August 03, 2026, 01:45:57 pm »
A: Published + Unit .PAS files, just make a copy of your units in a subfolder under 'published' and zip the lot...
Regards Benny

Right!  -  I'm at a 'Half-way-house'  -  I have been able to fathom the workflow to incorporate an OnPaint event and included some parts of the 'Plot' routine within that.

It's still not 'fully formed' but to provide a better understanding about my intentions and the differences I've added a Button to make the Bitmap (and OnPaint) active or not.

With the bitmap 'Active' I see a black background to the output (rather than clMoneyGreen). So I still haven't understood that aspect.

The issue about the personal Units files seems an odd one - but I think I have a reason  8) -  I did make a copy of them all and intended to add them to the .ZIP file but when I opened the .ZIP - - - - - they were all there !!!

The fact that it appears that they are not available I suspect is because I keep them in a separate sub-folder and that is added to the [Paths] Option in the Project Options under 'Other Unit Files (-Fu):'.

To foil any problem that the UnZip program may create I've made a separate .Zip of just those files.

Because I use two screens, the option to have the [Output] Form 'Floating' or 'Docked' gives the flexability to have a larger display on the second screen.

I'm getting there, but still need guidance so I hope that compilation is posible so that you can clearly see what I do.

Time for a late 'Breakfast' !
« Last Edit: August 03, 2026, 06:34:17 pm by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 1216
Re: Multi-Form Referencing
« Reply #26 on: August 03, 2026, 03:34:21 pm »
Oooops -  I've just remembered that I've modified the Canvas.inc file line 690 from LineTo(x1,y1)  to  MoveTo(x1,x2) to eliminate the line drawn from the 'centre' of the Arc.

This will affect what you see when the Reuleaux lines are drawn (Odd N° of Sides < 13).

It's not material to the problem but it's better that you know.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

cdbc

  • Hero Member
  • *****
  • Posts: 2931
    • http://www.cdbc.dk
Re: Multi-Form Referencing
« Reply #27 on: August 03, 2026, 04:02:31 pm »
Hi J-G
Sorry mate someone else will have to look at your project, because in two hours I'm flying on holiday with the Missus \o/
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

J-G

  • Hero Member
  • *****
  • Posts: 1216
Re: Multi-Form Referencing
« Reply #28 on: August 03, 2026, 05:42:20 pm »
No matter Benny, Have a good journey.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

WonderNomad

  • Newbie
  • Posts: 2
Re: Multi-Form Referencing
« Reply #29 on: August 06, 2026, 11:59:12 am »
Add Poly's unit to the uses clause of display.pas (under implementation to avoid circular references), and you'll be able to access its components directly.

 

TinyPortal © 2005-2018