Click here to Skip to main content
15,881,810 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

// fi_test.cpp : Defines the class behaviors for the application.
//

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

#include "MainFrm.h"
#include "fi_testDoc.h"
#include "fi_testView.h"

#include <afxadv.h> // the support for the CRecentFileList object

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

/////////////////////////////////////////////////////////////////////////////
// CFi_testApp

BEGIN_MESSAGE_MAP(CFi_testApp, CWinApp)
	//{{AFX_MSG_MAP(CFi_testApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	
	//}}AFX_MSG_MAP
	// Standard file based document commands
	
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFi_testApp construction

CFi_testApp::CFi_testApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CFi_testApp object

CFi_testApp theApp;

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

// CFi_testApp initialization

BOOL CFi_testApp::InitInstance()
{
	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("FreeImage Display Demo"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CFi_testDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CFi_testView));
	AddDocTemplate(pDocTemplate);

	// Enable DDE Execute open
	EnableShellOpen();
	RegisterShellFileTypes(TRUE);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The one and only window has been initialized, so show and update it.

	m_pMainWnd->ShowWindow(SW_MAXIMIZE);

	m_pMainWnd->UpdateWindow();

	// Enable drag/drop open
	m_pMainWnd->DragAcceptFiles();

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// 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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CFi_testApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CFi_testApp message handlers

BOOL CFi_testApp::GetRecentFilename(CString& strPathName, UINT nID)
{
	// Fixed MFC's CWinApp::OnOpenRecentFile
	// Always moves the file to the top of the recents list
	// Copyright Andrew Zhezherun http://windjview.sf.net

	ASSERT_VALID(this);

	if (m_pRecentFileList == NULL) return false;

	if (nID < ID_FILE_MRU_FILE1 && nID > ID_FILE_MRU_FILE1 + (UINT)m_pRecentFileList->GetSize()) return false;
	
	int nIndex = nID - ID_FILE_MRU_FILE1;

	strPathName = (*m_pRecentFileList)[nIndex];

	if (strPathName.IsEmpty()) return false;
	
	return true;
}

void CFi_testApp::RemoveRecentFilename(UINT nID)
{
	int nIndex = nID - ID_FILE_MRU_FILE1;

	m_pRecentFileList->Remove(nIndex);
}

BOOL CFi_testApp::OnIdle(LONG lCount) 
{
	// TODO: Add your specialized code here and/or call the base class

	// If a file is opened - displaying its name on the Status Bar

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

	ASSERT_VALID(pDoc);

	CMainFrame* pFr = (CMainFrame*)(AfxGetApp()->m_pMainWnd);

	ASSERT_VALID(pFr);

	if (pDoc->m_dib)		
	{

	CString PaneText = pFr->m_wndStatusBar.GetPaneText(0);

	//if (PaneText == "Ready") 

		//pFr->m_wndStatusBar.SetPaneText(0, pDoc->m_strFilename);
	}
	
	return CWinApp::OnIdle(lCount);
}

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