📄 zoomview.cpp
字号:
FUNCTION: SetZoomMode
PURPOSE : Put the view into the specified zoom mode
---------------------------------------------------------------------------*/
void CZoomView::SetZoomMode (
ZoomMode_ zoomMode)
{
ASSERT(m_nMapMode == MM_ANISOTROPIC);
if (zoomMode != m_zoomMode) {
m_zoomMode = zoomMode;
// Force cursor change now
OnSetCursor(NULL, HTCLIENT, 0);
}
} // SetZoomMode
/*---------------------------------------------------------------------------
FUNCTION: CenterOnLogicalPoint
PURPOSE : Same as CScrollView::CenterOnPoint, but for logical coordinates
---------------------------------------------------------------------------*/
void CZoomView::CenterOnLogicalPoint(CPoint pt)
{
// Convert the point to device coordinates
ViewLPtoDP(&pt);
// Account for scroll bar position
ClientToDevice(pt);
// Use CScrollView's function for device coordinates
CScrollView::CenterOnPoint(pt);
} // CenterOnLogicalPoint
/*---------------------------------------------------------------------------
FUNCTION: GetLogicalCenterPoint
PURPOSE : Get the center of screen in logical coordinates
---------------------------------------------------------------------------*/
CPoint CZoomView::GetLogicalCenterPoint (void) // Point in logical units
{
CPoint pt;
CRect rect;
// Get the center of screen
GetClientRect(&rect);
pt.x = (rect.Width() / 2);
pt.y = (rect.Height() / 2);
// Convert the point to logical coordinates
ViewDPtoLP(&pt);
return pt;
} // GetLogicalCenterPoint
/*---------------------------------------------------------------------------
FUNCTION: DrawBox
PURPOSE : Draw a box - XOR if want to erase
---------------------------------------------------------------------------*/
void CZoomView::DrawBox (
CDC &dc,
CRect &rect,
BOOL xor)
{
CPen pen;
// Save the device context
dc.SaveDC();
if (xor) {
dc.SetROP2(R2_NOTXORPEN);
pen.CreatePen(PS_DASH, 0, RGB(0, 0, 0)); // 0 width = 1 device unit
} else {
pen.CreatePen(PS_SOLID, 0, RGB(0, 0, 0)); // 0 width = 1 device unit
}
dc.SelectObject(&pen);
// Draw the rect with lines (eliminate rect middle fill)
dc.MoveTo(rect.left, rect.top);
dc.LineTo(rect.right, rect.top);
dc.LineTo(rect.right, rect.bottom);
dc.LineTo(rect.left, rect.bottom);
dc.LineTo(rect.left, rect.top);
// Clean up
dc.RestoreDC(-1);
} // DrawBox
/*---------------------------------------------------------------------------
FUNCTION: DrawLine
PURPOSE : Draw a line - XOR to erase
---------------------------------------------------------------------------*/
void CZoomView::DrawLine (
CDC &dc,
const int &x1, // Logical units
const int &y1,
const int &x2,
const int &y2,
BOOL xor)
{
CPen pen;
// Save the device context
dc.SaveDC();
if (xor) {
dc.SetROP2(R2_NOTXORPEN);
pen.CreatePen(PS_DASH, 0, RGB(0, 0, 0)); // 0 width = 1 device unit
} else {
pen.CreatePen(PS_SOLID, 0, RGB(0, 0, 0)); // 0 width = 1 device unit
}
dc.SelectObject(&pen);
// Draw the line
dc.MoveTo(x1, y1);
dc.LineTo(x2, y2);
// Clean up
dc.RestoreDC(-1);
} // DrawLine
/*---------------------------------------------------------------------------
FUNCTION: OnLButtonDown
PURPOSE : Handle the left mouse click
---------------------------------------------------------------------------*/
void CZoomView::OnLButtonDown(
UINT nFlags,
CPoint point)
{
// Pass the message along
CScrollView::OnLButtonDown(nFlags, point);
switch (m_zoomMode) {
case MODE_ZOOMIN:
// Capture the mouse for zooming in
m_bCaptured = TRUE;
SetCapture();
// Save the mouse down point for XOR rect
ViewDPtoLP(&point);
m_ptDragRect.SetRect(point.x, point.y, point.x, point.y);
// Set the cursor to the cross hair
::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_CROSS)));
break;
default:
// Do nothing.
break;
}
} // OnLButtonDown
/*---------------------------------------------------------------------------
FUNCTION: OnMouseMove
PURPOSE : Handle the mouse movement
---------------------------------------------------------------------------*/
void CZoomView::OnMouseMove(UINT nFlags, CPoint point)
{
// Pass the message along
CScrollView::OnMouseMove(nFlags, point);
if (m_bCaptured) {
// Get the Device Context
CClientDC dc(this);
OnPrepareDC(&dc);
switch (m_zoomMode) {
case MODE_ZOOMIN:
// Draw the drag-rect
// Erase last rect
DrawBox(dc, m_ptDragRect);
// Draw new rect
dc.DPtoLP(&point);
m_ptDragRect.BottomRight() = point;
DrawBox(dc, m_ptDragRect);
break;
default:
// Do nothing.
break;
}
}
} // OnMouseMove
/*---------------------------------------------------------------------------
FUNCTION: OnLButtonUp
PURPOSE : Handle the left mouse release
---------------------------------------------------------------------------*/
void CZoomView::OnLButtonUp (
UINT nFlags,
CPoint point)
{
// Pass the message along
CScrollView::OnLButtonUp(nFlags, point);
switch (m_zoomMode) {
case MODE_ZOOMIN:
// Uncapture the mouse?
if (m_bCaptured) {
m_bCaptured = FALSE;
ReleaseCapture();
// Set back the cross cursor to the Z
::SetCursor(m_hZoomCursor);
// Get the Device Context
CClientDC dc(this);
OnPrepareDC(&dc);
// Erase the bounding box
DrawBox(dc, m_ptDragRect);
// Now Zoom in on logical rectangle
DoZoomIn(m_ptDragRect);
}
break;
case MODE_ZOOMOUT:
ViewDPtoLP(&point);
DoZoomOut(&point);
break;
default:
// Do nothing.
break;
}
} // OnLButtonUp
/*---------------------------------------------------------------------------
FUNCTION: OnRButtonDown
PURPOSE : Handle the right mouse click - CANCELS CURRENT ZOOM MODE OR DRAG
---------------------------------------------------------------------------*/
void CZoomView::OnRButtonDown(UINT nFlags, CPoint point)
{
CScrollView::OnRButtonDown(nFlags, point);
// See if currently captured
if (m_bCaptured) {
// Maintain current mode, just stop current drag
m_bCaptured = FALSE;
ReleaseCapture();
// Get the Device Context
CClientDC dc(this);
OnPrepareDC(&dc);
switch (m_zoomMode) {
case MODE_ZOOMIN:
// Erase last rect
DrawBox(dc, m_ptDragRect);
break;
default:
// Do nothing.
break;
}
} else {
// Cancel current mode
m_zoomMode = MODE_ZOOMOFF;
}
} // OnRButtonDown
/*---------------------------------------------------------------------------
FUNCTION: OnSetCursor
PURPOSE : Set the cursor depending on the zoom mode
---------------------------------------------------------------------------*/
BOOL CZoomView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if (nHitTest != HTCLIENT)
return CScrollView::OnSetCursor(pWnd, nHitTest, message);
switch (m_zoomMode) {
case MODE_ZOOMOFF:
::SetCursor(::LoadCursor(NULL, IDC_ARROW));
break;
default:
// All other zoom modes
::SetCursor(m_hZoomCursor);
break;
} // Zoom mode select
return TRUE;
} // OnSetCursor
/*---------------------------------------------------------------------------
FUNCTION: ViewDPtoLP
PURPOSE : Same as DPtoLP, but gets the Client DC for the view
---------------------------------------------------------------------------*/
void CZoomView::ViewDPtoLP (
LPPOINT lpPoints,
int nCount)
{
// Convert to logical units
// Called from View when no DC is available
ASSERT(m_nMapMode > 0); // must be set
CWindowDC dc(this);
OnPrepareDC(&dc);
dc.DPtoLP(lpPoints, nCount);
} // ViewDPtoLP
/*---------------------------------------------------------------------------
FUNCTION: ViewLPtoDP
PURPOSE : Same as LPtoDP, but gets the Client DC for the view
---------------------------------------------------------------------------*/
void CZoomView::ViewLPtoDP (
LPPOINT lpPoints,
int nCount)
{
// Convert to logical units
// Called from View when no DC is available
ASSERT(m_nMapMode > 0); // must be set
CWindowDC dc(this);
OnPrepareDC(&dc);
dc.LPtoDP(lpPoints, nCount);
} // ViewLPtoDP
/*---------------------------------------------------------------------------
FUNCTION: ClientToDevice
PURPOSE : Convert from Client coordinates to relative Device coordinates
---------------------------------------------------------------------------*/
void CZoomView::ClientToDevice (
CPoint &point)
{
// Need to account for scrollbar position
CPoint scrollPt = GetDeviceScrollPosition();
point.x += scrollPt.x;
point.y += scrollPt.y;
} // ClientToDevice
/*---------------------------------------------------------------------------
FUNCTION: NormalizeRect
PURPOSE : Normalize the rectangle
---------------------------------------------------------------------------*/
void CZoomView::NormalizeRect (
CRect &rect)
{
if (rect.left > rect.right) {
int r = rect.right;
rect.right = rect.left;
rect.left = r;
}
if (rect.top > rect.bottom) {
int b = rect.bottom;
rect.bottom = rect.top;
rect.top = b;
}
} // NormalizeRect
/*---------------------------------------------------------------------------
FUNCTION: PersistRatio
PURPOSE : Make a CSize maintain the given ratio (by shrinking if nescessary)
---------------------------------------------------------------------------*/
void CZoomView::PersistRatio (
const CSize &orig,
CSize &dest,
CPoint &remainder)
{
float ratio1 = (float) orig.cx / orig.cy;
float ratio2 = (float) dest.cx / dest.cy;
int newSize;
// Do nothing if they are the same
if (ratio1 > ratio2) {
// Shrink hieght
newSize = (int)(dest.cx / ratio1);
remainder.x = 0;
remainder.y = dest.cy - newSize;
dest.cy = newSize;
} else if (ratio2 > ratio1) {
// Shrink width
newSize = (int)(dest.cy * ratio1);
remainder.x = dest.cx - newSize;
remainder.y = 0;
dest.cx = newSize;
}
} // PersistRatio
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -