Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2016-11

Exception in OnResize event handler

12 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jürgen Hestermann

In one of my Windows programs I use a splitter to separate two VirtualTreeView components on a Form. The relation of the heights of these two VirtualTreeView components should be constant even if the height of the Form changed.
To achieve this, the two VirtualTreeView components are anchored to the splitter and I use the OnResize event to move the splitter in case the height of the form has changed.

This works well except for one very special case:
If I type (on Windows 8.1) Windows key + arrow down three times and then go to the (now invisible program) with Alt+Tab then the program is shown again but consists of the Windows header with the standard icons for minimize, maximize and close only. If I then click on the icon to maximize the program I get an exception in TControl.InvalidatePreferredSize where RaiseLoop is executed. This only happens when I use the above key combinations. I don't know what the exception actually means and how I can avoid this crash. Any ideas?

BTW:
The help in Lazarus about the OnResize event says:

"This event is triggered after the Width, Height, ClientWidth or ClientHeight of the control has changed, and before the LCL sends the new size to the widgetset. The size of the underlying widget (e.g. unit LCLIntf function GetWindowSize and GetClientRect) may differ from the control's Width/Height/ClientRect during OnResize. During autosize the size can change multiple times, but only the last change triggers the OnResize." [...]
"Especially it is not sufficient to write only a TForm.OnResize handler to resize all controls on the form. This is a common bug in Delphi applications."

What does the last sentence mean?
What else is needed to resize all controls on the form?

Reply by email

Mattias Gaertner
to Jürgen Hestermann
Show quoted text 6 lines
Jürgen Hestermann via Lazarus hat am 6. November 2016 um 12:27 geschrieben: [...] "Especially it is not sufficient to write only a TForm.OnResize handler to resize all controls on the form. This is a common bug in Delphi applications." What does the last sentence mean?

For example a TButton in a TGroupBox in a TForm. When the theme changes, the GroupBox client area may change, and the form's client area not. Then form's OnResize is not triggered. You need to set the GroupBox.OnResize event.

What else is needed to resize all controls on the form?

Depending on your layout you may need to set more Control OnResize events.

Mattias

Reply by email

Jürgen Hestermann
to Mattias Gaertner
Mattias Gaertner wrote 5 lines
For example a TButton in a TGroupBox in a TForm. When the theme changes, the GroupBox client area may change, and the form's client area not. Then form's OnResize is not triggered. You need to set the GroupBox.OnResize event.

Then this seems to be unrelated to my problem.
It's not that the OnResize event is *not* triggered but that it is triggered but then leads to the RaiseLoop exception.

BTW:
The wording of the help is misleading.
It says:

"Common mistake: Keep in mind that ClientWidth and ClientHeight can change even when Width, Height stays the same"

but it also says:

"This event is triggered after the Width, Height, ClientWidth or ClientHeight of the control has changed"

When OnResize is triggered even when ClientWidth or ClientHeight has changed then the "Common mistake"-sentence makes no sense to me.

Reply by email

Ondrej Pokorny
to Jürgen Hestermann
Jürgen Hestermann wrote 15 lines
BTW: The wording of the help is misleading. It says: "Common mistake: Keep in mind that ClientWidth and ClientHeight can change even when Width, Height stays the same" but it also says: "This event is triggered after the Width, Height, ClientWidth or ClientHeight of the control has changed" When OnResize is triggered even when ClientWidth or ClientHeight has changed then the "Common mistake"-sentence makes no sense to me.

Why? It's good additional information.

Ondrej

Reply by email

Jürgen Hestermann
to Ondrej Pokorny
Ondrej Pokorny wrote 9 lines
On 06.11.2016 13:03, Jürgen Hestermann via Lazarus wrote: > The wording of the help is misleading. > It says: > "Common mistake: Keep in mind that ClientWidth and ClientHeight can change even when Width, Height stays the same" > but it also says: > "This event is triggered after the Width, Height, ClientWidth or ClientHeight of the control has changed" > When OnResize is triggered even when ClientWidth or ClientHeight has changed then > the "Common mistake"-sentence makes no sense to me. Why? It's good additional information.

The sentence

"Common mistake: Keep in mind that ClientWidth and ClientHeight can change even when Width, Height stays the same"

makes me think that the event is not triggered when *Client*Width/-Height changes but only when Width/Height changes (why else would there be a "common mistake"?) but actually it is triggered in both cases.

Reply by email

Ondrej Pokorny
to Jürgen Hestermann
Jürgen Hestermann wrote 24 lines
Am 2016-11-06 um 16:08 schrieb Ondrej Pokorny via Lazarus: > On 06.11.2016 13:03, Jürgen Hestermann via Lazarus wrote: >> The wording of the help is misleading. >> It says: >> "Common mistake: Keep in mind that ClientWidth and ClientHeight can change even when Width, Height stays the same" >> but it also says: >> "This event is triggered after the Width, Height, ClientWidth or ClientHeight of the control has changed" >> When OnResize is triggered even when ClientWidth or ClientHeight has changed then >> the "Common mistake"-sentence makes no sense to me. > Why? It's good additional information. The sentence "Common mistake: Keep in mind that ClientWidth and ClientHeight can change even when Width, Height stays the same" makes me think that the event is not triggered when *Client*Width/-Height changes but only when Width/Height changes (why else would there be a "common mistake"?) but actually it is triggered in both cases.

