I have a project I am working on where I need to fit the form position and width to the dimensions of the bitmap. The form must be right aligned to the desktop and its height is the screen.height minus the taskbar height. The zoom is set to FitToHeight. Center is True.
In order to set the Form1.Left position I need to figure out how to calculate the distance between the left edge of the form and the left edge of the image within TImageEnView. This distance varies depending on the dimensions of the image that is loaded. I tried using the OffsetX value but OffsetX always returns 0. OffsetX should return the horizontal position where the image is drawn. If Center property is False, OffsetX will be 0, but in this case, Center is true, so OffsetX should provide the distance from the left edge of the form and the left edge of the image, but it does not... it returns 0.
The screenshot below shows the form as it appears after I try to set the Form1.Left and Form1.Width values. The whitespace between the left side of the form and the left side of the image is the value I need to calculate or get from an ImageEnView property.

The screenshot below shows the form as I would like it to appear.

Here is the code I am using... Note that the code is sloppy because I have been trying different ways to adjust the left edge of the form to eliminate the whitespace without success.
function TaskbarHandle: Winapi.Windows.THandle;
begin
Result := Winapi.Windows.FindWindow('Shell_TrayWnd', nil);
end;
function TaskbarBounds: Winapi.Windows.TRect;
var
Data: Winapi.ShellAPI.TAppBarData; // structure receive task bar info
begin
// set up data structure
Data.cbSize := SizeOf(Data);
Data.HWND := TaskbarHandle;
// get bounding rectangle
if Winapi.ShellAPI.SHAppBarMessage(Winapi.ShellAPI.ABM_GETTASKBARPOS, Data) <> 0
then
Result := Data.rc
else
// can't get task bar info: return empty rectangle
FillChar(Result, SizeOf(Result), 0);
end;
procedure TForm1.Open1Click(Sender: TObject);
var
iWidth: Integer;
iLeft: Integer;
iTop: Integer;
iMx: Integer;
iMy: Integer;
iTaskBarHeight: Integer;
iHeight: Integer;
iAspectRatio: double;
iHorzContainerWidth: Integer;
iOffsetX: Integer;
x: integer;
xo: integer;
begin
{Make the form right aligned to the desktop and adjust the form dimensions to fit the image}
if OpenPictureDialog1.Execute then
begin
ImageEnView1.IO.LoadFromFile(OpenPictureDialog1.FileName);
ImageEnView1.Center := True;
{ Set up the form dimensions and position }
if ImageEnView1.IEBitmap.Width <= Screen.Width then
begin
iAspectRatio := ImageEnView1.Bitmap.Width / ImageEnView1.Bitmap.Height;
iHorzContainerWidth := Round(ImageEnView1.Bitmap.Height * iAspectRatio);
iLeft := Screen.Width - iHorzContainerWidth;
iWidth := ImageEnView1.Bitmap.Width;
Form1.Left := iLeft;
Form1.Width := iHorzContainerWidth;
iTop := 0;
iTaskBarHeight := TaskbarBounds.Bottom - TaskbarBounds.Top;
iHeight := Screen.Height - iTaskBarHeight;
Form1.Top := iTop;
Form1.Height := iHeight;
ImageEnView1.FitToHeight;
end
else if ImageEnView1.IEBitmap.Width >= Screen.Width then
begin
{ This part does not work as needed. There is white space between the
left side of the form and the left edge of the image and between the
right side of the form and the right edge of the image. To eliminate
this space, the forms left position needs to be adjusted, but I can
not figure out how to use ImageEn properties to get the width of the
white space to adjust Form1.Left and Form1.Width. }
iTaskBarHeight := TaskbarBounds.Bottom - TaskbarBounds.Top;
iHeight := Screen.Height - iTaskBarHeight;
iAspectRatio := ImageEnView1.Bitmap.Width / ImageEnView1.Bitmap.Height;
iHorzContainerWidth := Round(iHeight * iAspectRatio);
iLeft := Screen.Width - iHorzContainerWidth;
iWidth := ImageEnView1.Bitmap.Width;
iTop := 0;
Form1.Top := iTop;
Form1.Height := iHeight;
ImageEnView1.FitToHeight;
iOffsetX := Screen.Width - iHorzContainerWidth;
xo := ImageEnView1.OffsetX;
Form1.Left := iOffsetX;
x := iHorzContainerWidth - ImageEnView1.ExtentX;
Form1.Width := Screen.Width - Form1.Left;
end;
end;
end;
Any suggestions on how to accomplish this would be appreciated. A small demo that I am using to test this can be downloaded here:
http://www.imageen.com/ieforum/attach/w2m/20146251176_AlignFormRight.zip
Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development