Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

An Image (GIF, JPEG, BMP, ICO, WMF and EMF) Viewer

, 25 Oct 2002
A sample that can load, display, and print graphics files.
imgviewer.zip
Release
ImgViewer.exe
res
ImgViewer.ico
ImgViewerDoc.ico
Toolbar.bmp
ImgViewer.clw
ImgViewer.dsp
ImgViewer.dsw
// ImgViewerView.cpp : implementation of the CImgViewerView class
//

#include "stdafx.h"
#include "ImgViewer.h"

#include "ImgViewerDoc.h"
#include "ImgViewerView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CImgViewerView

IMPLEMENT_DYNCREATE(CImgViewerView, CScrollView)

BEGIN_MESSAGE_MAP(CImgViewerView, CScrollView)
	//{{AFX_MSG_MAP(CImgViewerView)
	ON_WM_CREATE()
	ON_WM_ERASEBKGND()
	ON_WM_DROPFILES()
	ON_WM_CONTEXTMENU()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImgViewerView construction/destruction

CImgViewerView::CImgViewerView()
{
}

CImgViewerView::~CImgViewerView()
{
}

/////////////////////////////////////////////////////////////////////////////
// CImgViewerView drawing

void CImgViewerView::OnDraw(CDC *pDC)
{
	CImgViewerDoc *const pDoc = this->GetDocument();
	ASSERT_VALID(pDoc);

	if (pDoc->m_pPicture != NULL)
	{
		// get palette
		HPALETTE hPal = NULL;
		VERIFY(SUCCEEDED(pDoc->m_pPicture->get_hPal(reinterpret_cast<OLE_HANDLE *>(&hPal))));

		CPalette *pPalOld = NULL;
		if (hPal != NULL)
		{
			TRACE(_T("CImgViewerView::OnDraw - RealizePalette\n"));
			pPalOld = pDC->SelectPalette(CPalette::FromHandle(hPal), FALSE);
			pDC->RealizePalette();
		}

		// transparent?
		DWORD dwAttr = 0;
		if (FAILED(pDoc->m_pPicture->get_Attributes(&dwAttr)) ||
			(dwAttr & PICTURE_TRANSPARENT))
		{
			TRACE(_T("CImgViewerView::OnDraw - Transparent\n"));

			// use an off-screen DC to prevent flickering
			CDC MemDC;
			VERIFY(MemDC.CreateCompatibleDC(pDC));
			CBitmap Bmp;
			VERIFY(Bmp.CreateCompatibleBitmap(pDC,
											  pDoc->m_sizeInPix.cx,
											  pDoc->m_sizeInPix.cy));

			CBitmap *pBmpOld = MemDC.SelectObject(&Bmp);
			CPalette *pPalMemOld = NULL;
			if (hPal != NULL)
			{
				pPalMemOld = MemDC.SelectPalette(CPalette::FromHandle(hPal), FALSE);
				MemDC.RealizePalette();
			}

			const RECT rc = { 0, 0, pDoc->m_sizeInPix.cx, pDoc->m_sizeInPix.cy };

			MemDC.FillSolidRect(&rc, ::GetSysColor(COLOR_WINDOW));

			// display picture using IPicture::Render
			VERIFY(SUCCEEDED(pDoc->m_pPicture->Render(MemDC.GetSafeHdc(),
													  0,
													  0,
													  pDoc->m_sizeInPix.cx,
													  pDoc->m_sizeInPix.cy,
													  0,
													  pDoc->m_sizeInHiMetric.cy,
													  pDoc->m_sizeInHiMetric.cx,
													  -pDoc->m_sizeInHiMetric.cy,
													  &rc)));

			VERIFY(pDC->BitBlt(0, 0, pDoc->m_sizeInPix.cx, pDoc->m_sizeInPix.cy,
							   &MemDC, 0, 0, SRCCOPY));

			MemDC.SelectObject(pBmpOld);

			if (pPalMemOld != NULL)
			{
				ASSERT(hPal != NULL);
				MemDC.SelectPalette(pPalMemOld, FALSE);
			}
		}
		else
		{
			RECT rc;
			this->GetClientRect(&rc);

			// display picture using IPicture::Render
			VERIFY(SUCCEEDED(pDoc->m_pPicture->Render(pDC->GetSafeHdc(),
													  0,
													  0,
													  pDoc->m_sizeInPix.cx,
													  pDoc->m_sizeInPix.cy,
													  0,
													  pDoc->m_sizeInHiMetric.cy,
													  pDoc->m_sizeInHiMetric.cx,
													  -pDoc->m_sizeInHiMetric.cy,
													  &rc)));
		}

		if (pPalOld != NULL)
		{
			ASSERT(hPal != NULL);
			pDC->SelectPalette(pPalOld, FALSE);
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CImgViewerView printing

BOOL CImgViewerView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CImgViewerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CImgViewerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CImgViewerView diagnostics

#ifdef _DEBUG
void CImgViewerView::AssertValid() const
{
	CScrollView::AssertValid();
}

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

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

/////////////////////////////////////////////////////////////////////////////
// CImgViewerView message handlers

int CImgViewerView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CScrollView::OnCreate(lpCreateStruct) == -1)
	{
		return -1;
	}
	
	this->DragAcceptFiles();

	return 0;
}

BOOL CImgViewerView::OnEraseBkgnd(CDC *pDC)
{
	CBrush br(::GetSysColor(COLOR_WINDOW));
    this->FillOutsideRect(pDC, &br);

	return TRUE;
}

void CImgViewerView::OnDropFiles(HDROP hDropInfo) 
{
	CScrollView::OnDropFiles(hDropInfo);

	// SDI, get the first file name.
	TCHAR lpszFile[MAX_PATH];
	VERIFY(::DragQueryFile(hDropInfo, 0, lpszFile, MAX_PATH) > 0);
	::DragFinish(hDropInfo);

	ASSERT_VALID(::AfxGetApp());
	::AfxGetApp()->OpenDocumentFile(lpszFile);
}

void CImgViewerView::OnUpdate(CView *pSender, LPARAM lHint, CObject *pHint) 
{
	CScrollView::OnUpdate(pSender, lHint, pHint);

	CImgViewerDoc *const pDoc = this->GetDocument();
	ASSERT_VALID(pDoc);

	this->SetScrollSizes(MM_TEXT,
						 pDoc->m_pPicture != NULL
						 ? CSize(pDoc->m_sizeInPix.cx, pDoc->m_sizeInPix.cy)
						 : CSize(0, 0));

#ifdef _DEBUG
	pDoc->Dump(afxDump);
#endif //_DEBUG
}

void CImgViewerView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) 
{
	CScrollView::OnPrepareDC(pDC, pInfo);

	if (pDC->IsPrinting())
	{
		// Set the mapping mode so that the image is printed at the same
		// size as on the screen. Note that images may be too large to be
		// printed completely.
		CDC ScreenDC;
		VERIFY(ScreenDC.CreateIC(_T("DISPLAY"), NULL, NULL, NULL));
		pDC->SetMapMode(MM_ANISOTROPIC);
		pDC->SetWindowExt(ScreenDC.GetDeviceCaps(LOGPIXELSX),
						  ScreenDC.GetDeviceCaps(LOGPIXELSY));
		pDC->SetViewportExt(pDC->GetDeviceCaps(LOGPIXELSX),
							pDC->GetDeviceCaps(LOGPIXELSY));
		pDC->SetWindowOrg(0, 0);
		pDC->SetViewportOrg(0, 0);
	}
}

