Click here to Skip to main content
15,895,084 members
Articles / Mobile Apps / Windows Mobile

A Picture Viewer for the Pocket PC 2002

Rate me:
Please Sign up or sign in to vote.
4.79/5 (21 votes)
2 Nov 2003CPOL4 min read 498.6K   685   43  
Putting imgdecmp.lib to work with a few extras.
// PicViewView.cpp : implementation of the CPicViewView class
//

#include "stdafx.h"
#include "PicView.h"

#include "PicViewDoc.h"
#include "PicViewView.h"

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


//---------------------------------------------------------------------------
//
//	CPicViewView
//
//---------------------------------------------------------------------------


IMPLEMENT_DYNCREATE(CPicViewView, CScrollView)

BEGIN_MESSAGE_MAP(CPicViewView, CScrollView)
	//{{AFX_MSG_MAP(CPicViewView)
	ON_WM_CREATE()
	ON_WM_SETFOCUS()
	ON_WM_DESTROY()
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//---------------------------------------------------------------------------
//
//	CPicViewView construction/destruction
//
//---------------------------------------------------------------------------


CPicViewView::CPicViewView()
:	m_bHorz	(TRUE),
	m_bVert	(TRUE)
{
}


CPicViewView::~CPicViewView()
{
}


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

	return CScrollView::PreCreateWindow(cs);
}


BOOL CPicViewView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}


// CPicViewView::ContextMenu
//
//		Displays the context menu
//
void CPicViewView::ContextMenu(CPoint point)
{
	CMenu	mnuCtxt;
	CMenu*	pMenu;
	CWnd*	pWnd;

	if(mnuCtxt.LoadMenu(IDR_MENU_CONTEXT))
	{
		pWnd = AfxGetMainWnd();

		pMenu = mnuCtxt.GetSubMenu(0);
		if(pMenu)
		{
			pMenu->TrackPopupMenu(TPM_LEFTALIGN,
				point.x, point.y, pWnd);
		}
	}
}


//---------------------------------------------------------------------------
//
//	CPicViewView drawing
//
//---------------------------------------------------------------------------


// CPicViewView::OnDraw
//
//		Draw the bitmap at the appropriate zoom
//
void CPicViewView::OnDraw(CDC* pDC)
{
	CPicViewDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	HBITMAP	hBmp;
	CDC		dcMem;

	hBmp = pDoc->GetBitmap();
	if(hBmp)
	{
		CPoint	pt;
		CRect	rcClient;

		GetClientRect(&rcClient);
		pt = GetScrollPosition();

		dcMem.CreateCompatibleDC(NULL);
		dcMem.SelectObject(hBmp);

		pDC->StretchBlt(rcClient.left, rcClient.top, 
						rcClient.right, rcClient.bottom, 
						&dcMem, 
						pDoc->ScaleScreen(pt.x), 
						pDoc->ScaleScreen(pt.y),
						pDoc->ScaleScreen(rcClient.right),
						pDoc->ScaleScreen(rcClient.bottom),
						SRCCOPY);
	}
}


// CPicViewView::ScrollTo
//
//		Scrolls the view to the new position.
//		This is MFC code without the scroll, in order to avoid
//		"jagged" scrolling.
//
void CPicViewView::ScrollTo(CPoint point)
{
    int xMax = GetScrollLimit(SB_HORZ);
    int yMax = GetScrollLimit(SB_VERT);

    if(point.x < 0)
        point.x = 0;
    else if(point.x > xMax)
        point.x = xMax;
    if(point.y < 0)
        point.y = 0;
    else if(point.y > yMax)
        point.y = yMax;

	if(m_bHorz)
		SetScrollPos(SB_HORZ, point.x);
	if(m_bVert)
		SetScrollPos(SB_VERT, point.y);
}


//---------------------------------------------------------------------------
//
//	CPicViewView updating
//
//---------------------------------------------------------------------------


// CPicViewView::OnInitialUpdate
//
//		Initializes the view
//
void CPicViewView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();
}


// CPicViewView::OnUpdate
//
//		Updates the view
//
void CPicViewView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	CSize			sizeTotal;
	CPicViewDoc*	pDoc = GetDocument();

	ASSERT_VALID(pDoc);

	if(pDoc->GetBitmap())
	{
		sizeTotal.cx = pDoc->GetZoomWidth();
		sizeTotal.cy = pDoc->GetZoomHeight();

		SetScrollSizes(MM_TEXT, sizeTotal);

		InvalidateRect(NULL, FALSE);
	}
}


//---------------------------------------------------------------------------
//
//	CPicViewView diagnostics
//
//---------------------------------------------------------------------------


#ifdef _DEBUG


void CPicViewView::AssertValid() const
{
	CScrollView::AssertValid();
}


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


CPicViewDoc* CPicViewView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPicViewDoc)));
	return (CPicViewDoc*)m_pDocument;
}


#endif //_DEBUG


//---------------------------------------------------------------------------
//
//	CPicViewView message handlers
//
//---------------------------------------------------------------------------


int CPicViewView::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
	int iResult = CView::OnCreate(lpCreateStruct);

	if(iResult == 0)
		ShowDoneButton(TRUE);

	return iResult;
}


void CPicViewView::OnSetFocus(CWnd* pOldWnd)
{
	CView::OnSetFocus(pOldWnd);
	//::SHSipPreference(m_hWnd, SIP_UP);
}


void CPicViewView::OnDestroy()
{
	//::SHSipPreference(m_hWnd, SIP_FORCEDOWN);
	CView::OnDestroy();
}


// CPicViewView::OnLButtonDown
//
//		Handles the stylus tap
//
void CPicViewView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	SHRGINFO	shrgi = {0};

	m_ptOrig = point;	// Store the initial point.

	//
	// Handle tap-and-hold
	//
	shrgi.cbSize		= sizeof(SHRGINFO);
	shrgi.hwndClient	= m_hWnd;
	shrgi.ptDown.x		= point.x;
	shrgi.ptDown.y		= point.y;
	shrgi.dwFlags		= SHRG_RETURNCMD;

	if(GN_CONTEXTMENU == ::SHRecognizeGesture(&shrgi))
	{
		ContextMenu(point);
	}
	else
	{
		Default();
	}
}


// CPicViewView::OnMouseMove
//
//		The user is dragging the view.
//		Scroll it accordingly, simulating the "tap-and-scroll"
//
void CPicViewView::OnMouseMove(UINT nFlags, CPoint point) 
{
	CPoint	ptDelta,
			ptScroll,
			pt;

	ptDelta		= m_ptOrig - point;
	m_ptOrig	= point;

	pt = GetScrollPosition();
	pt += ptDelta;

	ScrollTo(pt);
	InvalidateRect(NULL, FALSE);
	UpdateWindow();

	CScrollView::OnMouseMove(nFlags, point);
}


// CPicViewView::OnSize
//
//		The window was resized
//
void CPicViewView::OnSize(UINT nType, int cx, int cy) 
{
	CRect			rc;
	CPicViewDoc*	pDoc = GetDocument();

	ASSERT_VALID(pDoc);

	CScrollView::OnSize(nType, cx, cy);

	GetClientRect(&rc);

	m_bHorz = pDoc->GetZoomWidth()  > rc.Width();
	m_bVert = pDoc->GetZoomHeight() > rc.Height();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Frotcom International
Portugal Portugal
I work on R&D for Frotcom International, a company that develops web-based fleet management solutions.

Comments and Discussions