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

📄 mfcscroll1view.cpp

📁 带滚动条的图像浏览器
💻 CPP
字号:
// MfcScroll1View.cpp : implementation of the CMfcScroll1View class
//

#include "stdafx.h"
#include "MfcScroll1.h"
#include "MainFrm.h"

#include "MfcScroll1Doc.h"
#include "MfcScroll1View.h"
#include "BmpFile.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMfcScroll1View

IMPLEMENT_DYNCREATE(CMfcScroll1View, CScrollView)

BEGIN_MESSAGE_MAP(CMfcScroll1View, CScrollView)
	//{{AFX_MSG_MAP(CMfcScroll1View)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_PAINT()
	ON_COMMAND(ID_FILE_GRDIMAGE, OnFileGrdimage)
	ON_COMMAND(ID_VIEW_PIXEL, OnViewPixel)
	ON_UPDATE_COMMAND_UI(ID_VIEW_PIXEL, OnUpdateViewPixel)
	ON_COMMAND(ID_VIEW_GRAY, OnViewGray)
	ON_UPDATE_COMMAND_UI(ID_VIEW_GRAY, OnUpdateViewGray)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMfcScroll1View construction/destruction

CMfcScroll1View::CMfcScroll1View():m_sizeEllipse(100,-100),m_ptTopLeft(0,0),m_sizeOffset(0,0)
{
	m_bCaptured = 0;
	// new
	m_pMemDc = new CDC;
	m_pBmp   = new CBitmap;
	m_pGrdBmp= new CBmpFile;
	m_hBrush = 0;
	m_bDispPixel = 0;
}

CMfcScroll1View::~CMfcScroll1View()
{
	if( m_hBrush )
		 DeleteObject(m_hBrush);
	delete m_pGrdBmp;
	delete m_pBmp;
	delete m_pMemDc;
}

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

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMfcScroll1View drawing

void CMfcScroll1View::OnDraw(CDC* pDC)
{
	CMfcScroll1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

//	m_pGrdBmp->Draw( pDC, CPoint(0,0), m_sizeTotal);

	CBrush brHatch(HS_DIAGCROSS,RGB(255,0,0));
	CPoint point(0,0);

	pDC->LPtoDP(&point);
	pDC->SetBrushOrg(point);
	CBrush* pOldBrush = pDC->SelectObject(&brHatch);

	pDC->Ellipse(CRect(m_ptTopLeft,m_sizeEllipse));
//	pDC->SelectStockObject(BLACK_BRUSH);
	pDC->SelectObject(pOldBrush);


}

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


	if (m_pMemDc->GetSafeHdc() == NULL) 
	{
		CSize sizeTotal1(800,1050);
		SetScrollSizes(MM_LOENGLISH, sizeTotal1);

		CClientDC dc(this);
		OnPrepareDC(&dc);
//		m_pGrdBmp->LoadBitmap( IDB_BALLOON);

		m_pMemDc->CreateCompatibleDC(&dc);
		m_pMemDc->SetMapMode(MM_LOENGLISH);
		SetViewGround( &dc );
	}
}

/////////////////////////////////////////////////////////////////////////////
// CMfcScroll1View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMfcScroll1View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMfcScroll1View message handlers

void CMfcScroll1View::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CRect rcEllipse(m_ptTopLeft,m_sizeEllipse);
	CRgn  circle;
	CClientDC dc(this);
	OnPrepareDC(&dc);
	dc.LPtoDP( rcEllipse );
	circle.CreateEllipticRgnIndirect( rcEllipse );
	if( circle.PtInRegion( point ) )
	{
		SetCapture();
		m_bCaptured = 1;
		CPoint ptTopLeft( m_ptTopLeft );
		dc.LPtoDP( &ptTopLeft );
		m_sizeOffset = point - ptTopLeft;
		::SetCursor(::LoadCursor( NULL,IDC_CROSS ));
	}

	CScrollView::OnLButtonDown(nFlags, point);
}

void CMfcScroll1View::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if( m_bCaptured )
	{
		::ReleaseCapture( );
		m_bCaptured = 0;
	}
	
	CScrollView::OnLButtonUp(nFlags, point);
}

void CMfcScroll1View::OnMouseMove(UINT nFlags, CPoint point) 
{
	CClientDC dc(this);
	OnPrepareDC(&dc);
	CPoint pt(point);
	dc.DPtoLP( &pt );
	CRect rcTotal( CPoint(0,0), m_sizeTotal);
	rcTotal.NormalizeRect( );
	if( m_bCaptured )
	{
		CRect rcOld(m_ptTopLeft,m_sizeEllipse);
		dc.LPtoDP( rcOld );
		InvalidateRect( rcOld,FALSE);
		m_ptTopLeft = point - m_sizeOffset;
		dc.DPtoLP( &m_ptTopLeft );
		CRect rcNew( m_ptTopLeft,m_sizeEllipse);
		dc.LPtoDP( rcNew );
		InvalidateRect( rcNew,FALSE);
	}
	else if( m_bDispPixel  && m_pGrdBmp->m_pBmih && rcTotal.PtInRect(pt) )
	{
		char aa[20];
		CMainFrame *pMainWnd = (CMainFrame *)AfxGetMainWnd( );
		CStatusBar *p = pMainWnd->GetStatusBar();
		CPoint pt1(0,0);
		dc.DPtoLP( &pt1 );
		pt += pt1;
		dc.LPtoDP( &pt );
		sprintf(aa,"位置:x=%-4d y=%-4d", pt.x,pt.y );
		int i = p->SetPaneText( 1, aa);
		BYTE r,g,b;
		if( m_bDispPixel )
			m_pGrdBmp->GetPixel( pt,r,g,b);
		sprintf(aa,"R:%-3d",r );
		p->SetPaneText( 2, aa);

		sprintf(aa,"G:%-3d",g );
		p->SetPaneText( 3, aa);
		
		sprintf(aa,"B:%-3d",b );
		p->SetPaneText( 4, aa);

	}
	CScrollView::OnMouseMove(nFlags, point);
}

void CMfcScroll1View::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	OnPrepareDC(&dc);
	CRect rcUpdate;
	dc.GetClipBox(&rcUpdate);
	CBitmap* pOldBmp = m_pMemDc->SelectObject(m_pBmp);
	m_pMemDc->SelectClipRgn(NULL);
	m_pMemDc->IntersectClipRect(&rcUpdate);

	CPoint point(0,0);
	m_pMemDc->LPtoDP(&point);
	m_pMemDc->SetBrushOrg(point);

//	CBrush bgBrush(m_pGrdBmp);
	CBrush bgBrush;
	bgBrush.Attach(m_hBrush);

	CBrush* pOldBrush = m_pMemDc->SelectObject(&bgBrush);
	m_pMemDc->PatBlt(rcUpdate.left, rcUpdate.top,
	                    rcUpdate.Width(), rcUpdate.Height(),
	                    PATCOPY);
	OnDraw(m_pMemDc);
	dc.BitBlt(rcUpdate.left, rcUpdate.top,
	          rcUpdate.Width(), rcUpdate.Height(),
	          m_pMemDc, rcUpdate.left, rcUpdate.top,
	          SRCCOPY);
	m_pMemDc->SelectObject(pOldBmp);
	m_pMemDc->SelectObject(pOldBrush);
	bgBrush.Detach();

	// Do not call CScrollView::OnPaint() for painting messages
	//CScrollView::OnPaint();
}

void CMfcScroll1View::CopyBitMap()
{
	if( m_pGrdBmp->m_pBmih )
	{
		CBitmap* pOldBmp = m_pMemDc->SelectObject(m_pBmp);
		m_pGrdBmp->Draw(m_pMemDc, CPoint(0,0), m_sizeTotal);
		m_pMemDc->SelectObject(pOldBmp);
	}
}

void CMfcScroll1View::OnFileGrdimage() 
{
	CFileDialog dlg(TRUE,"bmp","*.bmp");

	if( dlg.DoModal( ) == IDOK )
	{
		CClientDC dc(this);
		OnPrepareDC(&dc);
		if(m_pGrdBmp->LoadFileSection( dlg.GetPathName(), &dc ) )
		{
			SetViewGround( &dc );
			InvalidateRect( NULL );
		}
		else
			m_bDispPixel = 0;
	}
}

void CMfcScroll1View::SetViewGround( CDC *pDC )
{
	CSize sizeTotal;
	if( m_pGrdBmp->m_hBitmap )
	{
		sizeTotal = m_pGrdBmp->GetDimensions();
		CBitmap bmp;
		bmp.Attach(m_pGrdBmp->m_hBitmap);
		CBrush bgBrush( &bmp );
		if( m_hBrush )
			 DeleteObject(m_hBrush);
		m_hBrush = (HBRUSH)bgBrush.Detach();
		bmp.Detach();
	}
	else
	{
		sizeTotal = CSize( 800,1050);
	}

	pDC->DPtoLP(&sizeTotal);
	CSize sizePage(sizeTotal.cx/2,sizeTotal.cy/2);
	CSize sizeLine(sizeTotal.cx/50,sizeTotal.cy/50);
	SetScrollSizes(MM_LOENGLISH, sizeTotal,sizePage,sizeLine);

	CRect rectMax(0, 0, sizeTotal.cx, -sizeTotal.cy);
	m_sizeTotal = CSize(sizeTotal.cx, -sizeTotal.cy);

	pDC->LPtoDP(rectMax);
	// makes bitmap same size as display window
	if( m_pBmp->GetSafeHandle( ) != NULL )
		m_pBmp->DeleteObject( );
	m_pBmp->CreateCompatibleBitmap( pDC, rectMax.right,
		                                rectMax.bottom);
	CopyBitMap( );
}

void CMfcScroll1View::OnViewPixel()
{
	m_bDispPixel = !m_bDispPixel;
}

void CMfcScroll1View::OnUpdateViewPixel(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable( m_pGrdBmp->m_pBmih != NULL );
	pCmdUI->SetCheck(m_bDispPixel);
}

void CMfcScroll1View::OnViewGray() 
{
	CClientDC dc(this);
	OnPrepareDC(&dc);
	m_pGrdBmp->GrayImage( &dc );
	m_pGrdBmp->MakePalette();
	if( m_pGrdBmp->m_hBitmap )
	{
		CBitmap bmp;
		bmp.Attach(m_pGrdBmp->m_hBitmap);
		CBrush bgBrush( &bmp );
		if( m_hBrush )
			 DeleteObject(m_hBrush);
		m_hBrush = (HBRUSH)bgBrush.Detach();
		bmp.Detach();
	}
	InvalidateRect( NULL );
}

void CMfcScroll1View::OnUpdateViewGray(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable( m_pGrdBmp->m_pBmih != NULL && !m_pGrdBmp->m_bGray );
}

⌨️ 快捷键说明

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