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

📄 imageslideview.cpp

📁 visual c++数字图像与图形处理中的光盘内容
💻 CPP
字号:
// ImageSlideView.cpp : implementation of the CImageSlideView class
//

#include "stdafx.h"
#include "ImageSlide.h"

#include "ImageSlideDoc.h"
#include "ImageSlideView.h"

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

#include "BorderRect.h"
#include "ImageSlideDialog.h"


/////////////////////////////////////////////////////////////////////////////
// CImageSlideView

IMPLEMENT_DYNCREATE(CImageSlideView, CView)

BEGIN_MESSAGE_MAP(CImageSlideView, CView)
	//{{AFX_MSG_MAP(CImageSlideView)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_COMMAND(IDM_SLIDE_WAY, OnSlideWay)
	ON_UPDATE_COMMAND_UI(IDM_SLIDE_WAY, OnUpdateSlideWay)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImageSlideView construction/destruction

CImageSlideView::CImageSlideView()
{
	// TODO: add construction code here
	m_nWay = left_right;
}

CImageSlideView::~CImageSlideView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CImageSlideView drawing

void CImageSlideView::OnDraw(CDC* pDC)
{
	BeginWaitCursor();
	CDC memDC;
	memDC.CreateCompatibleDC(pDC);
	CBitmap* pOldBitmap = memDC.SelectObject(&m_bitmap);

	
	CRect clientRect;
	GetClientRect(&clientRect);

	int cx = clientRect.Width();
	int cy = clientRect.Height();

	pDC->BitBlt(0,0,cx,cy,&memDC,0,0,SRCCOPY);	
	memDC.SelectObject(pOldBitmap);
	EndWaitCursor();

	//滑入图像,同时光标变为正常光标
	SlideInto(pDC);

}



/////////////////////////////////////////////////////////////////////////////
// CImageSlideView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CImageSlideView message handlers

int CImageSlideView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here

	//打开文件
	CDib* pBackDib = new CDib("back.bmp");

	if (!pBackDib->IsValid())
		MessageBox("文件打开出错","文件 \"back.bmp\"",
			MB_OK | MB_ICONEXCLAMATION);

	//
	CClientDC dc(this);

	m_bitmap.CreateCompatibleBitmap(&dc,800,600);

	CDC memDC;
	memDC.CreateCompatibleDC(&dc);

	CBitmap* pOldBitmap = memDC.SelectObject(&m_bitmap);
	CBrush* pBrush = new CBrush(RGB(255,255,255));
	CRect rect(0,0,800,600);
	memDC.FillRect(rect,pBrush);

	//绘制背景位图
	if (pBackDib)
	{
		BYTE* pBackDibData = pBackDib->GetDibData();
		LPBITMAPINFO pBackBmpInfo = pBackDib->GetBmpInfo();
		int nWidth = (int)pBackDib->GetWidth();
		int nHeight = (int)pBackDib->GetHeight();
		StretchDIBits(memDC.m_hDC, 0, 0, nWidth, nHeight,
			0, 0, nWidth, nHeight, pBackDibData, pBackBmpInfo,
			DIB_RGB_COLORS, SRCCOPY);
	}
	memDC.SelectObject(pOldBitmap);
	delete pBrush;	
	ReleaseDC(&dc);

	if (pBackDib)
	{
		delete pBackDib;
		pBackDib = NULL;
	}
	return 0;
}

void CImageSlideView::OnDestroy() 
{
	CView::OnDestroy();
	
	// TODO: Add your message handler code here
}