The sentence doesn't say anything about when OnResize is triggered so it doesn't make me think anything about OnResize triggers.

Don't be like a woman who always finds much more meanings in her husband's words than he had in mind, all leading to problems :)

Ondrej

Reply by email

Jürgen Hestermann
to Ondrej Pokorny: The sentence doesn't say anything about when OnResize is triggered so it doesn't make me…
Ondrej Pokorny wrote
> The sentence > "Common mistake: Keep in mind that ClientWidth and ClientHeight can

change even when Width, Height stays the same"

> makes me think that the event is not triggered when

*Client*Width/-Height changes

> but only when Width/Height changes (why else would there be a

"common mistake"?)

> but actually it is triggered in both cases. The sentence doesn't say anything about when OnResize is triggered so

it doesn't make me think anything about OnResize triggers.

Then I don't know what the text is warns but does not say against what.

Reply by email

zeljko
to Jürgen Hestermann
Jürgen Hestermann wrote 14 lines
Am 2016-11-06 um 16:39 schrieb Ondrej Pokorny via Lazarus: >> The sentence >> "Common mistake: Keep in mind that ClientWidth and ClientHeight can change even when Width, Height stays the same" >> makes me think that the event is not triggered when *Client*Width/-Height changes >> but only when Width/Height changes (why else would there be a "common mistake"?) >> but actually it is triggered in both cases. > The sentence doesn't say anything about when OnResize is triggered so it doesn't make me think anything about OnResize triggers. Then I don't know what the text is about at all. It warns but does not say against what.

It's against thinking that OnResize triggers only when width or/and height is changed. eg. let's assume that you have TCustomControl on form (with scrollbars), when theme is changed it is possible that vscrollbar width is changed from 20 to 22, so TCustomControl clientWidth is changed too since clientRect area is smaller now...OnResize() is triggered too.

zeljko

Reply by email

Mattias Gaertner
to Jürgen Hestermann: Then this seems to be unrelated to my problem. It's not that the OnResize event is *not*…
Show quoted text 5 lines
Jürgen Hestermann via Lazarus hat am 6. November 2016 um 13:03 geschrieben: [...] Then this seems to be unrelated to my problem. It's not that the OnResize event is *not* triggered but that it is triggered but then leads to the RaiseLoop exception.

Yes. I guess so too.

Show quoted text 12 lines
BTW: The wording of the help is misleading. It says: "Common mistake: Keep in mind that ClientWidth and ClientHeight can change even when Width, Height stays the same" but it also says: "This event is triggered after the Width, Height, ClientWidth or ClientHeight of the control has changed" When OnResize is triggered even when ClientWidth or ClientHeight has changed then the "Common mistake"-sentence makes no sense to me.

Well, it is rare that ClientWidth changes without Width changing, so it is common that programmers forget about ClientWidth. That's why I had to add a note.

Mattias

Show quoted text 7 lines
-- _______________________________________________ Lazarus mailing list http://lists.lazarus-ide.org/listinfo/lazarus

Reply by email

Jürgen Hestermann

I did a reply but then saw that this email was
not sent via Lazarus mailing list
but directly from zeljko to me.
I therefore forward it again to the list.

-------- Weitergeleitete Nachricht --------
Betreff: Re: [Lazarus] Exception in OnResize event handler Datum: Mon, 7 Nov 2016 16:07:24 +0100
Von: Jürgen Hestermann
An: zeljko

zeljko wrote
> Then I don't know what the text is about at all. > It warns but does not say against what. It's against thinking that OnResize triggers only when width or/and height is changed.

But that's the first sentence:

"This event is triggered after the Width, Height, ClientWidth or ClientHeight of the control has changed,..."

So it's already all said in the first paragraph.
Why repeat the same in convoluted and wrong wording? Reading the same in other words lets me think that there is something else to discover which I do just not get now. This text

"Common mistake: Keep in mind that ClientWidth and ClientHeight can change even when Width, Height stays the same. For example when the theme changes, the Width and Height remain the same, but the changed frame reduces the ClientWidth and ClientHeight. This does not happen that often under Windows, but it happens quite often on other platforms. Especially it is not sufficient to write only a TForm.OnResize handler to resize all controls on the form. This is a common bug in Delphi applications. "

should be reworded more clearly.
It starts with "Common mistake:" but then does
not tell about this mistake but only explains what has been said above already. So what is done wrongly often?

And telling me that size changes of components within the Form are not triggered by OnResize of the Form is somewhat confusing (because it's a banality). Therefore I was under the impression that I had to do additional things because I only used OnResize of my Form.
Especially, because the last sentence is wrong.
Of course can it be sufficient to resize all controls on a form within a TForm.OnResize handler only.
I can simply reduce (or expand) sizes of the components in relation to the ClientHeight/-Width change by portioning the remaining space.
But reading the text made me insecure about such coding.

If the facts should be repeated in more detail I would write the text above like this:

"Keep in mind:
ClientWidth and ClientHeight can change even when Width, Height stay the same. For example when the theme changes, the Width and Height remain the same, but the changed frame reduces ClientWidth and ClientHeight. This does not happen that often under Windows, but it happens quite often on other platforms."

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20161107/8ca47294/attachment.html>

Reply by email