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

CGridCtrl 1.5

Rate me:
Please Sign up or sign in to vote.
3.95/5 (19 votes)
17 Jun 20042 min read 132.5K   6.4K   41  
This article presents a tiny grid control derived from the standard list control
#include "stdafx.h"
#include "BgListViewDlg.h"

#ifdef _DEBUG
	#define new DEBUG_NEW
#endif

//
//	BgListViewDlg::BgListViewDlg
//
BgListViewDlg::BgListViewDlg(CWnd* pParent)
:	CDialog(BgListViewDlg::IDD, pParent)
{
}

//
//	BgListViewDlg::DoDataExchange
//
void BgListViewDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, mList);
	DDX_Control(pDX, IDC_COMBO1, mPalleteCombo);
}

//
//	BgListViewDlg::OnInitDialog
//
BOOL BgListViewDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	mPalleteCombo.InsertString(0, TEXT("System Pallete"));
	mPalleteCombo.InsertString(1, TEXT("Red Pallete"));
	mPalleteCombo.InsertString(2, TEXT("Indigo Pallete"));
	mPalleteCombo.InsertString(3, TEXT("Green Pallete"));
	mPalleteCombo.InsertString(4, TEXT("Orange Pallete"));
	mPalleteCombo.SetCurSel(0);

	// set image list
	mImageList.Create(IDB_BITMAP1, 16, 5, RGB(255, 0, 255));
	mList.SetImageList(&mImageList, LVSIL_SMALL);

	// populate the grid control
	int COLS = 30;
	int ROWS = 2000;

	// columns...
	CString s;
	for (int c = 0; c < COLS; c++)
	{
		s.Format(TEXT("Col %d"), c + 1);
		mList.InsertColumn(c, s, LVCFMT_LEFT, 100, c);
	}

	// rows...
	LVITEM item;
	item.mask = LVIF_TEXT | LVIF_IMAGE;
	for (int i = 0; i < ROWS; i++)
	{
		s.Format(TEXT("Cell %d, 1"), i + 1);
		item.pszText = s.GetBuffer(0);
		item.iItem = i;
		item.iSubItem = 0;
		item.iImage = 0;
		mList.InsertItem(&item);

		for (c = 1; c < COLS; c++)
		{
			s.Format(TEXT("Cell %d, %d"), i + 1, c + 1);
			item.pszText = s.GetBuffer(0);
			item.iSubItem  = c;
			item.iImage = 1;
			mList.SetItem(&item);
		}
	}

	// initial dimensions
	RECT rect;
	GetClientRect(&rect);
	OnSize(0, rect.right, rect.bottom);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

//
//	BgListViewDlg::OnSize
//
void BgListViewDlg::OnSize(UINT nType, int cx, int cy)
{
	UNREFERENCED_PARAMETER(nType);
	if (IsWindow(mList.m_hWnd))
	{
		RECT rect = { 0, 0, cx, cy };
		::InflateRect(&rect, -7, -7);

		RECT cbrect;
		mPalleteCombo.GetClientRect(&cbrect);
		::OffsetRect(&cbrect, 7 - cbrect.left, cy - 7 - cbrect.bottom);
		mPalleteCombo.MoveWindow(&cbrect);

		rect.bottom -= 7 + cbrect.bottom - cbrect.top;
		mList.MoveWindow(&rect);
	}
}

void BgListViewDlg::OnPalleteChange()
{
	int index = mPalleteCombo.GetCurSel();
	switch (index)
	{
		case 0:
			mList.SetColorsToDefault();
			mList.Invalidate(FALSE);
			break;

		case 1:
			mList.SetColors(RGB(128, 0, 0),			// frame
							RGB(200, 150, 150),		// normal fill
							RGB(200, 0, 0),			// selected fill
							RGB(200, 135, 135),		// button fill
							RGB(0, 0, 0),			// normal text
							RGB(255, 255, 255));	// selected text
			mList.Invalidate(FALSE);
			break;

		case 2:
			mList.SetColors(RGB(0, 0, 128),			// frame
							RGB(150, 150, 200),		// normal fill
							RGB(0, 0, 200),			// selected fill
							RGB(135, 135, 200),		// button fill
							RGB(0, 0, 0),			// normal text
							RGB(255, 255, 255));	// selected text
			mList.Invalidate(FALSE);
			break;

		case 3:
			mList.SetColors(RGB(0, 100, 0),			// frame
							RGB(150, 200, 150),		// normal fill
							RGB(0, 200, 0),			// selected fill
							RGB(165, 200, 165),		// button fill
							RGB(0, 0, 0),			// normal text
							RGB(255, 255, 255));	// selected text
			mList.Invalidate(FALSE);
			break;

		case 4:
			mList.SetColors(RGB(128, 0, 0),			// frame
							RGB(255, 200, 50),		// normal fill
							RGB(150, 0, 0),			// selected fill
							RGB(235, 200, 50),		// button fill
							RGB(255, 255, 255),		// normal text
							RGB(255, 255, 255));	// selected text
			mList.Invalidate(FALSE);
			break;
	}
}

//
//	BEGIN_MESSAGE
//
BEGIN_MESSAGE_MAP(BgListViewDlg, CDialog)
	//}}AFX_MSG_MAP
	ON_WM_SIZE()
	ON_CBN_SELCHANGE(IDC_COMBO1, OnPalleteChange)
END_MESSAGE_MAP()

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
Web Developer
Romania Romania
- C/C++ Programming
- Windows Device Driver programming (DDK)
- Low-level networking programmer (firewalls, routers, sniffers...)
- AutoCAD ObjectARX/DBX programming
- ADT Object Modeling Framework (OMF) programmer

Comments and Discussions