Click here to Skip to main content
15,893,722 members
Articles / Desktop Programming / MFC

Progress Cell for Grid Control.

Rate me:
Please Sign up or sign in to vote.
4.17/5 (16 votes)
10 May 20042 min read 65.2K   3K   41  
A new cell progress-bar-like type for Chris Maunder's Grid Control.
// GridProgressCellDlg.cpp : implementation file
// Written by Iain Clarke  (iain@imcsoft.co.uk / imcclarke@yahoo.com)

#include "stdafx.h"
#include "GridProgressCell.h"
#include "GridProgressCellDlg.h"

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

#include "GridCellProgress.h"

/////////////////////////////////////////////////////////////////////////////
// CGridProgressCellDlg dialog

CGridProgressCellDlg::CGridProgressCellDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGridProgressCellDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGridProgressCellDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGridProgressCellDlg::DoDataExchange(CDataExchange* pDX)
{
	DDX_Control (pDX, IDC_GRID, m_Grid);
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGridProgressCellDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGridProgressCellDlg, CDialog)
	//{{AFX_MSG_MAP(CGridProgressCellDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGridProgressCellDlg message handlers

BOOL CGridProgressCellDlg::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
	m_Grid.SetColumnCount (1);
	m_Grid.SetRowCount (10);
	m_Grid.ExpandColumnsToFit ();

	CGridCellProgress *pProg;
	for (int n = 0; n < 10; n++)
		m_Grid.SetCellType (n, 0, RUNTIME_CLASS(CGridCellProgress));

	pProg = (CGridCellProgress *)m_Grid.GetCell (0,0);
	pProg->SetText ("One");
	pProg->SetPos (0);

	pProg = (CGridCellProgress *)m_Grid.GetCell (1,0);
	pProg->SetText ("Two");
	pProg->SetUpper (200);

	pProg = (CGridCellProgress *)m_Grid.GetCell (2,0);
	pProg->SetText ("Three");
	pProg->SetColor (RGB(0,255,0));
	
	pProg = (CGridCellProgress *)m_Grid.GetCell (3,0);
	pProg->SetText ("Four");
	pProg->EnablePercentage (FALSE);
	
	pProg = (CGridCellProgress *)m_Grid.GetCell (4,0);
	pProg->SetText ("Five - But\\a\\very\\long string\\will\\now\\follow,\\because\\I\\want\\it\\to.\\And\\another\\long\\string\\to.\\Phew,\\my\\fingers\\are\\tired.");
	
	pProg = (CGridCellProgress *)m_Grid.GetCell (5,0);
	pProg->SetText ("Six - But\\a\\very\\long string\\will\\now\\follow,\\because\\I\\want\\it\\to.\\And\\another\\long\\string\\to.\\Phew,\\my\\fingers\\are\\tired.");
	pProg->EnablePathCompaction (FALSE);
	
	pProg = (CGridCellProgress *)m_Grid.GetCell (6,0);
	pProg->SetText ("Seven");
	
	pProg = (CGridCellProgress *)m_Grid.GetCell (7,0);
	pProg->SetText ("Eight");
	pProg->SetColor (RGB(255,255,0));
	
	pProg = (CGridCellProgress *)m_Grid.GetCell (8,0);
	pProg->SetText ("Nine");

	pProg = (CGridCellProgress *)m_Grid.GetCell (9,0);
	pProg->SetText ("Ten");



	SetTimer (1, 100, NULL);
	
	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 CGridProgressCellDlg::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 CGridProgressCellDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CGridProgressCellDlg::OnTimer(UINT nIDEvent) 
{
	CGridCellProgress *pProg;
	for (int n = 0; n < 10; n++)
	{
		pProg = (CGridCellProgress *)m_Grid.GetCell (n,0);
		pProg->SetPos ( (pProg->GetPos () + 1) % (pProg->GetUpper ()+1) );

	}

	m_Grid.Invalidate ();
}

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
Software Developer (Senior)
Sweden Sweden
I have now moved to Sweden for love, and recently married a lovely Swede.


-----------------
I started programming on BBC micros (6502) when I was six and never quite stopped even while I was supposed to be studying physics and uni.

I've been working for ~13 years writing software for machine control and data analysis. I now work on financial transaction transformation software, for a Software company in Gamlastan, Stockholm.
Look at my articles to see my excellent coding skills. I'm modest too!

Comments and Discussions