📄 imageviewerview.cpp
字号:
if (NewPos.x < 0)
NewPos.x = 0;
else if (NewPos.x > xMax)
NewPos.x = xMax;
}
if (GetStyle() & WS_VSCROLL)
{
if (pt.y < AUTOSCROLLBORDER)
NewPos.y -= pixels;
if (ClientRect.bottom - pt.y < AUTOSCROLLBORDER)
NewPos.y += pixels;
int yMax = GetScrollLimit(SB_VERT);
if (NewPos.y < 0)
NewPos.y = 0;
else if (NewPos.y > yMax)
NewPos.y = yMax;
}
if (NewPos != ScrollPos)
{
SetScrollPos(SB_HORZ, NewPos.x);
SetScrollPos(SB_VERT, NewPos.y);
ScrollWindow(ScrollPos.x - NewPos.x, ScrollPos.y - NewPos.y);
}
}
return;
}
CScrollView::OnTimer(nIDEvent);
}
void CImageViewerView::OnLButtonDown(UINT nFlags, CPoint point)
{
CRect rc;
GetClientRect(rc);
rc.DeflateRect(AUTOSCROLLBORDER, AUTOSCROLLBORDER);
if (rc.PtInRect(point))
{
SetCapture();
LButtonDown = true;
ClipCursor(GetVisibleRect());
}
CScrollView::OnLButtonDown(nFlags, point);
}
void CImageViewerView::OnLButtonUp(UINT nFlags, CPoint point)
{
if (LButtonDown)
{
ReleaseCapture();
LButtonDown = false;
AutoScrollTimer = false;
ClipCursor(NULL);
}
CScrollView::OnLButtonUp(nFlags, point);
}
CRect CImageViewerView::GetVisibleRect(void) const
{
// returns the part of the client area of the view window that
// is visible within the MDIClient area, in screen coordinates
CRect VisibleRect;
VisibleRect.SetRectEmpty();
CMainFrame *pMainFrame = dynamic_cast<CMainFrame *>(AfxGetMainWnd());
if (pMainFrame)
{
CWnd *pMDIClient = CWnd::FromHandle(pMainFrame->m_hWndMDIClient);
if (pMDIClient)
{
CRect MDIClientRect;
pMDIClient->GetClientRect(MDIClientRect);
pMDIClient->ClientToScreen(MDIClientRect);
CRect ViewClientRect;
GetClientRect(ViewClientRect);
ClientToScreen(ViewClientRect);
VisibleRect = ViewClientRect & MDIClientRect;
}
}
return VisibleRect;
}
void CImageViewerView::OnViewCenterMouse()
{
CRect rc = GetVisibleRect();
if (!rc.IsRectEmpty())
{
CPoint pt = rc.CenterPoint();
CenterCursorOnPixel(pt);
}
}
void CImageViewerView::OnUpdateViewCenterMouse(CCmdUI *pCmdUI)
{
CRect rc = GetVisibleRect();
pCmdUI->Enable(!rc.IsRectNull());
}
#if !defined NO_GDIPLUS
void CImageViewerView::OnImageSave()
{
bool locked = AfxGetMainWnd()->SendMessage(WMU_GETDISPLAYLOCK, 0, 0) != 0;
if (!locked)
{
// temporarily lock the display
AfxGetMainWnd()->SendMessage(WM_COMMAND, ID_VIEW_DISPLAY_LOCK, 0);
AfxGetApp()->OnIdle(-1);
}
// Save the image
Gdiplus::Bitmap GDIplusBitmap(CurrentBitmap, NULL);
CString Filter;
Filter.LoadString(IDS_SAVEFILTER);
CString Title;
Title.LoadString(IDS_SAVEIMAGEAS);
CFileDialog Dlg(FALSE,
_T(".png"),
_T("Untitled"),
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING,
Filter,
this);
Dlg.m_ofn.lpstrTitle = Title;
theApp.ModalDialogEvent.ResetEvent();
if (IDOK == Dlg.DoModal())
{
CString Path = Dlg.GetPathName();
CString Extension = Dlg.GetFileExt().MakeLower();
CString Encoder = _T("image/");
if (Extension == _T("tif") || Extension == _T("tiff"))
{
Encoder += _T("tiff");
}
else if (Extension == _T("jpg") || Extension == _T("jpeg"))
{
Encoder += _T("jpeg");
}
else if (Extension == _T("dib") || Extension == _T("bmp"))
{
Encoder += _T("bmp");
}
else if (Extension == _T("png"))
{
Encoder += _T("png");
}
else if (Extension == _T("gif"))
{
Encoder += _T("gif");
}
else
{
switch(Dlg.m_pOFN->nFilterIndex)
{
case 0:
case 1:
Path += _T(".png");
Encoder += _T("png");
break;
case 2:
Path += _T(".bmp");
Encoder += _T("bmp");
break;
case 3:
Path += _T(".tif");
Encoder += _T("tiff");
break;
case 4:
Path += _T(".gif");
Encoder += _T("gif");
break;
case 5:
Path += _T(".jpg");
Encoder += _T("jpeg");
break;
}
}
CLSID Clsid;
Gdiplus::Status Status = Gdiplus::UnknownImageFormat;
if (-1 != GetEncoderClsid(CT2CW(Encoder), &Clsid))
{
Status = GDIplusBitmap.Save(CT2CW(Path), &Clsid, NULL);
}
if (Status != Gdiplus::Ok)
{
AfxMessageBox(CString(_T("An error happened. Unable to save file:\n\n")) + Path);
}
}
theApp.ModalDialogEvent.SetEvent();
if (!locked)
{
// remove the temporary lock
AfxGetMainWnd()->SendMessage(WM_COMMAND, ID_VIEW_DISPLAY_LOCK, 0);
}
}
void CImageViewerView::OnUpdateImageSave(CCmdUI *pCmdUI)
{
pCmdUI->Enable(NULL != CurrentBitmap);
}
#endif // #if !defined NO_GDIPLUS
void CImageViewerView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if ((GetStyle() & WS_HSCROLL) != WS_HSCROLL)
{
return;
}
int pos = GetScrollPos(SB_HORZ);
int max = GetScrollLimit(SB_HORZ);
int newpos = pos;
switch(nSBCode)
{
case SB_LINERIGHT:
if (pos < max)
{
if (ZoomRatios[CurrentZoomRatio] < 100)
{
newpos = pos + 1;
}
else
{
double d = (double)pos / ((double)ZoomRatios[CurrentZoomRatio] / 100.0);
d = 1.0 + floor(d - floor(d) > 0.5 ? d + 1.0 : d);
newpos = (int)(((double)ZoomRatios[CurrentZoomRatio] / 100.0) * d);
}
if (newpos > max)
{
newpos = max;
}
}
break;
case SB_LINELEFT:
if (pos > 0)
{
if (ZoomRatios[CurrentZoomRatio] < 100)
{
newpos = pos - 1;
}
else
{
double d = (double)pos / ((double)ZoomRatios[CurrentZoomRatio] / 100.0);
d = -1.0 + floor(d - floor(d) > 0.5 ? d + 1.0 : d);
newpos = (int)(((double)ZoomRatios[CurrentZoomRatio] / 100.0) * d);
}
if (newpos < 0)
{
newpos = 0;
}
}
break;
case SB_THUMBTRACK:
case SB_THUMBPOSITION:
{
SCROLLINFO ScrollInfo = {0};
GetScrollInfo(SB_HORZ, &ScrollInfo, SIF_TRACKPOS);
newpos = ScrollInfo.nTrackPos;
}
break;
default:
CScrollView::OnHScroll(nSBCode, nPos, pScrollBar);
return;
}
if (pos != newpos)
{
SetScrollPos(SB_HORZ, newpos);
ScrollWindow(pos - newpos, 0);
AfxGetMainWnd()->SendMessage(WMU_UPDATETOOLTIP, (WPARAM)this, 0);
}
}
void CImageViewerView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if ((GetStyle() & WS_VSCROLL) != WS_VSCROLL)
{
return;
}
int pos = GetScrollPos(SB_VERT);
int max = GetScrollLimit(SB_VERT);
int newpos = pos;
switch(nSBCode)
{
case SB_LINEDOWN:
if (pos < max)
{
if (ZoomRatios[CurrentZoomRatio] < 100)
{
newpos = pos + 1;
}
else
{
double d = (double)pos / ((double)ZoomRatios[CurrentZoomRatio] / 100.0);
d = 1.0 + floor(d - floor(d) < 0.5 ? d : d + 1.0);
newpos = (int)(((double)ZoomRatios[CurrentZoomRatio] / 100.0) * d);
}
if (newpos > max)
{
newpos = max;
}
}
break;
case SB_LINEUP:
if (pos > 0)
{
if (ZoomRatios[CurrentZoomRatio] < 100)
{
newpos = pos - 1;
}
else
{
double d = (double)pos / ((double)ZoomRatios[CurrentZoomRatio] / 100.0);
d = -1.0 + floor(d - floor(d) < 0.5 ? d : d + 1.0);
newpos = (int)(((double)ZoomRatios[CurrentZoomRatio] / 100.0) * d);
}
if (newpos < 0)
{
newpos = 0;
}
}
break;
case SB_THUMBTRACK:
case SB_THUMBPOSITION:
{
SCROLLINFO ScrollInfo = {0};
GetScrollInfo(SB_VERT, &ScrollInfo, SIF_TRACKPOS);
newpos = ScrollInfo.nTrackPos;
}
break;
default:
CScrollView::OnVScroll(nSBCode, nPos, pScrollBar);
return;
}
if (pos != newpos)
{
SetScrollPos(SB_VERT, newpos);
ScrollWindow(0, pos - newpos);
AfxGetMainWnd()->SendMessage(WMU_UPDATETOOLTIP, (WPARAM)this, 0);
}
}
void CImageViewerView::CalcBitmapRect()
{
if (!ZoomedBitmapRect.IsRectEmpty())
{
CRect ClientRect;
GetClientRect(ClientRect);
// Set the top left corner to 0, 0
ZoomedBitmapRect.OffsetRect(-ZoomedBitmapRect.left, -ZoomedBitmapRect.top);
// Center the zoomed image on the view
int top = ZoomedBitmapRect.Height() < ClientRect.Height() ? ClientRect.CenterPoint().y - ZoomedBitmapRect.Height() / 2 : 0;
int left = ZoomedBitmapRect.Width() < ClientRect.Width() ? ClientRect.CenterPoint().x - ZoomedBitmapRect.Width() / 2 : 0;
ZoomedBitmapRect.OffsetRect(left, top);
}
}
void CImageViewerView::OnSize(UINT nType, int cx, int cy)
{
CScrollView::OnSize(nType, cx, cy);
CalcBitmapRect();
}
LRESULT CImageViewerView::OnMouseGesture(WPARAM wp, LPARAM /* lp */)
{
switch (wp)
{
case GESTUREUP:
OnImageFirst();
break;
case GESTUREDOWN:
OnImageLast();
break;
case GESTURELEFT:
OnImagePrevious();
break;
case GESTURERIGHT:
OnImageNext();
break;
default:
TRACE(_T("Unhandled mouse gesture: ID = %d\n"), wp);
ASSERT(0);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -