📄 imagezoominview.cpp
字号:
// ImageZoomInView.cpp : implementation of the CImageZoomInView class
//
#include "stdafx.h"
#include "ImageZoomIn.h"
#include "ImageZoomInDoc.h"
#include "ImageZoomInView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "BorderRect.h"
/////////////////////////////////////////////////////////////////////////////
// CImageZoomInView
IMPLEMENT_DYNCREATE(CImageZoomInView, CView)
BEGIN_MESSAGE_MAP(CImageZoomInView, CView)
//{{AFX_MSG_MAP(CImageZoomInView)
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CImageZoomInView construction/destruction
CImageZoomInView::CImageZoomInView()
{
// TODO: add construction code here
}
CImageZoomInView::~CImageZoomInView()
{
}
BOOL CImageZoomInView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CImageZoomInView drawing
void CImageZoomInView::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();
ZoomInto(pDC);
}
/////////////////////////////////////////////////////////////////////////////
// CImageZoomInView diagnostics
#ifdef _DEBUG
void CImageZoomInView::AssertValid() const
{
CView::AssertValid();
}
void CImageZoomInView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CImageZoomInDoc* CImageZoomInView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CImageZoomInDoc)));
return (CImageZoomInDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CImageZoomInView message handlers
int CImageZoomInView::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 CImageZoomInView::Delay(DWORD ms)
{
DWORD time = GetTickCount();
do{;}while((GetTickCount() - time) < ms);
}
void CImageZoomInView::ZoomInto(CDC *pDC)
{
CImageZoomInDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//第一步, 获取在文档中已经打开的位图
//获取外部DIB信息
CDib* pDib = pDoc->m_pDib;
if(!pDib)return;
BYTE* pDibData = pDib->GetDibData();
LPBITMAPINFO pBmpInfo = pDib->GetBmpInfo();
int nWidth = (int)pDib->GetWidth();
int nHeight = (int)pDib->GetHeight();
//第二步, 绘制一个嵌入矩形框
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);
//第三步, 推进图像
//绘制坐标步长,
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);
}
//这里只实现从中央向四周推进的效果
//图像中心坐标
int nCenterX = cx + (nWidth / 2);
int nCenterY = cy + (nHeight / 2);
//
//如果图像为8, 4, 2位颜色位图
if(pDib->GetRGBQuad())
{
//实现逻辑调色盘
HPALETTE hPalette = pDib->CreateBitmapPalette();
HPALETTE hOldPalette = ::SelectPalette(pDC->m_hDC, hPalette, FALSE);
::RealizePalette(pDC->m_hDC);
//绘制
for(int x = nStepX, y = nStepY; x < nWidth / 2;x += nStepX, y += nStepY)
{
StretchDIBits(pDC->m_hDC, (nCenterX - x), (nCenterY - y), (x + x), (y + y),
0, 0, nWidth, nHeight,
pDibData, pBmpInfo, DIB_RGB_COLORS, SRCCOPY);
Delay(25);
}
StretchDIBits(pDC->m_hDC, cx, cy, nWidth, nHeight,
0, 0, nWidth, nHeight,
pDibData, pBmpInfo, DIB_RGB_COLORS, SRCCOPY);
//释放
::SelectPalette(pDC->m_hDC, hOldPalette, FALSE);
::DeleteObject(hPalette);
}
else
{
for(int x = nStepX, y = nStepY; x < nWidth / 2;x += nStepX, y += nStepY)
{
StretchDIBits(pDC->m_hDC, (nCenterX - x), (nCenterY - y), (x + x), (y + y),
0, 0, nWidth, nHeight,
pDibData, pBmpInfo, DIB_RGB_COLORS, SRCCOPY);
Delay(25);
}
StretchDIBits(pDC->m_hDC, cx, cy, nWidth, nHeight,
0, 0, nWidth, nHeight,
pDibData, pBmpInfo, DIB_RGB_COLORS, SRCCOPY);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -