⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 picviewerview.cpp

📁 一个国人自己实现图像库的程序(有参考价值)
💻 CPP
字号:
// PicViewerView.cpp : implementation of the CPicViewerView class
//

#include "stdafx.h"
#include "PicViewer.h"

#include "PicViewerDoc.h"
#include "PicViewerView.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPicViewerView

IMPLEMENT_DYNCREATE(CPicViewerView, CScrollView)

BEGIN_MESSAGE_MAP(CPicViewerView, CScrollView)
	//{{AFX_MSG_MAP(CPicViewerView)
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_SETCURSOR()
	ON_WM_SIZE()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONDOWN()
	//}}AFX_MSG_MAP
	// Standard printing commands
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPicViewerView construction/destruction

CPicViewerView::CPicViewerView()
{
	m_bDraging = FALSE ;
}

CPicViewerView::~CPicViewerView()
{
}

BOOL CPicViewerView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CPicViewerView drawing
//BOOL fooIsShowDibEffectContinue ()
//{
//	return TRUE ;
//}
void CPicViewerView::OnDraw(CDC* pDC)
{
	CPicViewerDoc* pDoc = GetDocument();
	int		nOperation = ((CMainFrame*)::AfxGetMainWnd())->m_iCurrOption ;

	pDoc->m_DDB.SetCpuSpeed (((CPicViewerApp *)::AfxGetApp())->m_dwCpuFreq) ;

	if (pDoc->m_DDB.GetHandle())
	{
		if (this->GetDocument()->m_bFirstShow)
		{
			SHOWEFFECT	shParam ;
			switch (rand() % 3)
			{
				case 0 :
					shParam.dwMethod = SHOWDIB_SCAN ;
					shParam.dwSubMethod = 1 << (rand() % 8) ;
					shParam.dwInterval = 350 ;
					break ;
				case 1 :
					shParam.dwMethod = (rand()%2) ? SHOWDIB_HSHUTTER : SHOWDIB_VSHUTTER ;
					shParam.dwBlockLen = 16 ;
					shParam.dwInterval = 14 ;
					break ;
				case 2 :
					shParam.dwMethod = SHOWDIB_MOSAIC ;
					shParam.dwBlockLen = 15 ;
					shParam.dwInterval = 70 ;
					break ;
			}
			this->GetDocument()->m_DDB.DrawEx (pDC->GetSafeHdc(),shParam) ;
			this->GetDocument()->m_bFirstShow = FALSE ;
		}
		else
			pDoc->m_DDB.Draw (pDC->GetSafeHdc()) ;

		// shape_cut && select rect
		if ((nOperation == 2) || (nOperation == 4) || (nOperation == 1) || (nOperation == 3) || (nOperation == 5))
			if (pDoc->m_fPic.HasSelection ())
			{
				CRectTracker		RectTracker ;
				RectTracker.m_nStyle = CRectTracker::dottedLine ;
				pDoc->m_fPic.GetBoundBox(&RectTracker.m_rect) ;
				pDoc->m_DDB.MapScaledRect (RectTracker.m_rect) ;
				RectTracker.Draw (pDC) ;
			}
	}
}

void CPicViewerView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = sizeTotal.cy = 0;
	SetScrollSizes(MM_TEXT, sizeTotal);
}

/////////////////////////////////////////////////////////////////////////////
// CPicViewerView printing

BOOL CPicViewerView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CPicViewerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CPicViewerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CPicViewerView diagnostics

#ifdef _DEBUG
void CPicViewerView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CPicViewerView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CPicViewerDoc* CPicViewerView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPicViewerDoc)));
	return (CPicViewerDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPicViewerView message handlers

BOOL CPicViewerView::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	CBrush br;
	br.CreateSolidBrush (RGB(255,255,255)) ;
	FillOutsideRect(pDC, &br);
	return FALSE;
}

void CPicViewerView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	if (this->GetDocument()->m_DDB.GetHandle ())
	{
		CPicViewerDoc	* pDoc = GetDocument () ;
		CSize sizeTotal;
		sizeTotal.cx = pDoc->m_DDB.GetScaledWidth () ;
		sizeTotal.cy = pDoc->m_DDB.GetScaledHeight() ;
		// TODO: calculate the total size of this view
		SetScrollSizes(MM_TEXT, sizeTotal);
	}
	CView::OnUpdate (pSender, lHint, pHint) ;
}

void CPicViewerView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if (this->GetDocument ()->m_fPic.GetHandle () == NULL)
		return ;
	int		nOperation = ((CMainFrame*)::AfxGetMainWnd())->m_iCurrOption ;

	if (nOperation == 3) // view
		if (m_bDraging)
		{
			m_bDraging = FALSE ;
			::ReleaseCapture () ;
			::SetCursor (::LoadCursor (NULL, IDC_ARROW)) ;
		}

	// select polygon area
	if (nOperation == 6)
		if (m_bDraging)
		{
			m_pPolygon[m_nCurrent++] = point ;
			this->GetDocument()->m_DDB.MapRealPoint (m_pPolygon[m_nCurrent-1]) ;
			this->GetDocument()->m_fPic.SetPolygonRgn (m_pPolygon, m_nCurrent) ;
			CDC		* pDC = this->GetDC () ;
			POINT	start(m_pPolygon[0]) ;
			this->GetDocument()->m_DDB.MapScaledPoint (start) ;
			pDC->MoveTo (start) ;
			pDC->LineTo (point) ;
			this->ReleaseDC (pDC) ;
			::ReleaseCapture () ;
			delete[] m_pPolygon ;
			m_bDraging = FALSE ;
		}

	CScrollView::OnLButtonUp(nFlags, point);
}
void CPicViewerView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CPicViewerDoc	* pDoc = this->GetDocument () ;
	if (pDoc->m_fPic.GetHandle () == NULL)
		return ;
	int		nOperation = ((CMainFrame*)::AfxGetMainWnd())->m_iCurrOption ;

	// shape_cut && select rect && select ellipse
	if ((nOperation == 2) || (nOperation == 4) || (nOperation == 5))
	{
		CRectTracker		RectTracker ;
		if (!RectTracker.TrackRubberBand (this, point)) // mustn't SetCapture to mouse
			return ;
		// positive inverted (x,y) axis
		RectTracker.m_rect.NormalizeRect () ;

		// force the selected region within image
		pDoc->m_DDB.MapRealRect (RectTracker.m_rect) ; // get real rect
		CRect		rcDest, rcImg (0, 0, pDoc->m_fPic.Width(), pDoc->m_fPic.Height()) ;
		if (rcDest.IntersectRect (&rcImg, &RectTracker.m_rect) == 0)
			return ;
		if (nOperation == 2)
			pDoc->m_fPic.SetRectRgn (rcDest.left, rcDest.top, rcDest.right, rcDest.bottom) ;
		if (nOperation == 5)
			pDoc->m_fPic.SetEllipticRgn (rcDest.left, rcDest.top, rcDest.right, rcDest.bottom) ;

		// shape_cut process
		if (nOperation == 4)
		{
			CWaitCursor		waitcur ;
			FCImage		Mask (((CMainFrame*)::AfxGetMainWnd())->m_barShapeCut.m_CurrImg) ;
			Mask.Stretch (rcDest.Width(), rcDest.Height(), FALSE) ;
			if (pDoc->m_fPic.GetUndoFlag ())
				pDoc->m_fPic.AddToUndoList () ;
			pDoc->m_fPic.AlphaBlend ((FCDib)Mask, rcDest.left, rcDest.top) ;
			pDoc->AttachPic () ;
		}
	}

	// select polygon area
	if (nOperation == 6)
	{
		DISPLAYMODE		mode ;
		::fooGetCurrentDisplayMode (&mode) ;
		m_pPolygon = new POINT[mode.dwWidth * mode.dwHeight] ; // max pixel num limit screen size
		m_nCurrent = 0 ;
		m_pPolygon[m_nCurrent++] = point ;
		pDoc->m_DDB.MapRealPoint (m_pPolygon[m_nCurrent-1]) ;
		::SetCapture (GetSafeHwnd()) ;
		m_bDraging = TRUE ;
	}

	// zoom out
	if (nOperation == 1)
		pDoc->m_DDB.IncreaseZoomScale () ;

	// view
	if (nOperation == 3)
		if ((m_iClientW < pDoc->m_DDB.GetScaledWidth()) || (m_iClientH < pDoc->m_DDB.GetScaledHeight()))
		{
			m_bDraging = TRUE ;
			::SetCapture (GetSafeHwnd()) ;
			::SetCursor (::LoadCursor (::AfxGetInstanceHandle (), MAKEINTRESOURCE(IDC_AFTER_DRAG))) ;
			m_LastMousePos.x = point.x ;
			m_LastMousePos.y = point.y ;
		}

	pDoc->UpdateAllViews (NULL) ;
	this->InvalidateRect (NULL, FALSE) ;
	CScrollView::OnLButtonDown(nFlags, point);
}

void CPicViewerView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	if (this->GetDocument ()->m_fPic.GetHandle () == NULL)
		return ;
	int		nOperation = ((CMainFrame*)::AfxGetMainWnd())->m_iCurrOption ;

	// zoom in
	if (nOperation == 1)
		this->GetDocument ()->m_DDB.DecreaseZoomScale () ;	

	this->GetDocument ()->UpdateAllViews (NULL) ;
	this->InvalidateRect (NULL, FALSE) ;
	CScrollView::OnRButtonDown(nFlags, point);
}

// mouse move process
void CPicViewerView::OnMouseMove(UINT nFlags, CPoint point) 
{
	CPicViewerDoc	* pDoc = this->GetDocument () ;
	if (pDoc->m_fPic.GetHandle() == NULL)
		return ;
	int		nOperation = ((CMainFrame*)::AfxGetMainWnd())->m_iCurrOption ;

	// select polygon area
	if (nOperation == 6)
		if (m_bDraging)
		{
			m_pPolygon[m_nCurrent++] = point ;
			pDoc->m_DDB.MapRealPoint (m_pPolygon[m_nCurrent-1]) ;
			CDC		* pDC = this->GetDC () ;
			POINT	* scale = new POINT[m_nCurrent] ; // bak and scale it
			CopyMemory (scale, m_pPolygon, sizeof(POINT) * m_nCurrent) ;
			for (int i=0 ; i < m_nCurrent ; i++)
				pDoc->m_DDB.MapScaledPoint (scale[i]) ;
			pDC->Polyline (scale, m_nCurrent) ;
			delete[] scale ;
			this->ReleaseDC (pDC) ;
		}

	if (nOperation == 3) // view
		if (m_bDraging)
		{
			if (this->m_iClientW < pDoc->m_DDB.GetScaledWidth())
			{
				SCROLLINFO		sbInfo;
				this->GetScrollInfo (SB_HORZ, &sbInfo);
				int		nX =::fooBound (m_LastMousePos.x - point.x + sbInfo.nPos, sbInfo.nMin, sbInfo.nMax) ;
				this->SetScrollPos (SB_HORZ, nX) ;
			}
			if (this->m_iClientH < pDoc->m_DDB.GetScaledHeight())
			{
				SCROLLINFO		sbInfo;
				this->GetScrollInfo (SB_VERT, &sbInfo);
				int		nY =::fooBound (m_LastMousePos.y - point.y + sbInfo.nPos, sbInfo.nMin, sbInfo.nMax) ;
				this->SetScrollPos (SB_VERT, nY) ;
			}
			m_LastMousePos.x = point.x ;
			m_LastMousePos.y = point.y ;
			this->InvalidateRect (NULL, FALSE) ;
		}

	CScrollView::OnMouseMove(nFlags, point);
}

BOOL CPicViewerView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	if ((CPicViewerDoc*)this->GetDocument ()->m_fPic.GetHandle () == NULL)
	{
		::SetCursor (::LoadCursor (NULL, IDC_ARROW)) ;
		return TRUE ;
	}
	int		nOperation = ((CMainFrame*)::AfxGetMainWnd())->m_iCurrOption ;
	if (nOperation == 1) // zoom out & in
	{
		::SetCursor (::LoadCursor (::AfxGetInstanceHandle (), MAKEINTRESOURCE(IDC_ZOOMCUR))) ;
		return TRUE ;
	}
	if (nOperation == 3) // drag view
	{
		::SetCursor (::LoadCursor (::AfxGetInstanceHandle (), MAKEINTRESOURCE(IDC_BEFORE_DRAG))) ;
		return TRUE ;
	}
	// select operation
	if ((nOperation == 2) || (nOperation == 4) || (nOperation == 5) || (nOperation == 6))
	{
		::SetCursor (::LoadCursor (::AfxGetInstanceHandle (), MAKEINTRESOURCE(IDC_SELECT_AREA))) ;
		return TRUE ;
	}
	return CScrollView::OnSetCursor(pWnd, nHitTest, message);
}

void CPicViewerView::OnSize(UINT nType, int cx, int cy) 
{
	CScrollView::OnSize(nType, cx, cy);
	m_iClientW = cx ; m_iClientH = cy ;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -