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

📄 graphicsview.cpp

📁 这些源代码
💻 CPP
字号:
// GraphicsView.cpp : implementation of the CGraphicsView class
//

#include "stdafx.h"
#include "Graphics.h"

#include "GraphicsDoc.h"
#include "GraphicsView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGraphicsView

IMPLEMENT_DYNCREATE(CGraphicsView, CView)

BEGIN_MESSAGE_MAP(CGraphicsView, CView)
	//{{AFX_MSG_MAP(CGraphicsView)
	ON_WM_HSCROLL()
	ON_WM_VSCROLL()
	ON_COMMAND(IDM_SCALE_11, OnScale11)
	ON_COMMAND(IDM_SCALE_12, OnScale12)
	ON_COMMAND(IDM_SCALE_21, OnScale21)
	ON_COMMAND(IDM_SCALE_TOFIT, OnScaleToFit)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGraphicsView construction/destruction

CGraphicsView::CGraphicsView()
{
	m_nScale = 100;
	m_pPix = NULL;
}

CGraphicsView::~CGraphicsView()
{
}

BOOL CGraphicsView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CGraphicsView drawing

void CGraphicsView::OnDraw(CDC* pDC)
{
	CGraphicsDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	if (m_pPix)
	{
// get width and height of picture
		long hmWidth;
		long hmHeight;
		m_pPix->get_Width(&hmWidth);
		m_pPix->get_Height(&hmHeight);
// Let the DC convert himetric to pixels
		SIZE sz;
		sz.cx = hmWidth;
		sz.cy = hmHeight;
		pDC->HIMETRICtoDP (&sz);
		int nWidth = (sz.cx * m_nScale) / 100;
		int nHeight = (sz.cy * m_nScale) / 100;
		RECT rc;
		GetClientRect(&rc);

		if ((rc.right - rc.left) > nWidth)
		{
			ShowScrollBar(SB_HORZ, FALSE);
			EnableScrollBar (SB_HORZ, ESB_DISABLE_BOTH);
			SetScrollRange (SB_HORZ, 0, nWidth, TRUE);
		}
		else
		{
			ShowScrollBar(SB_HORZ, TRUE);
			m_siHorz.cbSize = sizeof (SCROLLINFO);
			m_siHorz.nMin = 0;
			m_siHorz.fMask = SIF_ALL;
			m_siHorz.nMax = nWidth;
			m_siHorz.nPage = rc.right - rc.left;
			SetScrollInfo (SB_HORZ, &m_siHorz, true);
			EnableScrollBar (SB_HORZ, ESB_ENABLE_BOTH);
			SetScrollRange (SB_HORZ, 0, nWidth, TRUE);
//			SetScrollRange (SB_VERT, 0, nHeight, TRUE);
		}
		if ((rc.bottom - rc.top) > nHeight)
		{
			ShowScrollBar(SB_VERT, FALSE);
			EnableScrollBar (SB_VERT, ESB_DISABLE_BOTH);
		}
		else
		{
			ShowScrollBar(SB_VERT, TRUE);
			m_siVert.cbSize = sizeof (SCROLLINFO);
			m_siVert.nMin = 0;
			m_siVert.fMask = SIF_ALL;
			m_siVert.nMax = nHeight;
			m_siVert.nPage = rc.bottom - rc.top;
			SetScrollInfo (SB_VERT, &m_siVert, true);
			EnableScrollBar (SB_VERT, ESB_ENABLE_BOTH);
			SetScrollRange (SB_VERT, 0, nHeight, TRUE);
		}

		sz.cx = m_siHorz.nPos;
		sz.cy = m_siVert.nPos;
		pDC->DPtoHIMETRIC (&sz);
		sz.cx = (sz.cx * 100) / m_nScale;
		sz.cy = (sz.cy * 100) / m_nScale;

// display the picture
		m_pPix->Render(pDC->m_hDC,
						  0,			//	Horizontal position of image in the device context
						  0,			//	Vertical position of image in the device context
						  nWidth,		//	Horizontal dimension of destination rectangle
						  nHeight,		//	Vertical dimension of destination rectangle
						  sz.cx,		//	Horizontal offset in source picture
						  hmHeight - sz.cy,	//	Vertical offset in source picture
						  hmWidth,		//	Amount to copy horizontally in source picture
						  -hmHeight,	//	Amount to copy vertically in source picture
						  &rc);			//	Pointer to position of destination for a metafile hdc)

	}
}

/////////////////////////////////////////////////////////////////////////////
// CGraphicsView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CGraphicsView diagnostics

#ifdef _DEBUG
void CGraphicsView::AssertValid() const
{
	CView::AssertValid();
}

void CGraphicsView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CGraphicsView message handlers

void CGraphicsView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	RECT	rClip;
	GetClientRect (&rClip);
	switch (nSBCode)
	{
	int		Pos, Scroll;
		case SB_BOTTOM:			//	Scrolls to the lower right.
		case SB_ENDSCROLL:		//	Ends scroll.
			return;
		case SB_LINEDOWN:		//	Scrolls one line down.
			if ((m_siHorz.nPos + (int) m_siHorz.nPage) >= m_siHorz.nMax)
				return;
			++m_siHorz.nPos;
			ScrollWindowEx (-1, 0, NULL, &rClip, NULL, NULL, SW_ERASE | SW_INVALIDATE);
			break;
		case SB_LINEUP:			//	Scrolls one line right.
			--m_siHorz.nPos;
			if (m_siHorz.nPos < 0)
				m_siHorz.nPos = 0;
			else
				ScrollWindowEx (1, 0, NULL, &rClip, NULL, NULL, SW_ERASE | SW_INVALIDATE);
			break;
		case SB_PAGEDOWN:		//	Scrolls one page right.
			if (m_siHorz.nPos >= m_siHorz.nMax)
				return;
			Pos = m_siHorz.nPos;
			m_siHorz.nPos += m_siHorz.nPage / 2;
			if ((m_siHorz.nPos + (int) m_siHorz.nPage) > m_siHorz.nMax)
			{
				m_siHorz.nPos = m_siHorz.nMax - m_siHorz.nPage;
				Scroll = m_siHorz.nMax - m_siHorz.nPage - Pos;
			}
			else
				Scroll = m_siHorz.nPage / 2;
			ScrollWindowEx (-Scroll, 0, NULL, &rClip, NULL, NULL, SW_ERASE | SW_INVALIDATE);
			break;
		case SB_PAGEUP:			//	Scrolls one page left.
			if (m_siHorz.nPos == 0)
				return;
			Pos = m_siHorz.nPos;
			m_siHorz.nPos -= m_siHorz.nPage / 2;
			if (m_siHorz.nPos < 0)
			{
				m_siHorz.nPos = 0;
				Scroll = Pos;
			}
			else
				Scroll = m_siHorz.nPage / 2;
			ScrollWindowEx (Scroll, 0, NULL, &rClip, NULL, NULL, SW_ERASE | SW_INVALIDATE);
			break;
		case SB_THUMBPOSITION:	//	The user has dragged the scroll box (thumb) and released the mouse button. The nPos parameter indicates the position of the scroll box at the end of the drag operation.
		case SB_THUMBTRACK:		//	The user is dragging the scroll box. This message is sent repeatedly until the user releases the mouse button. The nPos parameter indicates the position that the scroll box has been dragged to.
			ScrollWindowEx (m_siHorz.nPos - nPos, 0, NULL, &rClip, NULL, NULL, SW_ERASE | SW_INVALIDATE);
			m_siHorz.nPos = nPos;// HIWORD(wParam);
			break;
		case SB_TOP:				//	Scrolls to the upper left.
			return;
			break;
	}
	SetScrollInfo (SB_HORZ, &m_siHorz, true);
}

void CGraphicsView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	RECT	rClip;
	GetClientRect (&rClip);
	switch (nSBCode)
	{
	int		Pos, Scroll;
		case SB_BOTTOM:			//	Scrolls to the lower right.
		case SB_ENDSCROLL:		//	Ends scroll.
			return;
		case SB_LINEDOWN:		//	Scrolls one line down.
			if ((m_siVert.nPos + (int) m_siVert.nPage) >= m_siVert.nMax)
				return;
			ScrollWindowEx (0, -1, NULL, &rClip, NULL, NULL, SW_ERASE | SW_INVALIDATE);
			m_siVert.nPos += 1;
			break;
		case SB_LINEUP:			//	Scrolls one line up.
			if (m_siVert.nPos == 0)
				return;
			ScrollWindowEx (0, 1, NULL, &rClip, NULL, NULL, SW_ERASE | SW_INVALIDATE);
			m_siVert.nPos -= 1;
			break;
		case SB_PAGEDOWN:		//	Scrolls one page down.
			if (m_siVert.nPos >= m_siVert.nMax)
				return;
			Pos = m_siVert.nPos;
			m_siVert.nPos += (int) m_siVert.nPage / 2;
			if ((m_siVert.nPos + (int) m_siVert.nPage) > m_siVert.nMax)
			{
				m_siVert.nPos = m_siVert.nMax - m_siVert.nPage;
				Scroll = Pos;
			}
			else
				Scroll = m_siVert.nPage / 2;
			ScrollWindowEx (0, -Scroll, NULL, &rClip, NULL, NULL, SW_ERASE | SW_INVALIDATE);
//			ScrollWindowEx (0, -((int) m_siVert.nPage / 2), NULL, &rClip, NULL, NULL, SW_ERASE | SW_INVALIDATE);
			break;
		case SB_PAGEUP:			//	Scrolls one page up.
			if (m_siVert.nPos == 0)
				return;
			Pos = m_siVert.nPos;
			m_siVert.nPos -= m_siVert.nPage / 2;
			if (m_siVert.nPos < 0)
			{
//				Scroll = 0;
				m_siVert.nPos = 0;
				Scroll = Pos;
			}
			else
				Scroll = m_siVert.nPage / 2;
			ScrollWindowEx (0, Scroll, NULL, &rClip, NULL, NULL, SW_ERASE | SW_INVALIDATE);
//			ScrollWindowEx (0, m_siVert.nPage / 2, NULL, &rClip, NULL, NULL, SW_ERASE | SW_INVALIDATE);
			break;
		case SB_THUMBPOSITION:	//	The user has dragged the scroll box (thumb) and released the mouse button. The nPos parameter indicates the position of the scroll box at the end of the drag operation.
		case SB_THUMBTRACK:		//	The user is dragging the scroll box. This message is sent repeatedly until the user releases the mouse button. The nPos parameter indicates the position that the scroll box has been dragged to.
			ScrollWindowEx (0, m_siVert.nPos - nPos, NULL, &rClip, NULL, NULL, SW_ERASE | SW_INVALIDATE);
			m_siVert.nPos = nPos;
			break;
		case SB_TOP:				//	Scrolls to the upper left.
			return;
	}
	SetScrollInfo (SB_VERT, &m_siVert, true);
}

void CGraphicsView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	if (pSender == this)
		return;
//
//	If the screen is being cleared, hide the scroll bars.
//
	if (lHint == 1)
	{
		ShowScrollBar(SB_HORZ, FALSE);
		ShowScrollBar(SB_VERT, FALSE);
	}
	LPSTREAM pStream = NULL;
	DWORD dwSize = GetDocument()->GetGlobalSize();
	HGLOBAL hGlobal = GetDocument()->GetGlobalBuffer();
//
//	If there's already a picture, dump it
//
	if (m_pPix)
	{
		m_pPix->Release();
		m_pPix = NULL;
		Invalidate ();
	}
	if (hGlobal == NULL)
		return;
// Create an IStream* from global memory
	HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pStream);
	_ASSERTE(SUCCEEDED(hr) && pStream);

// Create IPicture from image file
	hr = ::OleLoadPicture(pStream, dwSize, FALSE, IID_IPicture, (LPVOID *)&m_pPix);
	_ASSERTE(SUCCEEDED(hr) && m_pPix);	
	pStream->Release();
	m_nScale = 100;
	memset (&m_siVert, '\0', sizeof (SCROLLINFO));
	memset (&m_siHorz, '\0', sizeof (SCROLLINFO));
	m_siVert.cbSize = sizeof (SCROLLINFO);
	m_siHorz.cbSize = sizeof (SCROLLINFO);
	Invalidate ();
}

void CGraphicsView::OnScale11() 
{
	if (m_nScale == 100)
		return;
	m_nScale = 100;
	memset (&m_siVert, '\0', sizeof (SCROLLINFO));
	memset (&m_siHorz, '\0', sizeof (SCROLLINFO));
	m_siVert.cbSize = sizeof (SCROLLINFO);
	m_siHorz.cbSize = sizeof (SCROLLINFO);
	Invalidate ();
}

void CGraphicsView::OnScale12() 
{
	if (m_nScale == 50)
		return;
	m_nScale = 50;
	memset (&m_siVert, '\0', sizeof (SCROLLINFO));
	memset (&m_siHorz, '\0', sizeof (SCROLLINFO));
	m_siVert.cbSize = sizeof (SCROLLINFO);
	m_siHorz.cbSize = sizeof (SCROLLINFO);
	Invalidate ();
}

void CGraphicsView::OnScale21() 
{
	if (m_nScale == 200)
		return;
	m_nScale = 200;
	memset (&m_siVert, '\0', sizeof (SCROLLINFO));
	memset (&m_siHorz, '\0', sizeof (SCROLLINFO));
	m_siVert.cbSize = sizeof (SCROLLINFO);
	m_siHorz.cbSize = sizeof (SCROLLINFO);
	Invalidate ();
}

void CGraphicsView::OnScaleToFit() 
{
	if (!m_pPix)
		return;
// get width and height of picture
	long hmWidth;
	long hmHeight;
	m_pPix->get_Width(&hmWidth);
	m_pPix->get_Height(&hmHeight);
// Let the DC convert himetric to pixels
	CSize sz;
	sz.cx = hmWidth;
	sz.cy = hmHeight;
	CWindowDC dc(this);
	dc.HIMETRICtoDP (&sz);
	CRect rcWindow;
	GetClientRect (rcWindow);
	m_nScale = 100 * (rcWindow.right - rcWindow.left) / sz.cx;
	int nHeight = (sz.cy * m_nScale) / 100;
	if (nHeight > sz.cy)
		m_nScale = 100 * (rcWindow.bottom - rcWindow.top) / sz.cy;
	Invalidate ();
}

⌨️ 快捷键说明

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