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

📄 imageviewwnd.cpp

📁 Under CE4.2 view bitmap with shrink and magnify
💻 CPP
字号:
//////////////////////////////////////////////////////////////////////////
//ImageViewWnd.cpp: implementation file

#include "stdafx.h"
#include "MainFrm.h"
#include "ImageViewWnd.h"

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

//Define global objects
#define ID_BTN_MAGNIFY                  200
#define ID_BTN_SHRINK                   201
#define ID_BTN_MENU						202
#define ID_BTN_SATELLITE				203
#define ID_BTN_INFO						204
#define ID_BTN_QUIT                     205

//////////////////////////////////////////////////////////////////////////
//CImageViewWnd

CImageViewWnd::CImageViewWnd()
{
	m_ncxImageWidth  =    240;
	m_ncyImageHeight =    320;
    m_nxImageBegin   =      0;
	m_nyImageBegin   =      0;

	m_dZoomScale     =    1.0;

	m_nxSrcBegin     =      0;
	m_nySrcBegin     =      0;
	m_ncxSrcWidth    =    240;
	m_ncySrcHeight   =    320;

	m_bMoveFlag      =  false;	
	m_ptStartMove    =  (0,0);
	m_nxImgMove      =      0;
	m_nyImgMove      =      0;
    m_nxAbsoluteImgMove  =  0;
	m_nyAbsoluteImgMove  =  0;

	m_mode = Location_mode;	//default is Location Mode

	m_dZoomScaleStep =    0.2;
}

CImageViewWnd::~CImageViewWnd()
{
}

void CImageViewWnd::SetViewMode(ImageViewMode mode)
{
	if (m_mode == Location_mode)
	{
		m_xbtnMenu.Show();
		m_xbtnSatellite.Show();
		m_xbtnInfo.Show();
		m_mode = Picture_mode;
	}
	else
	{
		m_xbtnMenu.Hide();
		m_xbtnSatellite.Hide();
		m_xbtnInfo.Hide();
		m_mode = Location_mode;
	}
}

CImageViewWnd::ImageViewMode CImageViewWnd::GetViewMode()
{	
	return m_mode;
}	

BEGIN_MESSAGE_MAP(CImageViewWnd, CWnd)
	//{{AFX_MSG_MAP(CTransferSelectWnd)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
//	ON_BN_CLICKED(ID_BTN_MAGNIFY,OnBtnMagnify)
//	ON_BN_CLICKED(ID_BTN_SHRINK, OnBtnShrink)
	ON_BN_CLICKED(ID_BTN_MENU,   OnBtnMenu)
	ON_BN_CLICKED(ID_BTN_SATELLITE,   OnBtnSatellite)
	ON_BN_CLICKED(ID_BTN_INFO,   OnBtnInfo)

//	ON_MESSAGE(WM_TRANSLUCENTBTN, OnTranslucent)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImageViewWnd message handlers

int CImageViewWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO: Add your specialized creation code here
	CDC *pDC = GetDC();

	m_memDC.CreateMemDC(240,320);
	m_memSrcDC.CreateMemDC(240,320);
	
	//HBITMAP hBmp=SHLoadDIBitmap(L"\\test.bmp");
    HBITMAP hBmp = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BG));
	BITMAP bmp;

    GetObject(hBmp,sizeof(bmp),&bmp);
	
	//Get the width and height of original image
	m_ncxImageWidth = bmp.bmWidth;
	m_ncyImageHeight = bmp.bmHeight;
	CBitmap bmp2;
	
    HDC hDCTemp=CreateCompatibleDC(pDC->m_hDC);
    HBITMAP hBmpOld=(HBITMAP)SelectObject(hDCTemp,hBmp);

    StretchBlt(m_memSrcDC.m_hDC,0,0,240,320,hDCTemp,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);

	::BitBlt(m_memDC.m_hDC, 0, 0, 240, 320, m_memSrcDC.m_hDC, 0, 0, SRCCOPY);

	SelectObject(hDCTemp,hBmpOld);

	//Create buttons
	//m_xbtnMagnify.Create(NULL, WS_CHILD|WS_VISIBLE,  CRect(197,277,197+43,277+43), this, ID_BTN_MAGNIFY);
	//m_xbtnMagnify.LoadBitmaps(IDB_BTN_MAGNIFY_UP,IDB_BTN_MAGNIFY_DOWN,IDB_BTN_MAGNIFY_DIS);

	//m_xbtnShrink.Create(NULL, WS_CHILD|WS_VISIBLE,  CRect(0,277,43,277+43), this, ID_BTN_SHRINK);
	//m_xbtnShrink.LoadBitmaps(IDB_BTN_SHRINK_UP,IDB_BTN_SHRINK_DOWN,IDB_BTN_SHRINK_DIS);

	m_xbtnMenu.Create(NULL, WS_CHILD,  CRect(0,0,65,43), this, ID_BTN_MENU);
	m_xbtnMenu.LoadBitmaps(IDB_BTN_MENU, IDB_BTN_MENU_DOWN, IDB_BTN_MENU);
	//m_xbtnMenu.Show();

	m_xbtnSatellite.Create(NULL, WS_CHILD,  CRect(0,43,65,82), this, ID_BTN_SATELLITE);
	m_xbtnSatellite.LoadBitmaps(IDB_BTN_SATELLITE, IDB_BTN_SATELLITE_DOWN, IDB_BTN_SATELLITE);
	//m_xbtnSatellite.Show();

	m_xbtnInfo.Create(NULL, WS_CHILD,  CRect(0,82,65,123), this, ID_BTN_INFO);
	m_xbtnInfo.LoadBitmaps(IDB_BTN_INFO, IDB_BTN_INFO_DOWN, IDB_BTN_INFO);
	//m_xbtnInfo.Show();



	CXButton m_btn_satellite;
	CXButton m_btn_info;

	m_trsWndMagnify.Create(this, CRect(197,277,197+43,277+43), ID_BTN_MAGNIFY);
	m_trsWndMagnify.SetBkMem(&m_memDC);
	m_trsWndMagnify.SetIcon(IDI_MAGNIFY_UP, IDI_MAGNIFY_DOWN);
	m_trsWndMagnify.ShowWindow(SW_HIDE);
//	m_trsWndMagnify.SetFillMode(1);

	m_trsWndShrink.Create(this, CRect(0,277,43,277+43), ID_BTN_SHRINK);
    m_trsWndShrink.SetBkMem(&m_memDC);
	m_trsWndShrink.SetIcon(IDI_SHRINK_UP, IDI_SHRINK_DOWN);
	m_trsWndShrink.ShowWindow(SW_HIDE);
//	m_trsWndShrink.SetFillMode(1);

	return 0;
}
void CImageViewWnd::UpdateMemDC()
{
     m_trsWndShrink.Popup();
	 m_trsWndMagnify.Popup();
}

void CImageViewWnd::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
    UpdateMemDC();
	// Do not call CWnd::OnPaint() for painting messages
	::BitBlt(dc.m_hDC,0,0,240,320,m_memDC.m_hDC,0,0,SRCCOPY);
}

BOOL CImageViewWnd::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	
	//return CWnd::OnEraseBkgnd(pDC);
/*	
	HBITMAP hBmp=SHLoadDIBitmap(L"\\test.bmp");
    BITMAP bmp;

    GetObject(hBmp,sizeof(bmp),&bmp);

    HDC hDCTemp=CreateCompatibleDC(pDC->m_hDC);
    HBITMAP hBmpOld=(HBITMAP)SelectObject(hDCTemp,hBmp);

    BitBlt(m_memDC.m_hDC,0,0,bmp.bmWidth,bmp.bmHeight,hDCTemp,0,0,SRCCOPY);

	SelectObject(hDCTemp,hBmpOld);
*/
	return TRUE;
}

void CImageViewWnd::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CRect rtBtnShrink(0,277,43,277+43);
	CRect rtBtnMagnify(197,277,197+43,277+43);

	if(rtBtnShrink.PtInRect(point))
	{
		OnBtnShrink();
	}
	else if(rtBtnMagnify.PtInRect(point))
	{
		OnBtnMagnify();
	}
	else
	{
		if (Location_mode == m_mode)
		{
			SetViewMode(Picture_mode);
		}
		else
		{
			if(m_dZoomScale > 1.0
				&& !rtBtnShrink.PtInRect(point) 
				&& !rtBtnMagnify.PtInRect(point))
			{
				m_bMoveFlag = TRUE;
				m_ptStartMove = point;		
			}
		}
	}

	CWnd::OnLButtonDown(nFlags, point);
}

void CImageViewWnd::OnLButtonUp(UINT nFlags, CPoint point)
{
	if(m_bMoveFlag)
	{
		m_bMoveFlag = FALSE;		
	}
	CWnd::OnLButtonUp(nFlags, point);
}

void CImageViewWnd::OnMouseMove(UINT nFlags, CPoint point)
{
	if(m_bMoveFlag)
	{
        m_nxImgMove = m_ptStartMove.x - point.x;
		m_nyImgMove = m_ptStartMove.y - point.y;
		m_ptStartMove = point;
		m_nxAbsoluteImgMove += m_nxImgMove;
		m_nyAbsoluteImgMove += m_nyImgMove;
		DragImage(m_nxAbsoluteImgMove, m_nyAbsoluteImgMove);
//		UpdateMemDC();
		Invalidate();
		PostMessage(WM_PAINT);
	}
	CWnd::OnMouseMove(nFlags, point);
}

void CImageViewWnd::DragImage(int ncxOffset, int ncyOffset)
{
    int x, y, cx, cy;

	x    =  (m_ncxSrcWidth - (int)(m_ncxSrcWidth/m_dZoomScale))/2  
		    + int(ncxOffset/m_dZoomScale);
	y    =  (m_ncySrcHeight- (int)(m_ncySrcHeight/m_dZoomScale))/2
		    + int(ncyOffset/m_dZoomScale);
    
	if(x<0 || x>(240-(int)(m_ncxSrcWidth/m_dZoomScale)))
	{
		x = (x<0) ? 0 : (240 - (int)(m_ncxSrcWidth/m_dZoomScale));
	}
	
	if(y<0 || y>(320-(int)(m_ncySrcHeight/m_dZoomScale)))
	{
		y = (y<0) ? 0 : (320-(int)(m_ncySrcHeight/m_dZoomScale));
	}

	cx   =  (int)(m_ncxSrcWidth/m_dZoomScale);
	cy   =  (int)(m_ncySrcHeight/m_dZoomScale);

	::StretchBlt(m_memDC.m_hDC, 0, 0, 240, 320, m_memSrcDC.m_hDC,
		         x, y, cx, cy, SRCCOPY);

}
//////////////////////////////////////////////////////////////////////////
//Buttons clicked message handler
void CImageViewWnd::OnBtnMagnify()
{
    m_dZoomScale += m_dZoomScaleStep;
    
	int x, y, cx, cy;
	x    =  (m_ncxSrcWidth - (int)(m_ncxSrcWidth/m_dZoomScale))/2;
	y    =  (m_ncySrcHeight- (int)(m_ncySrcHeight/m_dZoomScale))/2;

	cx   =  (int)(m_ncxSrcWidth/m_dZoomScale);
	cy   =  (int)(m_ncySrcHeight/m_dZoomScale);

	::StretchBlt(m_memDC.m_hDC, 0, 0, 240, 320, m_memSrcDC.m_hDC,
		         x, y, cx, cy, SRCCOPY);

	Invalidate();

}

void CImageViewWnd::OnBtnShrink()
{
	if(m_dZoomScale <= 1)
		return;

	m_dZoomScale  -=  m_dZoomScaleStep;

	int x, y, cx, cy;
	x    =  (m_ncxSrcWidth - (int)(m_ncxSrcWidth/m_dZoomScale))/2;
	y    =  (m_ncySrcHeight- (int)(m_ncySrcHeight/m_dZoomScale))/2;

	cx   =  (int)(m_ncxSrcWidth/m_dZoomScale);
	cy   =  (int)(m_ncySrcHeight/m_dZoomScale);

	::StretchBlt(m_memDC.m_hDC, 0, 0, 240, 320, m_memSrcDC.m_hDC,
		         x, y, cx, cy, SRCCOPY);

	Invalidate();
	
}

void CImageViewWnd::OnBtnMenu()
{
	OnBtnQuit();
}

void CImageViewWnd::OnBtnSatellite()
{
	SetViewMode(Location_mode);
}

void CImageViewWnd::OnBtnInfo()
{
	RECT rc = {0,0,240,320};
	::FillRect(::GetDC(GetSafeHwnd()), &rc, CreateSolidBrush(RGB(0,0,80)));
	
	Invalidate();
	PostMessage(WM_PAINT);

}

void CImageViewWnd::OnBtnQuit()
{
	GetParent()->PostMessage(WM_CLOSE);
}

/*
LRESULT CImageViewWnd::OnTranslucent(WPARAM wParam, LPARAM lParam)
{
    UINT nID = (UINT)wParam;
	switch(nID)
	{
	case ID_BTN_MAGNIFY:
		OnBtnMagnify();
		break;
	case ID_BTN_SHRINK:
		OnBtnShrink();
		break;
	default:
		break;
	}
	return TRUE;	
}
*/

⌨️ 快捷键说明

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