//图像滑入
void CImageSlideView::SlideInto(CDC *pDC)
{
	CImageSlideDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

	//第一步,获取在文档中已经打开的位图
	//获取外部DIB信息
	CDib* pDib = pDoc->m_pDib;

	if(!pDib)return;

	int nWidth = (int)pDib->GetWidth();
	int nHeight = (int)pDib->GetHeight();

	//第二步,将位图绘制到内存设备
	CDC memDC;
	memDC.CreateCompatibleDC(pDC);
	CBitmap ddb;
	ddb.CreateCompatibleBitmap(pDC,nWidth,nHeight);
	CBitmap* pOldBitmap = memDC.SelectObject(&ddb);

	//把整个位图绘制到内存,
	//更快的办法是将图像直接绘制到pDC
	pDib->Draw(memDC.m_hDC,0,0,nWidth,nHeight,
				0,0,nWidth,nHeight,DIB_RGB_COLORS,SRCCOPY);

	//第三步,从内存设备中读出位图,实现滑入

	CRect clientRect;
	GetClientRect(&clientRect);

	//左上角点
	int cx = clientRect.Width();
	int cy = clientRect.Height();

	cx = (cx - nWidth) / 2;
	cy = (cy - nHeight) / 2;

	//绘制一个内嵌框
	CBorderRect br(cx - 2,cy - 2,nWidth + 4,nHeight + 4);
	br.Draw(pDC,FALSE);

	//创建剪切区域
	CRgn rgn;
	rgn.CreateRectRgn(cx,cy,cx + nWidth,cy + nHeight);
	pDC->SelectClipRgn(&rgn,RGN_COPY);

	switch(m_nWay)
	{
		//从左滑至右
		case left_right:
		{
			for(int x = (-nWidth + cx + 10) ; x < cx;x += 10)
			{
				pDC->BitBlt(x,cy,nWidth,nHeight,&memDC,0,0,SRCCOPY);
				Delay(25);
			}
			break;
		}
		//从右滑至左
		case right_left:
		{
			for(int x = (nWidth + cx - 10); x > cx;x -= 10)
			{
				pDC->BitBlt(x,cy,nWidth,nHeight,&memDC,0,0,SRCCOPY);
				Delay(25);
			}
			break;
		}

		//从上滑至下
		case top_bottom:
		{
			for(int y = (-nHeight + cy + 10); y < cy;y += 10)
			{
				pDC->BitBlt(cx,y,nWidth,nHeight,&memDC,0,0,SRCCOPY);
				Delay(25);
			}
			break;

		}
		//从下滑至上
		case bottom_top:
		{
			for(int y = (nHeight + cy - 10) ; y > cy;y -= 10)
			{
				pDC->BitBlt(cx,y,nWidth,nHeight,&memDC,0,0,SRCCOPY);
				Delay(25);
			}
			break;
		}
		//从左上滑至右下
		case topLeft_bottomRight:
		{
			//绘制坐标步长,
			int nStepX,nStepY;

			//求出宽度和高度的最大者
			if(nWidth >= nHeight)
			{
				nStepX = 10;
				nStepY = (int)(10.0f * ((float)nHeight / (float)nWidth) + 0.5f);
			}
			else
			{
				nStepY = 10;
				nStepX = (int)(10.0f * ((float)nWidth / (float)nHeight) + 0.5f);
			}

			for(int x = (-nWidth + cx + nStepX),y = (-nHeight + cy + nStepY); x < cx;x += nStepX,y += nStepY)
			{
				pDC->BitBlt(x,y,nWidth,nHeight,&memDC,0,0,SRCCOPY);
				Delay(25);
			}
			break;
		}
		//从右下滑至左上
		case bottomRight_topLeft:
		{
			//绘制坐标步长,
			int nStepX,nStepY;

			//求出宽度和高度的最大者
			if(nWidth >= nHeight)
			{
				nStepX = 10;
				nStepY = (int)(10.0f * ((float)nHeight / (float)nWidth) + 0.5f);
			}
			else
			{
				nStepY = 10;
				nStepX = (int)(10.0f * ((float)nWidth / (float)nHeight) + 0.5f);
			}

			for(int x = (nWidth + cx - nStepX),y = (nHeight + cy - nStepY); x > cx;x -= nStepX,y -= nStepY)
			{
				pDC->BitBlt(x,y,nWidth,nHeight,&memDC,0,0,SRCCOPY);
				Delay(25);
			}
			break;
		}
		//从右上滑至左下
		case topRight_bottomLeft:
		{
			//绘制坐标步长,
			int nStepX,nStepY;

			//求出宽度和高度的最大者
			if(nWidth >= nHeight)
			{
				nStepX = 10;
				nStepY = (int)(10.0f * ((float)nHeight / (float)nWidth) + 0.5f);
			}
			else
			{
				nStepY = 10;
				nStepX = (int)(10.0f * ((float)nWidth / (float)nHeight) + 0.5f);
			}

			for(int x = (nWidth + cx - nStepX),y = (-nHeight + cy + nStepY); x > cx;x -= nStepX,y += nStepY)
			{
				pDC->BitBlt(x,y,nWidth,nHeight,&memDC,0,0,SRCCOPY);
				Delay(25);
			}
	
			break;
		}
		//从左下滑至右上
		case bottomLeft_topRight:
		{
			//绘制坐标步长,
			int nStepX,nStepY;

			//求出宽度和高度的最大者
			if(nWidth >= nHeight)
			{
				nStepX = 10;
				nStepY = (int)(10.0f * ((float)nHeight / (float)nWidth) + 0.5f);
			}
			else
			{
				nStepY = 10;
				nStepX = (int)(10.0f * ((float)nWidth / (float)nHeight) + 0.5f);
			}

			for(int x = (-nWidth + cx + nStepX),y = (nHeight + cy - nStepY); x < cx;x += nStepX,y -= nStepY)
			{
				pDC->BitBlt(x,y,nWidth,nHeight,&memDC,0,0,SRCCOPY);
				Delay(25);
			}

			break;
		}
	}

	//补上最后的剩余部分
	pDC->BitBlt(cx,cy,nWidth,nHeight,&memDC,0,0,SRCCOPY);

	ddb.DeleteObject();
	memDC.SelectObject(pOldBitmap);
	rgn.DeleteObject();
	pDC->SelectClipRgn(NULL,RGN_COPY);

}

void CImageSlideView::Delay(DWORD ms)
{
	DWORD time = GetTickCount();
	do{;}while((GetTickCount() - time) < ms);
}


void CImageSlideView::OnSlideWay() 
{
	// TODO: Add your command handler code here
	CImageSlideDialog dlg;
	dlg.m_nWay = m_nWay;

	int responeDlg = dlg.DoModal();
	if(responeDlg == IDOK)
	{
		m_nWay = dlg.m_nWay;
		Invalidate();
	}	
}

void CImageSlideView::OnUpdateSlideWay(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
}

⌨️ 快捷键说明

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