Link to home
Start Free TrialLog in
Avatar of zebada
zebada

asked on

TImage and Resize

I have a component that is inherited from TImage. I place the component on a form and resize it to larger than it's default size. In the resize event I create a new bitmap and assign it to the picture property, then free the bitmap. So far so good.

When the form is created and made visible at runtime the resize event of my component is invoked twice:  Once when the width is set and once when the height is set.

My problem is that during the resize event the ClientWidth and ClientHeight of my component have the default component size as their values, instead of the size that I dragged out when I dropped the control on the form at development time.

How can I, when the form is first shown, know the size of my component?

Avatar of Madshi
Madshi

Umhh... Have you tried overriding the SetBounds method instead of using the resize event? And then do you set the width and height in two different calls? Please call SetBounds(Left, Top, newWidth, newHeight) instead...

Does this help?

Regards, Madshi.
I would limit the resize event (or SetBounds method for that matter, which will likely produce the same result as the event solution) not to react as long as the form is loading. You can check this either by csReading or csLoading in the image's or the form's component state. This avoids unnecessary execution during program start. The override TImage.Loaded and adjust the new image the first time. At this time you already know the correct size.

In the case you don't want to derive a new class from TImage you can also use those methods from the form you are designing. The resize event is already using OnResize, no problem, and the Loaded method also exists for TForm.

Ciao, Mike
Avatar of zebada

ASKER

Madshi,
I think you misunderstood the question. I am not trying to change the size of my component. I am trying to find out what its size is.

Mike,
Thanks, the TImage.Loaded event gives me the component size, thanks.
So Mike, answer for the points.


ASKER CERTIFIED SOLUTION
Avatar of Lischke
Lischke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hmm... Overriding the SetBounds method probably would have given you the correct size, too - but doesn't matter. Nice, that TImage.Loaded works for you...   :-)

Regards, Madshi.

P.S: My suggestion had two parts: Part 1 was the suggestion to override the SetBounds method to get the correct size. Part 2 was the suggestion to call SetBounds to SET the correct size. Okay, seems that Part 2 was not what you needed anyway...   (-:
Avatar of zebada

ASKER

Quick followup question...

csLoading appears to have the desired effect at runtime of restricting what happens in the resize event.

However at design time csLoading doesn't appear to be set, nor does the Loading event get executed (probably 'cause its not streaming in from a form file).

What can I use at desing time to restrict what happens in resize.

All I need to do is to create a new bitmap when the image is a) created and b) resized. Both at runtime and development time.

Avatar of zebada

ASKER

Madshi,

How would I use (override) the SetBounds method to "get the correct size"?

Something like this:

  public
    procedure SetBounds(ALeft, ATop, AWidth, AHeight: integer); override;

procedure TYourComponent.SetBounds(ALeft, ATop, AWidth, AHeight: integer);
begin
  inherited SetBounds(ALeft, ATop, AWidth, AHeight);
  // here you should have the correct size...
end;

Hmmm... I didn't test it. It's just an idea and written out of my mind...

Regards, Madshi.
zebada,

Loaded gets also called at design time with one exception: When the component is created the very first time (when you dropped it onto the form). In this case you will have no choice than to listen to a resize method (SetBounds, OnResize) in order to adjust your component. This is also necessary for all other size changes after the component has been created loaded with the stored values. So you need that anyway.

Madshi,

yep, your code is okay. We are using this kind of sizing notification all the time here ...

Ciao, Mike