Click here to Skip to main content
15,886,518 members
Articles / Desktop Programming / MFC

CIconDialog - Selecting Icons

Rate me:
Please Sign up or sign in to vote.
4.79/5 (28 votes)
28 Mar 2004CPOL3 min read 134.4K   5.7K   54  
The CIconDialog class allows you to add an icon-selection dialog box to your applications
// IconDialogDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "IconDialogDemo.h"
#include "IconDialogDemoDlg.h"

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

#include "IconDialog.h"

/////////////////////////////////////////////////////////////////////////////
// CIconDialogDemoDlg dialog

CIconDialogDemoDlg::CIconDialogDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CIconDialogDemoDlg::IDD, pParent)
	, m_dwMaxIconIndex( 0 )
	, m_strIconFile( _T("") )
	, m_dwIconIndex( 0 )	
{
	//{{AFX_DATA_INIT(CIconDialogDemoDlg)
	//}}AFX_DATA_INIT

	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CIconDialogDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIconDialogDemoDlg)
	DDX_Control(pDX, IDC_LINK, m_wndLink);
	DDX_Control(pDX, IDC_PIC_ICON, m_wndPicIcon);	
	DDX_Control(pDX, IDC_SPN_ICONINDEX, m_wndSpinIconIndex);
	DDX_Text(pDX, IDC_TXT_ICONFILE, m_strIconFile);
	DDV_MaxChars(pDX, m_strIconFile, _MAX_PATH);	
	DDX_Text(pDX, IDC_TXT_ICONINDEX, m_dwIconIndex);	
	DDV_MinMaxDWord(pDX, m_dwIconIndex, 0, m_dwMaxIconIndex);
	// }}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CIconDialogDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CIconDialogDemoDlg)	
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CMD_BROWSE, OnCmdBrowse)
	ON_EN_CHANGE(IDC_TXT_ICONINDEX, OnChangeTxtIconindex)	
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIconDialogDemoDlg message handlers

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

	// 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

	CWnd* pWnd = (CWnd*)GetDlgItem( IDC_TXT_ICONINDEX );
	if( pWnd != NULL )
	{
		m_wndSpinIconIndex.SetBuddy( pWnd );
		m_wndSpinIconIndex.SetRange32( 0, 0 ); 
	}
	m_wndPicIcon.SetIcon( AfxGetApp()->LoadIcon( IDR_MAINFRAME ) );
	
	m_wndLink.SetURL( _T("mailto:armenh@web.am") ); 
	return TRUE;  // return TRUE  unless you set the focus to a control
}

//	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 CIconDialogDemoDlg::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
		CDialog::OnPaint();
}

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

void CIconDialogDemoDlg::OnCmdBrowse() 
{
	UpdateData();

	CIconDialog dlg( m_strIconFile, m_dwIconIndex, this );
	if( dlg.DoModal() == IDOK )
	{
		m_strIconFile = dlg.GetIconFile();
		m_dwIconIndex = dlg.GetIconIndex();
			
		m_dwMaxIconIndex = dlg.GetIconCount() - 1; // DDV_MinMaxDWord - limits icon index
		m_wndSpinIconIndex.SetRange32( 0, m_dwMaxIconIndex ); 
		m_wndSpinIconIndex.Invalidate();

		UpdateData( FALSE );
		OnChangeTxtIconindex(); 
	}
}

void CIconDialogDemoDlg::OnChangeTxtIconindex() 
{
	UpdateData();
	
	if(!m_strIconFile.IsEmpty())
	{
		HICON hIcon = ::ExtractIcon(AfxGetInstanceHandle(), m_strIconFile, m_dwIconIndex);
		if(hIcon != NULL)
			m_wndPicIcon.SetIcon(hIcon);
	}
}

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
Software Developer (Senior) SafeNet Inc
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions