Fitting an Image to a Window
If you are using the high-level LBitmapWindow (or derived) class, you can use the LBitmapWindow::SetZoomMode function to zoom an image in the following manners:
ZOOM_FIT |
to fit the bitmap in the window while maintaining the aspect ratio. |
ZOOM_STRETCH |
to stretch the bitmap to the window. |
ZOOM_NORMAL |
to display the bitmap in its normal size. |
ZOOM_FITWIDTH |
to fit the bitmap to the width of the window. |
ZOOM_FITHEIGHT |
to fit the bitmap to the height of the window. |
You can also use LBitmapWindow::FitToParent to fit the class object's window to it's parent.
If you are not using LBitmapWindow, to fit an image to a window, you only have to calculate a display rectangle that fits the window's client area, while preserving the image's aspect ratio. The following code snippets show how this can be done.
LBitmapBase LeadBitmap; //bitmap
for the loading image
RECT rWndSize;
RECT rClientSize;
RECT rLeadDest;
L_INT WidthAllowed;
L_INT HeightAllowed;
L_INT WidthFactor;
L_INT HeightFactor;
L_INT Left, Top, Width, Height;
L_INT nRet;
/* Load the LEAD DLLs */
LBase LeadBase;
LeadBase.LoadLibraries(LT_ALL_LEADLIB);
/* Load an image */
nRet = LeadBitmap.Load(TEXT("v:\\images\\eagle.cmp"), 8);
/* Get the current window size and client area */
::GetWindowRect(this->m_hWnd, &rWndSize);
::GetClientRect(this->m_hWnd,&rClientSize);
/* Use this to fit the displayed image in the client area. */
WidthAllowed = rClientSize.right;
HeightAllowed = rClientSize.bottom;
WidthFactor = LeadBitmap.GetWidth();
HeightFactor = LeadBitmap.GetHeight();
/* See if using the maximum width will make the image too tall */
if(MulDiv(WidthAllowed, HeightFactor, WidthFactor) < HeightAllowed)
{ /* Use the maximum width, and calculate the height and top values */
Left = 0;
Width = WidthAllowed;
Height = MulDiv(Width, HeightFactor, WidthFactor);
Top = 0;
}
else
{
/* Use the
maximum height, and calculate the width and left values */
Top = 0;
Height = HeightAllowed;
Width = MulDiv(Height, WidthFactor, HeightFactor);
Left = 0;
}
RECT rc;
/* Set the source rectangle to use the whole bitmap */
SetRect(&rc, 0, 0, LeadBitmap.GetWidth(), LeadBitmap.GetHeight());
LeadBitmap.SetSrcRect(&rc);
/* Set the source clipping rectangle to use the whole bitmap */
LeadBitmap.SetClipSrcRect(&rc);
/* Set the destination rectangles */
SetRect(&rLeadDest, Left, Top, Width, Height);
LeadBitmap.SetDstRect(&rLeadDest);
LeadBitmap.SetClipDstRect(&rLeadDest);
/* paint the image */
HDC hDC = ::GetDC(this->m_hWnd);
LeadBitmap.Paint()->SetDC(hDC);
LeadBitmap.Paint()->PaintDC();
::ReleaseDC(this->m_hWnd, hDC);