Click here to Skip to main content
12,449,420 members (60,500 online)
Click here to Skip to main content

Stats

106K views
5.6K downloads
40 bookmarked
Posted

CGridCtrl 1.5

, 17 Jun 2004
This article presents a tiny grid control derived from the standard list control
#include "StdAfx.h"
#include "BgListView.h"

//
//	BgListView::BgListView
//
BgListView::BgListView()
:	CGridCtrl()
{
	// set all checkboxes to CHECKED
	FillMemory(mChecks, CHECK_COUNT, 1);
}


//
//	BgListView::PreSubclassWindow
//
void BgListView::PreSubclassWindow(void)
{
	CGridCtrl::PreSubclassWindow();

	// init popup menu
	mMenu.CreatePopupMenu();
	mMenu.AppendMenu(MF_STRING, 1, TEXT("You can fill"));
	mMenu.AppendMenu(MF_STRING, 2, TEXT("this popup menu..."));
	mMenu.AppendMenu(MF_SEPARATOR);
	mMenu.AppendMenu(MF_STRING, 3, TEXT("...with whatever you like"));
}

//
//	BgListView::OnDestroy
//
void BgListView::OnDestroy()
{
	CGridCtrl::OnDestroy();
}

//
//	BgListView::OnGetComboBoxItems
//
void BgListView::OnGetComboBoxItems(IN  int Row,
									IN  int Col,
									OUT bool& HasImages,
									OUT CStringList& Items,
									OUT CArray<int, int>& Images)
{
	// first of all, call the parent implementation
	// NOTE: do it for all CGridCtrl virtual methods!
	CGridCtrl::OnGetComboBoxItems(Row, Col, HasImages, Items, Images);

	// fill the combo box with whatever items
	Items.AddTail(TEXT("3"));
	Items.AddTail(TEXT("7"));
	Items.AddTail(TEXT("4"));
	Items.AddTail(TEXT("5"));
	Items.AddTail(TEXT("10"));
	Items.AddTail(TEXT("50"));

	// we want combo images
	HasImages = true;
	Images.Add(0);
	Images.Add(1);
	Images.Add(1);
	Images.Add(0);
	Images.Add(0);
	Images.Add(1);
}

//
//	BgListView::OnGetCellColors
//
bool BgListView::OnGetCellColors(	IN  int Row,
									IN  int Col,
									OUT COLORREF& TextColor,
									OUT COLORREF& BkColor,
									OUT UINT8& TextStyle) const
{
	// NOTE: this routine is called VERY often by the painting routines. keep it
	//       as small/fast as possible!
	// NOTE: if you are comfortable with the default colors of the current cell,
	//       you should return FALSE, instead of returning TRUE and leaving the
	//       colors unchanged. this will increase drawing speed...

	if (mDraw.Type == CELL_SEPARATOR)
		return false;

	switch (Col)
	{
		case 0:
			TextStyle = FONT_BOLD;
			break;
		case 1:
			TextStyle = FONT_ITALIC;
			break;
		case 2:
			TextStyle = FONT_UNDERLINE;
			break;
		case 3:
			TextStyle = FONT_STRIKEOUT;
			break;
		case 4:
			TextStyle = FONT_BOLD | FONT_ITALIC;
			break;
		case 5:
			TextStyle = FONT_UNDERLINE | FONT_STRIKEOUT;
			break;
	}

	// change the background of even rows
	if (Row % 2 == 0 && !mDraw.Selected)
	{
		AdjustBrightness(BkColor, -15);
		AdjustBrightness(TextColor, 60);
		return true;
	}
	return (TextStyle != 0);
}

//
//	BgListView::OnGetCellType
//
CellType BgListView::OnGetCellType(IN int Row, IN int Col)
{
	// NOTE: this routine is called VERY often by the painting routines. keep it
	//       as small/fast as possible!

	if ((Row + 1) % 11 == 0)
		return CELL_SEPARATOR;

	switch (Col)
	{
	case 0:
		if (Row < 5) return CELL_EDITBOX;
		break;

	case 1:
		if (Row < 6) return CELL_COMBOBOX;
		break;

	case 2:
		if (Row < 7) return CELL_EDITCOMBOBOX;
		break;

	case 3:
		if (Row < 8) return CELL_CUSTOMEDITOR;
		break;

	case 4:
		if (Row < CHECK_COUNT) return mChecks[Row] ? CELL_CHECKBOXON : CELL_CHECKBOXOFF;
		break;
	}
	return CELL_REGULAR;
}

//
//	BgListView::OnCheckBoxClick
//
void BgListView::OnCheckBoxClick(IN int Row, IN int Col)
{
	// change the CHECKED state of this check box
	mChecks[Row] = !mChecks[Row];

	// supercall default implementation for repainting the cell
	CGridCtrl::OnCheckBoxClick(Row, Col);
}

//
//	BgListView::OnCustomCellClick
//
void BgListView::OnCustomCellClick(int Row, int Col)
{
	CString sTemp;
	sTemp.Format(TEXT("Custom edit @ (%d, %d)\nYou could pop up a \"details\" dialog or something..."), Row + 1, Col + 1);
	MessageBox(sTemp, TEXT("CGridCtrl"), MB_ICONINFORMATION);
}

//
//	BgListView::AdjustBrightness
//
void BgListView::AdjustBrightness(	IN OUT COLORREF& Color,
									IN     INT16 Amount) const
{
	INT32 comp = GetRValue(Color) + Amount;
	if (comp > 255) comp = 255;
	else if (comp < 0) comp = 0;
	Color = (Color & 0xffffff00) | comp;

	comp = GetGValue(Color) + Amount;
	if (comp > 255) comp = 255;
	else if (comp < 0) comp = 0;
	Color = (Color & 0xffff00ff) | (comp << 8);

	comp = GetBValue(Color) + Amount;
	if (comp > 255) comp = 255;
	else if (comp < 0) comp = 0;
	Color = (Color & 0xff00ffff) | (comp << 16);
}

//
//	BgListView::OnRClick
//
void BgListView::OnCellRBtnUp(	IN int Row,
								IN int Col,
								IN const RECT *CellRect,
								IN const CPoint& Point)
{
	POINT pt = { Point.x, Point.y };
	ClientToScreen(&pt);
	mMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, this);
}

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

Share

About the Author

Marius Negrutiu
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

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160811.3 | Last Updated 18 Jun 2004
Article Copyright 2004 by Marius Negrutiu
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid