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

📄 dibview.cpp

📁 这是一款蛮COOL的图像处理系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DIBView.cpp : implementation of the CDIBView class
//

#include "stdafx.h"
#include "QuickImage.h"

#include "DIBDoc.h"
#include "DIBView.h"
#include "Piccontrol.h"
//#include "MathEx.h"
#include <math.h>
#include "resource.h"
#include "MainFrm.h"
#include "Global.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDIBView
extern CString	g_strCurrentDir;
extern bool		g_bShowHide;
extern CList<CString,CString&> g_strImgFileNames;

IMPLEMENT_DYNCREATE(CDIBView, CView)

BEGIN_MESSAGE_MAP(CDIBView, CView)
	ON_WM_CONTEXTMENU()
	//{{AFX_MSG_MAP(CDIBView)
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_KEYDOWN()
	//}}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)

	ON_COMMAND_RANGE(ID_MAP_PAN, ID_MAP_SCISSOR, OnMapButton)
	ON_UPDATE_COMMAND_UI_RANGE(ID_MAP_PAN, ID_MAP_SCISSOR, OnUpdateMapButtonState)
	ON_COMMAND_RANGE(ID_DIR_HOME, ID_DIR_END, OnDirButtons)
	ON_UPDATE_COMMAND_UI_RANGE(ID_DIR_HOME, ID_DIR_END, OnUpdateDirButtons)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDIBView construction/destruction

CDIBView::CDIBView()
{
	m_uIDMapButton = UINT_MAX;
	m_ptMouseDown.x=m_ptMouseDown.y=-1;
	m_ptTracing.x = m_ptTracing.y = 0;
	m_ptFirst.x = m_ptFirst.y = -1;
	m_iBacks = 0;
	m_iMaxBacks = 10;
	m_dMeasure = 0.0;
}

CDIBView::~CDIBView()
{
}

BOOL CDIBView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
//	cs.lpszClass = AfxRegisterWndClass(NULL,AfxGetApp()->LoadCursor(IDC_AIM),
//		0,0);

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDIBView drawing

void CDIBView::OnDraw(CDC* pDC)
{
	CDIBDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	HDIB hDIB = pDoc->GetHDIB();
	if (hDIB != NULL)
	{
		CRect rcDest , rcDIB;
		if (pDC->IsPrinting())   // printer DC
		{
			LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
			int cxDIB = (int) ::DIBWidth(lpDIB);         // Size of DIB - x
			int cyDIB = (int) abs(::DIBHeight(lpDIB));        // Size of DIB - y
			::GlobalUnlock((HGLOBAL) hDIB);

			rcDIB.top = rcDIB.left = 0;
			rcDIB.right = cxDIB;
			rcDIB.bottom = cyDIB;

			// get size of printer page (in pixels)
			int cxPage = pDC->GetDeviceCaps(HORZRES);
			int cyPage = pDC->GetDeviceCaps(VERTRES);
			// get printer pixels per inch
			int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
			int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);

			//
			// Best Fit case -- create a rectangle which preserves
			// the DIB's aspect ratio, and fills the page horizontally.
			//
			// The formula in the "->bottom" field below calculates the Y
			// position of the printed bitmap, based on the size of the
			// bitmap, the width of the page, and the relative size of
			// a printed pixel (cyInch / cxInch).
			//
			rcDest.top = rcDest.left = 0;
			rcDest.bottom = (int)(((double)cyDIB * cxPage * cyInch)
					/ ((double)cxDIB * cxInch));
			rcDest.right = cxPage;
		}
		else   // not printer DC
		{
			GetClientRect(&rcDest);
			CPicControl::CalcuRect(rcDest, rcDIB, m_ptDIBCenter, hDIB, m_dZoom);
		}
		::PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(),
			&rcDIB, pDoc->GetDocPalette());
	}
}

/////////////////////////////////////////////////////////////////////////////
// CDIBView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDIBView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDIBView message handlers

void CDIBView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	HDIB hDIB = GetDocument()->GetHDIB();
	int cxDIB,cyDIB;
	if (hDIB != NULL)
	{
		LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
		cxDIB = (int) ::DIBWidth(lpDIB);         // Size of DIB - x
		cyDIB = (int) abs(::DIBHeight(lpDIB));        // Size of DIB - y
		::GlobalUnlock((HGLOBAL) hDIB);
		
		m_ptDIBCenter.x = cxDIB/2;
		m_ptDIBCenter.y = cyDIB/2;
		
		CRect rcDC;
		GetClientRect(&rcDC);
		
		if(cxDIB > rcDC.Width() || cyDIB > rcDC.Height())
		{
			m_dZoom = CPicControl::ViewFit(m_ptDIBCenter, rcDC, hDIB);
		}
		else
		{
			m_dZoom = CPicControl::ViewActual(m_ptDIBCenter, rcDC, hDIB);
		}
		
		CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();
		ASSERT(NULL != pMainWnd);
		CString strText;
		strText.Format("Image Size: %d,%d", cxDIB, cyDIB);
		pMainWnd->SetStatusText(1, (LPCTSTR)strText);
		
		strText.Format("Zoom: %d%%",
			int(100.0 / m_dZoom));
		pMainWnd->SetStatusText(2, (LPCTSTR)strText);
	}
}

void CDIBView::OnMapButton(UINT uID)
{
	CRect rcView;
	GetClientRect(&rcView);
	switch(uID)
	{
	case ID_MAP_ACTUAL:
		Push();
		m_dZoom = CPicControl::ViewActual(m_ptDIBCenter, rcView, GetDocument()->GetHDIB());
		m_uIDMapButton = UINT_MAX;
		Invalidate();
		SetStatusText();
		break;
	case ID_MAP_FIT:
		Push();
		m_dZoom = CPicControl::ViewFit(m_ptDIBCenter, rcView, GetDocument()->GetHDIB());
		m_uIDMapButton = UINT_MAX;
		Invalidate();
		SetStatusText();
		break;
	case ID_MAP_VIEWPREVIOUS:
		if(m_iBacks > 0)
		{
			PICCONTROL a = Pop();
			m_ptDIBCenter.x = a.x;
			m_ptDIBCenter.y = a.y;
			m_dZoom = a.zoom;
			Invalidate();
			SetStatusText();
		}
		m_uIDMapButton = UINT_MAX;
		break;
	case ID_MAP_ZOOMOUT:
		BeginWaitCursor();
		Push();
		m_dZoom = CPicControl::ZoomOut(m_dZoom);
		Invalidate();
		EndWaitCursor();
		m_uIDMapButton = UINT_MAX;
		SetStatusText();
		break;
	case ID_MAP_ZOOMIN:
		BeginWaitCursor();
		Push();
		m_dZoom = CPicControl::ZoomIn(m_dZoom);
		Invalidate();
		EndWaitCursor();
		m_uIDMapButton = UINT_MAX;
		SetStatusText();
		break;
	default:
		m_uIDMapButton = (m_uIDMapButton == uID) ? UINT_MAX : uID;
		break;
	}
}

void CDIBView::OnUpdateMapButtonState(CCmdUI* pCmdUI)
{
	if(ID_MAP_VIEWPREVIOUS == pCmdUI->m_nID)
//		&& ID_MAP_VIEWPREVIOUS == m_uIDMapButton)
	{
		pCmdUI->SetCheck(FALSE);
		pCmdUI->Enable(m_iBacks > 0);
	}
	else
	{
		pCmdUI->SetCheck(pCmdUI->m_nID == m_uIDMapButton);
	}
}

