Dynamically Positioning the Control (C++ 4.0 and later)

Take the following steps to dynamically scale and position the LEAD control when loading an image. This example scales the control to use most of the form's area while preserving the image's aspect ratio.

1. Start with the project that you created in Loading and Displaying an Image.

2. Modify the OnLoadlead procedure so that it appears as follows. For a general explanation of how images are positioned and scaled, refer to Displaying an Image.

// Declare local variables.
float HeightFactor, WidthFactor, HeightAllowed, WidthAllowed;
float Width, Height, Left, Top;
CString ImageFile;

// Load the bitmap. This hard-coded path name may be different on your system.
/******* put a valid image path here **********/
ImageFile = "c:\\lead\\images\\image1.cmp";
/**********************************************/

// Get file information
m_Lead1.GetFileInfo(ImageFile, 0, 0);

// Set the variables used for preserving the aspect ratio.
// Allow for a border of 1/8 of the form size.
// The units of measure do not matter, since we are calculating proportions.
HeightFactor = m_Lead1.GetInfoHeight();
WidthFactor = m_Lead1.GetInfoWidth();
CRect rcWindow;
GetClientRect(rcWindow);
HeightAllowed = rcWindow.Height() * 3.0f / 4;
WidthAllowed = rcWindow.Width() * 3.0f / 4;

// Center the LEAD control on the form, preserving the aspect ratio.
// Check to see if using the maximum width will make the image too tall.
// Set the dimensions based on the result.
if(((WidthAllowed * HeightFactor) / WidthFactor) < HeightAllowed)
{
  Left = rcWindow.Width() / 8.0f;
  Width = WidthAllowed;
  Height = (Width * HeightFactor) / WidthFactor;
  Top = (rcWindow.Height() - Height) / 2;
}
else
{
  Top = rcWindow.Height() / 8.0f;
  Height = HeightAllowed;
  Width = (Height * WidthFactor) / HeightFactor;
  Left = (rcWindow.Width() - Width) / 2;
}
rcWindow.SetRect(0, 0, (int) Width, (int) Height);
rcWindow.OffsetRect((int) Left, (int) Top);
m_Lead1.MoveWindow(rcWindow);

// Set up for handling our own display rectangles.
m_Lead1.SetAutoScroll(FALSE);
m_Lead1.SetAutoSetRects(FALSE);
m_Lead1.SetAutoRepaint(TRUE);

// Set the image display size to match the LEAD control
m_Lead1.SetDstRect(0.0f, 0.0f, m_Lead1.GetScaleWidth(), m_Lead1.GetScaleHeight());
m_Lead1.SetDstClipRect(0.0f, 0.0f, m_Lead1.GetScaleWidth(), m_Lead1.GetScaleHeight());

// Load the bitmap
m_Lead1.Load(ImageFile, 0, 0, 1);