Click here to Skip to main content
15,880,405 members
Articles / Desktop Programming / MFC

FreeImage Display Demo

Rate me:
Please Sign up or sign in to vote.
3.98/5 (20 votes)
4 Jun 2008Public Domain17 min read 97.1K   10.4K   54  
How to display a bitmap in your MFC SDI application using FreeImage. Various rescaling algorithms considered.
//	FreeImage Display Demo v3.4

//	Copyright (C) 2008 monday2000@yandex.ru
//
//	This program is free software; you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation; either version 2 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program; if not, write to the Free Software
//	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//	http://www.gnu.org/copyleft/gpl.html

// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "fi_test.h"

#include "MainFrm.h"

#include "fi_testDoc.h" // <- added for OnDropFiles

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_DROPFILES()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
		ID_INDICATOR_CAPS,
		ID_INDICATOR_NUM,
		ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}
	
	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}
	
	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);
	
	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{	
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	//cs.style |= WS_MAXIMIZE; // does not work - most likely MFC bug

  cs.style &= ~FWS_ADDTOTITLE;

  RECT sr;
	
  ::SystemParametersInfo(
  SPI_GETWORKAREA, // system parameter to query or set
  0,  // depends on action to be taken
  &sr, // depends on action to be taken
  0   // user profile update flag
  );

  int x = ::GetSystemMetrics(SM_CXFRAME);

  int y = ::GetSystemMetrics(SM_CYFRAME);

  cs.cx = sr.right - sr.left + 2*x;
  cs.cy =  sr.bottom - sr.top + 2*y;

  cs.x = sr.left - x;
  cs.y = sr.top - y;

  return TRUE;
}

// CMainFrame message handlers

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////

void CMainFrame::OnDropFiles(HDROP hDropInfo) 
{
	// TODO: Add your message handler code here and/or call default

	char buf[MAX_PATH] = {0};

	DragQueryFile(hDropInfo, 0, buf, MAX_PATH);

	DragFinish(hDropInfo);	

	CString FileName = buf;

	FileName.MakeLower();

	CString Ext = FileName.Right(FileName.GetLength() - FileName.ReverseFind('.') - 1);

	if (Ext!="bmp" && 
		Ext!="cut" &&
		Ext!="dds" &&
		Ext!="g3" &&
		Ext!="gif" &&
		Ext!="hdr" &&
		Ext!="ico" &&
		Ext!="iff" &&
		Ext!="lbm" &&
		Ext!="jng" &&
		Ext!="jpg" &&
		Ext!="jif" &&
		Ext!="jpeg" &&
		Ext!="jpe" &&
		Ext!="koa" &&
		Ext!="mng" &&
		Ext!="pbm" &&
		Ext!="pcd" &&
		Ext!="pcx" &&
		Ext!="pgm" &&
		Ext!="png" &&
		Ext!="ppm" &&
		Ext!="psd" &&
		Ext!="ras" &&
		Ext!="sgi" &&
		Ext!="tga" &&
		Ext!="targa" &&
		Ext!="tif" &&
		Ext!="tiff" &&
		Ext!="wap" &&
		Ext!="wbmp" &&
		Ext!="wbm" &&
		Ext!="xbm" &&
		Ext!="xpm"		
		)	
		
		return;

	else	
	{ 

	CFi_testDoc* pDoc = (CFi_testDoc*)((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->GetActiveDocument();

	ASSERT_VALID(pDoc);

	pDoc->OpenBitmapFile(FileName);

	}	
}






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 A Public Domain dedication


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions