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

📄 imagewnd.cpp

📁 阮秋琦的数字图像处理学一书所附的代码
💻 CPP
字号:
// ImageWnd.cpp : implementation file
//

#include "stdafx.h"
#include "DIPDemo.h"
#include "ImageWnd.h"

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

/////////////////////////////////////////////////////////////////////////////
// CImageWnd

CImageWnd::CImageWnd()
{
	HScrollPosition=0;
	HCurrentPosition=0;
	HScrollMax=0;
	VScrollPosition=0;
	VCurrentPosition=0;
	VScrollMax=0;
	lpBits=0;
}

CImageWnd::~CImageWnd()
{
	if (lpBits) delete lpBits;
}


BEGIN_MESSAGE_MAP(CImageWnd, CWnd)
	//{{AFX_MSG_MAP(CImageWnd)
	ON_WM_ERASEBKGND()
	ON_WM_HSCROLL()
	ON_WM_VSCROLL()
	ON_WM_PAINT()
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CImageWnd message handlers
#define BACKGROUND RGB(128,128,128)
BOOL CImageWnd::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
//	if (!lpBits) pDC->FillSolidRect(0,0,320,240,BACKGROUND);
//	return CWnd::OnEraseBkgnd(pDC);
	if (!lpBits || nWidth<320 || nHeight<240)
	{
		pDC->BitBlt(0,0,320,240,&memdc,10,10,SRCCOPY);
	}
	return TRUE;
}

void CImageWnd::SetScroll(int cx, int cy)
{
	HScrollPosition=0;
	HCurrentPosition=0;
	VScrollPosition=0;
	VCurrentPosition=0;
	SetScrollPos(SB_HORZ,0);
	SetScrollPos(SB_VERT,0);

	SCROLLINFO sinfo;
	sinfo.cbSize=sizeof(SCROLLINFO);
	sinfo.fMask=SIF_PAGE|SIF_RANGE;
	sinfo.nMin=0;

	//HScroll
	sinfo.nMax=cx-1;
	sinfo.nPage=302;
	SetScrollInfo(SB_HORZ,&sinfo);
	HScrollMax=cx-302;

	//VScroll
	sinfo.nMax=cy-1;
	sinfo.nPage=222;
	SetScrollInfo(SB_VERT,&sinfo);
	VScrollMax=cy-222;
}

void CImageWnd::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	switch(nSBCode)
	{
	case SB_LINEDOWN:
		HScrollPosition+=5;
		break;
	case SB_LINEUP:
		HScrollPosition-=5;
		break;
	case SB_PAGEDOWN:
		HScrollPosition+=302;
		break;
	case SB_PAGEUP:
		HScrollPosition-=302;
		break;
	case SB_THUMBPOSITION:
	case SB_THUMBTRACK:
		HScrollPosition=nPos;
		break;
	}
	if (HScrollPosition<0) HScrollPosition=0;
	if (HScrollPosition>HScrollMax) HScrollPosition=HScrollMax;

	SetScrollPos(SB_HORZ,HScrollPosition);
	if (HCurrentPosition!=HScrollPosition)
	{
		ScrollWindow((HCurrentPosition-HScrollPosition),0);
		HCurrentPosition=HScrollPosition;
	}
	CWnd::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CImageWnd::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	switch(nSBCode)
	{
	case SB_LINEDOWN:
		VScrollPosition+=5;
		break;
	case SB_LINEUP:
		VScrollPosition-=5;
		break;
	case SB_PAGEDOWN:
		VScrollPosition+=222;
		break;
	case SB_PAGEUP:
		VScrollPosition-=222;
		break;
	case SB_THUMBPOSITION:
	case SB_THUMBTRACK:
		VScrollPosition=nPos;
		break;
	}
	if (VScrollPosition<0) VScrollPosition=0;
	if (VScrollPosition>VScrollMax) VScrollPosition=VScrollMax;

	SetScrollPos(SB_VERT,VScrollPosition);
	if (VCurrentPosition!=VScrollPosition)
	{
		ScrollWindow(0,(VCurrentPosition-VScrollPosition));
		VCurrentPosition=VScrollPosition;
	}
	CWnd::OnVScroll(nSBCode, nPos, pScrollBar);
}

void CImageWnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
//	dc.MoveTo(-HScrollPosition,-VScrollPosition);
//	dc.LineTo(-HScrollPosition+100,-VScrollPosition+100);
	if (lpBits)
	{
		int x,y;
		x=-HScrollPosition;
		y=-VScrollPosition;
/*		if (nWidth<320 || nHeight<240)
		{
			dc.FillSolidRect(0,0,320,240,BACKGROUND);
		}*/
		if (nWidth<320) x=(320-nWidth)/2;
		if (nHeight<240) y=(240-nHeight)/2;
		BITMAPINFOHEADER bmi;
		bmi.biSize=sizeof(BITMAPINFOHEADER);
		bmi.biWidth=nWidth;
		bmi.biHeight=nHeight;
		bmi.biPlanes=1;
		bmi.biBitCount=24;
		bmi.biCompression=BI_RGB;
		bmi.biSizeImage=0;
		bmi.biXPelsPerMeter=0;
		bmi.biYPelsPerMeter=0;
		bmi.biClrUsed=0;
		bmi.biClrImportant=0;
		StretchDIBits(dc.m_hDC,x,y,nWidth,nHeight,0,0,nWidth,nHeight,
		lpBits,
		(BITMAPINFO *)&bmi,
		DIB_RGB_COLORS,
		SRCCOPY);
	}
	// Do not call CWnd::OnPaint() for painting messages
}

void CImageWnd::SetImage(int cx, int cy, const void *bits)
{
	nWidth=cx;
	nHeight=cy;
	nByteWidth=nWidth*3;
	if (nByteWidth%4) nByteWidth+=4-(nByteWidth%4);
	if (lpBits) delete lpBits;
	lpBits=new BYTE[nByteWidth*nHeight];
	memcpy(lpBits,bits,nByteWidth*nHeight);
	if (cx>320 || cy>240) SetScroll(cx,cy);
	else SetScroll(0,0);
	Invalidate();
}

int CImageWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	CClientDC dc(this);
	memdc.CreateCompatibleDC(&dc);
	backbm.LoadBitmap(IDB_FRAME);
	memdc.SelectObject(backbm);

	return 0;
}

⌨️ 快捷键说明

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