/* Use the mouse position's percentage offsets in the image rectangle
to determine the pixel offsets in the bitmap.
Using percentages allows for the possibility that the image is zoomed. */
EndPixelX = MulDiv(LeadBitmap.GetWidth(), EndGDIX - rLeadDest.left, DisplayWidth);
EndPixelY = MulDiv(LeadBitmap.GetHeight(), EndGDIY - rLeadDest.top, DisplayHeight);
/* Do nothing if no area is defined */
if ((EndPixelX == StartPixelX) || (EndPixelY == StartPixelY))
return;
/* Get the source rectangle coordinates for painting.
Allow for different mouse drag directions */
if (StartGDIX < EndGDIX)
{
rLeadSource.left = StartPixelX;
rLeadSource.right = EndPixelX;
}
else
{
rLeadSource.left = EndPixelX;
rLeadSource.right = StartPixelX;
}
if (StartGDIY < EndGDIY)
{
rLeadSource.top = StartPixelY;
rLeadSource.bottom = EndPixelY;
}
else
{
rLeadSource.top = EndPixelY;
rLeadSource.bottom = StartPixelY;
}
::GetClientRect(this->m_hWnd, &rClientSize);
/* Use this function to center the cropped display in the client area. */
CalcDisplay (rClientSize.right, /* Width allowed */
rClientSize.bottom, /* Height allowed */
rLeadSource.right - rLeadSource.left, /* Width factor, for aspect ratio */
rLeadSource.bottom - rLeadSource.top, /* Height factor, for aspect ratio */
&DisplayLeft, /* Resulting left value, for centering */
&DisplayTop, /* Resulting top value, for centering */
&DisplayWidth, /* Resulting width value */
&DisplayHeight); /* Resulting height value */
/* Set the destination rectangle for painting */
SetRect(&rLeadDest,
DisplayLeft, DisplayTop,
DisplayLeft + DisplayWidth,
DisplayTop + DisplayHeight);
/* If necessary, translate the source rectangle to the bitmap's view perspective */
if (LeadBitmap.GetViewPerspective() != TOP_LEFT)
LeadBitmap.RectToBitmap(TOP_LEFT, &rLeadSource);
/* Repaint the whole client area */
::InvalidateRect(this->m_hWnd,NULL,TRUE);
CView::OnLButtonUp(nFlags, point);
/*---[CalcDisplay]-----------------------------------------------------------
Parameters: WidthAllowed Maximum width
HeightAllowed Maximum height
WidthFactor For preserving aspect ratio, usually bitmap width
HeightFactor For preserving aspect ratio, usually bitmap height
*ResultLeft Pass NULL if you do not care about centering
Otherwise, this is updated with the X offset
*ResultTop Pass NULL if you do not care about centering
Otherwise, this is updated with the Y offset
*ResultWidth Address of the width variable to update
*ResultHeight Address of the height variable to update
Notes: Use this function to fit a displayed image in a particular
space, while preserving the aspect ratio.
--------------------------------------------------------------------------*/
void CTt2View::CalcDisplay (L_INT WidthAllowed, L_INT HeightAllowed,
L_INT WidthFactor, L_INT HeightFactor,
L_INT *ResultLeft, L_INT *ResultTop,
L_INT *ResultWidth, L_INT *ResultHeight)
{
/* Local variables for calculating results */
L_INT Left, Top, Width, Height;
/* 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 = (HeightAllowed - Height) / 2;
}
else
{ /* Use the maximum height, and calculate the width and left values */
Top = 0;
Height = HeightAllowed;
Width = MulDiv(Height, WidthFactor, HeightFactor);
Left = (WidthAllowed - Width) / 2;
}
/* Update the top and left results, if the caller did not pass NULL */
if (ResultTop != NULL)
*ResultTop = Top;
if (ResultLeft != NULL)
*ResultLeft = Left;
/* Update the width and height results */
*ResultWidth = Width;
*ResultHeight = Height;
return;
}