Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / WTL

WTLIPicture Viewer: An Update to the WTL BmpView Sample Application

Rate me:
Please Sign up or sign in to vote.
4.64/5 (5 votes)
29 Jun 2011CPOL5 min read 22.5K   1K   12  
An updated version of the WTL BmpView sample application using an OLE Picture object to handle multiple image types
// View.h : interface of the CWTLIPictureView class
//
/////////////////////////////////////////////////////////////////////////////

#ifndef __CWTLIPICTUREVIEW_H__
#define __CWTLIPICTUREVIEW_H__

#pragma once

class CWTLIPictureView : public CScrollWindowImpl<CWTLIPictureView>
{
public:
	DECLARE_WND_CLASS_EX(NULL, 0, -1)

	CWTLIPicture m_pix;
	bool m_border;
	int m_scale;
	SIZE m_size;

	CWTLIPictureView()
	{
		m_border = true;
		m_scale = 100;
		m_size.cx = m_size.cy = 0;
	}

	BOOL PreTranslateMessage(MSG* pMsg)
	{
		pMsg;
		return FALSE;
	}

	BEGIN_MSG_MAP(CWTLIPictureView)
		MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBackground)
		CHAIN_MSG_MAP(CScrollWindowImpl<CWTLIPictureView>);
	END_MSG_MAP()

	LRESULT OnEraseBackground(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		CDCHandle dc = (HDC)wParam;

		RECT rect;
		GetClientRect(&rect);
		dc.FillRect(&rect, (HBRUSH)(COLOR_WINDOW + 1));

		if(!m_pix.IsNull())
		{
			// draw border around picture
			if(m_border)
			{
				dc.MoveTo(m_size.cx, 0);
				dc.LineTo(m_size.cx, m_size.cy);
				dc.LineTo(0, m_size.cy);
			}
		}

		return 1;
	}

	void DoPaint(CDCHandle dc)
	{
		if(!m_pix.IsNull())
		{
			// draw the picture
			RECT rc = { 0, 0, m_size.cx, m_size.cy };
			m_pix.Render(dc, rc);
		}
	}

	void SetSize(int nScale = 100)
	{
		if (!m_pix.IsNull())
			m_pix.GetSizeInPixels(m_size);
		else
			m_size.cx = m_size.cy = 0;

		// allowable scale 25% to 800%
		if (nScale >= 25 && nScale <= 800)
		{
			m_scale = nScale;

			// need at least 4 pixels to scale 25%
			if (m_size.cx >= 4 && m_size.cy >= 4)
			{
				m_size.cx = (m_size.cx * nScale) / 100;
				m_size.cy = (m_size.cy * nScale) / 100;
			}
		}

		SetScrollOffset(0, 0, FALSE);

		if (m_size.cx > 0 && m_size.cy > 0)
			SetScrollSize(m_size);
		else
			SetScrollSize(1, 1);

	}
};

#endif // __CWTLIPICTUREVIEW_H__

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
Founder Choycer
United States United States
Ed has over 40 years experience in computer technology and a bachelor's degree in Business Administration. He's currently a marketing technology consultant. During his career, he's led software development departments and created software still in use in the communications and healthcare industries. Ed is a veteran of the United States Army. He lives in Arizona in the United States.

Find Ed on Linkedin.

This material is copyright 2019 by Ed Gadziemski. Unauthorized use is strictly prohibited. All rights reserved.

Comments and Discussions