I remember the days when I programmed in assembler and managed to work very fast having complete control of my programs and not endlessly banging my head on the windows wall when dealing with the Microsoft side of my software.

Below is an example of some code that is simply resizing a timage component sited on a tpanel in response to a redraw event.

With the proportional property in the timage component 'main_image' set to true, all I have to do is specify the height or the width of the image and Delphi will calculate the width and height respectively based on the aspect ratio.

However, I want to also set the top and left properties so that the image is centred in the tpanel - the centred property seems to have no effect.

with main_image do
     if (width>0) and (height>0) then
      if t_width>panel_width then
[1]   else if t_height>panel_height then
[2]       height:=panel_height;
[3]       t_width:=width;
      left:=(panel_width-t_width) div 2;
      top:=(panel_height-t_height) div 2;

In the situation when the image is taller[1] than the panel 'panel_height' I set the height[2] property of the image to the same height as the panel.

As you can see, in order to calculate the 'left' property of the image I need to know the new value for the width[3] of the image.

The result however always is the same, even the value for the width of the image in the debug watch never changes when it is clear the width is reducing in size when I squash the form by dragging the top of the window down. Don't also forget, I am changing the height property of the component, not the image itself which is resized inside the component because the proportional property is set to true.

The reason I believe the width[3] property is not immediately accessible is 1: the code is situated in the redraw event method and 2: I have to seal off the event to prevent recursion due to changes made to the properties of the image component.

This code is also not exactly complete. ATM it assumes the width of the image is always wider than the height, but that can be dealt with later. All I am asking for is a way to make the code above work so that I can reposition the left/top properties of the image based on changes made to the height or width.


procedure TForm1.FormResize(Sender: TObject);
  Panel1.Width  := (ClientWidth div 100) * 80;
  Panel1.Height := (ClientHeight div 100) * 80;
  Panel1.Left   := (ClientWidth div 2) - (Panel1.Width div 2);
  Panel1.Top    := (ClientHeight div 2) - (Panel1.Height div 2);

  Image1.Width  := (Panel1.Width div 100) * 80;
  Image1.Height := (Panel1.Height div 100) * 80;
  Image1.Left   := (Panel1.Width div 2) - (Image1.Width div 2);
  Image1.Top    := (Panel1.Height div 2) - (Image1.Height div 2);

I shall check the code you have provided, but as I said every time you make any changes to any control on the form the resize event is re-triggered. However, I have found a solution by recording both the newheight and newwidth arguments provided by the canresize event to global variables. I can use them instead of directly accessing the width or height of the timage control when I make a change.


I'm sorry, do not speak English. View property Anchor

Edited 6 Years Ago by Wolfgan: n/a

This article has been dead for over six months. Start a new discussion instead.