Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help understanding what event is calling what

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB
In my map form I have the below procedures.
In order to try to solve another bug, I added a counter to the form and an inc(counter) to the beginning of the DrawHeadings procedure.
When I run my app, default checked is largemap and squares.
If I click HeadingsRadio, then shut the form (called by my main form) the counter is 7.
If I activate the form again, and de-activate, the counter is incremented by 12, every time.

Why is the DrawHeadings procedure being called so many times?

Code:
procedure TMapForm.FormCreate(Sender: TObject);
begin
  counter := 0;
end;

procedure TMapForm.FormDeactivate(Sender: TObject);
begin
showmessage(inttostr(counter));
end;

procedure TMapForm.FormDestroy(Sender: TObject);
begin
BMPbackground.Free;
end;

procedure TMapForm.FormPaint(Sender: TObject);
begin
Canvas.Draw( 0, 32, BMPbackground ); // 32 pixels down for the top panel
if SquaresRadio.Checked then DrawSquares else Drawheadings;
end;

procedure TMapForm.FormShow(Sender: TObject);
begin
  if LargeMapRadio.Checked then LoadLargeMap else LoadSmallMap;
end;

procedure TMapForm.HeadingsRadioClick(Sender: TObject);
begin
DrawHeadings;
end;

procedure TMapForm.SquaresRadioClick(Sender: TObject);
begin
DrawSquares;
end;

procedure TMapForm.SmallMapRadioClick(Sender: TObject);
begin
LoadSmallMap;
end;

procedure TMapForm.LargeMapRadioClick(Sender: TObject);
begin
LoadLargeMap;
end;

procedure TMapForm.LoadSmallMap;
begin
// small map loading stuff cut out for space saving
if SquaresRadio.Checked then DrawSquares else Drawheadings;
end;

procedure TMapForm.LoadLargeMap;
begin
// large map loading stuff cut out for space saving
if SquaresRadio.Checked then DrawSquares else Drawheadings;
end;

Steve (Delphi 2007 & XP)
 
put a break and step through the code, so you can see whats calling what and why.

Aaron
 

thanks Aaron. I did think of that after I posted and went to bed. I've only just discovered using breaks, so I'm still forgetting to use them!


Steve (Delphi 2007 & XP)
 
Delphi help said:
The form component calls Paint in response to the WM_PAINT message (sent when the window needs to be painted) before rendering all controls on the form.

Unless you are specifically overriding this (also this help item doesn't mention ownerdraw) Onpaint can be called in the background loads of times.
But never when you want it to!



Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
OK, I didn't get too much joy with the breakpoints, I think I got into a repainting loop as it went to and fro to IDE.

Anyway, I added counters to all procedures that could call my DrawHeadings proc.

The multiple calls to DrawHeadings are definately coming from FormPaint. The counters for Formpaint and Drawheadings increment in sync.

Can I add to my DrawHeadings proc something similar to the Begin and EndUpdate commands, or FormPaint=nil, to surround my drawing for to loop?

At the moment my test data only has 11 or 12 items to loop thru, but could grow to 100's.
I'm getting at least 1 repaint per data item (kinda figures as I'm drawing something for each one)
I'm concerned all these paint cycles will start to get sluggish.
Also, as I'm resizing form and controls when the user selects map 1 or map 2, I although I have truncated lineto's to not draw past y pixel 32 (bottom of the TPanels) as I resize from large to small, I do get a stray line on the panels. This appears to be a line that was OK on large map being repainted during resizing of the form and controls, as as a result not yet being limited to y > 32.
If there is a begin/end update or similar, I'm hoping I can prevent this.

This is the part my loadsmallmap proc I refer to.
At the top of my form I have Panel1, aligned top.
On that I have panel2 aligned left, then two radios aligned left.
On panel two I have two other radios, aligned right.
This pretty much centers them all:
Code:
MapName := 'europe-small.bmp';
BMPbackground := TBitmap.Create;
BMPbackground.LoadFromFile(Mapname);
ClientWidth := BMPbackground.Width; 
ClientHeight := BMPbackground.Height + 32; 
Panel2.Width := trunc(ClientWidth / 2);

When I tried using a Timage component to load the maps as suggested, and changed the formsize as above, I got real weird effects (mentioned higher in thread).

Steve (Delphi 2007 & XP)
 
Are you calling FormPaint(Sender) anywhere in your code?

If you are, do this instead:
Code:
procedure TMapForm.DoMyPaint;
begin
  Canvas.Draw(0, 32, BMPbackground); // 32 pixels down for the top panel
  if SquaresRadio.Checked then DrawSquares else Drawheadings;
end;

procedure TMapForm.FormPaint(Sender: TObject);
begin
  DoMyPaint
end;
Then wherever you were calling FormPaint(Sender), change it to call DoMyPaint instead. I've found that triggering the OnPaint event can cause recursive calls. This will prevent the "never when you want it to!" that Steve mentioned.

(Just a suggestion (idea) - not researched nor tested.)

Roo
Delphi Rules!
 
Also, as I'm resizing form and controls when the user selects map 1 or map 2,
This is definately triggering FormPaint().

Roo
Delphi Rules!
 

Thanks Roo. No I'm not calling FormPaint anywhere. The only procedures I didn't paste are the actual drawing ones, which don't either.

In the meantime, I've been playing.

I shut my main app and started a new one from fresh to save inheriting any rubbish, and tried using a Timage again, as suggested to me in my other thread.
(The failure I mention higher as 'described above' was in the other thread, oops)

Long story cut short, starting over from scratch and one step at a time, and with google, I seem to have a demo app doing what I wanted all along.

I've yet to integrate my new code into the real app, but should be OK. There is no FormPaint at all (due to the magic self repainting properties of the Timage it seems) so I shouldn't be getting myself into a pickle.

Assuming I can google the statement to clear all that I have drawn on it that is.


Steve (Delphi 2007 & XP)
 
It's amazing how often "starting over from scratch" resolves most headaches. But suggesting that to someone always get you that look of "you must be nuts".

Glad it worked for you!

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top