void CDIBView::OnMouseMove(UINT nFlags, CPoint point) 
{
	CDC *pDC=GetDC();
	ASSERT_VALID(pDC);
	int iOldROP;
	CBrush* pOldBrush;
	CPen* pOldPen;
	CPen pen;
	CRect rcTracing(m_ptMouseDown,m_ptTracing);
	
	switch(m_uIDMapButton)
	{
	case ID_MAP_PAN://pan
		if(nFlags & MK_LBUTTON)
		{
			SetCursor(AfxGetApp()->LoadCursor(IDC_HANDD));
//			::ZoomPan(m_ptDIBCenter, m_ptMouseDown, point, m_dZoom);
			m_ptDIBCenter.x -= int((point.x-m_ptMouseDown.x)*m_dZoom);
			m_ptDIBCenter.y -= int((point.y-m_ptMouseDown.y)*m_dZoom);
			m_ptMouseDown=point;
			Invalidate();
		}
		else
		{
			SetCursor(AfxGetApp()->LoadCursor(IDC_HANDU));
		}
		break;
	case ID_MAP_ZOOMOUT://Zoom +
//		SetCursor(AfxGetApp()->LoadCursor(IDC_ZOOMOUT));
		break;
	case ID_MAP_ZOOMIN://Zoom -
//		SetCursor(AfxGetApp()->LoadCursor(IDC_ZOOMIN));
		break;
	case ID_MAP_ZOOMWINDOW://zoom window
		if(nFlags & MK_LBUTTON)
		{
			SetCursor(AfxGetApp()->LoadCursor(IDC_ZOOMWINDOW));
			iOldROP = pDC->SetROP2(R2_XORPEN);
			pen.CreatePen(PS_DOT,1,RGB(0,0,255));
			pOldBrush=(CBrush*)pDC->SelectStockObject(NULL_BRUSH);
			pOldPen=pDC->SelectObject(&pen);
		
			rcTracing.NormalizeRect();
			pDC->Rectangle(&rcTracing);

			m_ptTracing = point;
			rcTracing.TopLeft() = m_ptMouseDown;
			rcTracing.BottomRight() = m_ptTracing;
			rcTracing.NormalizeRect();
			pDC->Rectangle(&rcTracing);

			pDC->SelectObject(pOldPen);
			pDC->SelectObject(pOldBrush);
			pDC->SetROP2(iOldROP);
		}
		break;
	case ID_MAP_ACTUAL://view actual size
		break;
	case ID_MAP_FIT://view fit in
		break;
	case ID_MAP_SCISSOR://Scissor
/*
//		if(nFlags & MK_LBUTTON)//if(m_ptTracing.x != 0 || m_ptTracing.y != 0)
		{
			iOldROP = pDC->SetROP2(R2_NOT);
			pDC->MoveTo(0, m_ptTracing.y);
			pDC->LineTo(1024, m_ptTracing.y);
			pDC->MoveTo(m_ptTracing.x, 0);
			pDC->LineTo(m_ptTracing.x, 768);
//			TRACE("\nO:%d\t%d",m_ptTracing.x,m_ptTracing.y);
			m_ptTracing = point;
//			TRACE("\nN:%d\t%d",m_ptTracing.x,m_ptTracing.y);
			pDC->MoveTo(0, m_ptTracing.y);
			pDC->LineTo(1024, m_ptTracing.y);
			pDC->MoveTo(m_ptTracing.x, 0);
			pDC->LineTo(m_ptTracing.x, 768);
			pDC->SetROP2(iOldROP);
		}*/
		SetCursor(AfxGetApp()->LoadCursor(IDC_SCISSOR));
		if(nFlags & MK_LBUTTON)
		{
			iOldROP = pDC->SetROP2(R2_XORPEN);
			pen.CreatePen(PS_DOT,1,RGB(0,0,255));
			pOldBrush=(CBrush*)pDC->SelectStockObject(NULL_BRUSH);
			pOldPen=pDC->SelectObject(&pen);
		
			rcTracing.NormalizeRect();
			pDC->Rectangle(&rcTracing);

			m_ptTracing = point;
			rcTracing.TopLeft() = m_ptMouseDown;
			rcTracing.BottomRight() = m_ptTracing;
			rcTracing.NormalizeRect();
			pDC->Rectangle(&rcTracing);

			pDC->SelectObject(pOldPen);
			pDC->SelectObject(pOldBrush);
			pDC->SetROP2(iOldROP);
		}
		break;
	default:
//		ASSERT(FALSE);
		break;
	}

	CView::OnMouseMove(nFlags, point);
}

void CDIBView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CPen pen, *pOldPen = NULL;
	CDC *pDC = GetDC();
	ASSERT(NULL != pDC);

	switch(m_uIDMapButton)

⌨️ 快捷键说明

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