Click here to Skip to main content
15,896,111 members
Articles / Desktop Programming / MFC

Displaying Bitmap with Scrolling

Rate me:
Please Sign up or sign in to vote.
4.84/5 (85 votes)
9 Nov 20029 min read 392.8K   15.8K   145  
An article showing how to display a picture within a dialog, and add scrollbars where needed to view the whole image.
// ScrollBitmapDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ScrollBitmap.h"
#include "ScrollBitmapDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// ScrollBitmapDlg.cpp: Implementation File.
// Written by Shibu K.V (shibukv@erdcitvm.org)
// Sr.Research Associate
// ASIC Hardware Design Group
// Electronics Research & Development Centre of India
// Trivandrum, Kerala, India.
// Copyright (c) 2002
//
// Warning: this code hasn't been subject to a heavy testing, so
// use it on your own risk. The author accepts no liability for the 
// possible damage caused by this code.
//
// Version 1.0  22 Oct 2002.

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScrollBitmapDlg dialog

CScrollBitmapDlg::CScrollBitmapDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CScrollBitmapDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CScrollBitmapDlg)
	m_hBmpNew = NULL;
	flag=true;
	erase=false;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CScrollBitmapDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CScrollBitmapDlg)
	DDX_Control(pDX, IDC_STATIC1, m_st1);
	DDX_Control(pDX, IDC_SCROLLBAR2, m_vbar);
	DDX_Control(pDX, IDC_SCROLLBAR1, m_hbar);
	DDX_Control(pDX, IDC_COMMONDIALOG1, m_com);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CScrollBitmapDlg, CDialog)
	//{{AFX_MSG_MAP(CScrollBitmapDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SHOW, OnShow)
	ON_WM_VSCROLL()
	ON_WM_HSCROLL()
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScrollBitmapDlg message handlers

BOOL CScrollBitmapDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	CClientDC dc(this);
	 m_dcMem.CreateCompatibleDC( &dc );
		m_vbar.ShowWindow(false);
			m_hbar.ShowWindow(false);
		
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CScrollBitmapDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CScrollBitmapDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{	
		CPaintDC dc(this);
	  dc.BitBlt(offsetx,offsety,m_size.cx,m_size.cy, 
                   &m_dcMem, sourcex, sourcey,SRCCOPY);
	  erase=false;
			CDialog::OnPaint();
		
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CScrollBitmapDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CScrollBitmapDlg::OnShow() 
{
	// TODO: Add your control notification handler code here
	CString	file;
	file.Empty();
	m_com.ShowOpen();
	file=m_com.GetFileName();
	file.TrimRight();
	if(m_hBmpNew != NULL )
		DeleteObject(m_hBmpNew);
	sourcex=sourcey=0;//
	if(file.IsEmpty())
AfxMessageBox("Please Select a picture file");
		else{
			if(file.Right(3)!="bmp")
				AfxMessageBox("Please Select a .bmp file");
			else
			{
	m_hBmpNew =	 (HBITMAP) LoadImage(
	AfxGetInstanceHandle(),   // handle to instance
	file,  // name or identifier of the image (root is where project is)
	IMAGE_BITMAP,        // image types
	0,     // desired width
	0,     // desired height
	LR_LOADFROMFILE); 
	if( m_hBmpNew == NULL ){
	AfxMessageBox("Load Image Failed");}
	
   // put the HBITMAP info into the CBitmap (but not the bitmap itself)
   else{
	 m_st1.GetClientRect( &rectStaticClient );
	rectStaticClient.NormalizeRect();
	m_size.cx=rectStaticClient.Size().cx;
	m_size.cy=rectStaticClient.Size().cy;
   m_size.cx = rectStaticClient.Width();    // zero based
    m_size.cy = rectStaticClient.Height();    // zero based

    // Convert to screen coordinates using static as base,
    // then to DIALOG (instead of static) client coords 
    // using dialog as base
  m_st1.ClientToScreen( &rectStaticClient );
  ScreenToClient( &rectStaticClient);
        
    m_pt.x = rectStaticClient.left;
    m_pt.y = rectStaticClient.top;
  GetObject( m_hBmpNew , sizeof(BITMAP), &m_bmInfo );
 VERIFY(m_hBmpOld = (HBITMAP)SelectObject(m_dcMem, m_hBmpNew )  );

  offsetx= m_pt.x;
  offsety=m_pt.y;
m_vbar.MoveWindow(offsetx+m_size.cx,offsety,18,m_size.cy);
m_hbar.MoveWindow(offsetx,offsety+m_size.cy,m_size.cx,18);
horz.cbSize = sizeof(SCROLLINFO);
horz.fMask = SIF_ALL;
horz.nMin = 0;
horz.nMax = m_bmInfo.bmWidth-m_size.cx;
horz.nPage =0;
horz.nPos = 0;
horz.nTrackPos=0;
if(m_bmInfo.bmWidth<=m_size.cx)
{
	if((m_size.cx-m_bmInfo.bmWidth)==0)
		offsetx= m_pt.x;
	else
		offsetx= m_pt.x+((m_size.cx-m_bmInfo.bmWidth)/2);
	m_vbar.MoveWindow(offsetx+m_bmInfo.bmWidth,offsety,18,m_size.cy);
	m_hbar.ShowWindow(false);
}
else
m_hbar.ShowWindow(true);
m_hbar.SetScrollInfo(&horz);
vert.cbSize = sizeof(SCROLLINFO);
vert.fMask = SIF_ALL;
vert.nMin = 0;
vert.nMax = m_bmInfo.bmHeight-(m_size.cy);
vert.nPage = 0;
vert.nTrackPos=0;
if(m_bmInfo.bmHeight<=m_size.cy)
{
	if((m_size.cy-m_bmInfo.bmHeight)==0)
		offsety= m_pt.y;
	else
		offsety= m_pt.y+((m_size.cy-m_bmInfo.bmHeight)/2);
	m_hbar.MoveWindow(offsetx,offsety+m_bmInfo.bmHeight,m_size.cx,18);
	m_vbar.ShowWindow(false);
}
else
m_vbar.ShowWindow(true);
m_vbar.SetScrollInfo(&vert);

  InvalidateRect(&rectStaticClient);
		}
			}
		}

}

void CScrollBitmapDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	switch (nSBCode)
{
case SB_TOP:
sourcey = 0;
break;
case SB_BOTTOM:
sourcey = INT_MAX;
break;
case SB_THUMBTRACK:
sourcey = nPos;
break;
}

//ScrollWindow(0,-(x-xOrig));
m_vbar.SetScrollPos(sourcey);
erase=true;
InvalidateRect(&rectStaticClient);
	CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}

void CScrollBitmapDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
switch (nSBCode)
{
case SB_TOP:
sourcex = 0;
break;
case SB_BOTTOM:
sourcex = INT_MAX;
break;
case SB_THUMBTRACK:
sourcex= nPos;
break;
}	
m_hbar.SetScrollPos(sourcex);
//Invalidate();
erase=true;
InvalidateRect(&rectStaticClient);
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}


BOOL CScrollBitmapDlg::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	if(erase)
		return false;
	else
	return CDialog::OnEraseBkgnd(pDC);
}


void CScrollBitmapDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

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


Written By
Architect Infosys Technologies
India India
Currently working with the Embedded Systems & DSP Lab of Infosys Technologies, Thiruvananthapuram Development unit (www.infosys.com)

Comments and Discussions