void CImgViewerView::OnContextMenu(CWnd* /*pWnd*/, CPoint point) 
{
	CWnd *const pWndMain = ::AfxGetMainWnd();
	if (pWndMain != NULL)
	{
		ASSERT_VALID(pWndMain);

		// Get the main window's menu
		CMenu *const pMenu = pWndMain->GetMenu();
		if (pMenu != NULL)
		{
			ASSERT_VALID(pMenu);

			CString strPrint, strPrintPreview, strSend, strProperties;

			VERIFY(pMenu->GetMenuString(ID_FILE_PRINT,
										strPrint,
										MF_BYCOMMAND) > 0);
			VERIFY(pMenu->GetMenuString(ID_FILE_PRINT_PREVIEW,
										strPrintPreview,
										MF_BYCOMMAND) > 0);
			VERIFY(pMenu->GetMenuString(ID_FILE_SEND_MAIL,
										strSend,
										MF_BYCOMMAND) > 0);
			VERIFY(pMenu->GetMenuString(ID_FILE_PROPERTIES,
										strProperties,
										MF_BYCOMMAND) > 0);

			CMenu menu;
			VERIFY(menu.CreatePopupMenu());
			VERIFY(menu.AppendMenu(MF_STRING, ID_FILE_PRINT, strPrint));
			VERIFY(menu.AppendMenu(MF_STRING, ID_FILE_PRINT_PREVIEW, strPrintPreview));
			VERIFY(menu.AppendMenu(MF_SEPARATOR, 0));
			VERIFY(menu.AppendMenu(MF_STRING, ID_FILE_SEND_MAIL, strSend));
			VERIFY(menu.AppendMenu(MF_STRING, ID_FILE_PROPERTIES, strProperties));

			VERIFY(menu.TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON,
									   point.x, point.y,
									   this));
		}
		else
		{
			ASSERT(FALSE);
		}
	}
	else
	{
		ASSERT(FALSE);
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Chen Su
Software Developer
Canada Canada
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 26 Oct 2002
Article Copyright 2002 by Chen Su
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid