Objective Grid: Displaying icons in cells

Article ID: 466
Last updated: 25 Apr, 2018
Article ID: 466
Last updated: 25 Apr, 2018
Revision: 4
Views: 2041
Posted: 19 Jan, 2001
by Meltreger B.
Updated: 25 Apr, 2018
by Meltreger B.

Problem


How can I display icons in cells?


Cause



Action


Here is a sample icon cell type class:

// In the .h file
class CGXIconControl : public CGXStatic
{
DECLARE_CONTROL(CGXIconControl)
public:
CGXIconControl(CGXGridCore* pGrid);

virtual ~CGXIconControl();

virtual void Draw(CDC* pDC, CRect rect, ROWCOL nRow, ROWCOL nCol, const CGXStyle& style, const CGXStyle* pStandardStyle);

};

// in the .cpp file

IMPLEMENT_CONTROL(CGXIconControl, CGXStatic);

CGXIconControl::CGXIconControl(CGXGridCore* pGrid)

: CGXStatic(pGrid)
{

}

CGXIconControl::~CGXIconControl()
{

}
void CGXIconControl::Draw(CDC* pDC, CRect rect, ROWCOL nRow, ROWCOL nCol, const CGXStyle& style, const CGXStyle* pStandardStyle)
{

ASSERT(pDC != NULL && pDC->IsKindOf(RUNTIME_CLASS(CDC)));
// ASSERTION-> Invalid Device Context ->END
ASSERT(nRow <= Grid()->GetRowCount() && nCol <= Grid()->GetColCount());
// ASSERTION-> Cell coordinates out of range ->END
ASSERT_VALID(pDC);

DrawBackground(pDC, rect, style);

if (rect.right <= rect.left || rect.Width() <="1" || rect.Height() <="1)</FONT">

return;
CString str = style.GetIncludeValue() ? style.GetValue() : _T("");

if(str.Left(4) == _T("#ICO"))
{

int n = str.Find(_T(")"));
CString strIDResource = str.Mid(5,n-5);
UINT nIDResource = _ttoi(strIDResource);
HICON hIcon = AfxGetApp()->LoadIcon (nIDResource);
CRect r = CGXControl::GetCellRect(nRow, nCol, rect, &style);
pDC->DrawIcon(r.left, r.top, hIcon);

// child Controls: spin-buttons, hotspot, combobox btn, ...
CGXControl::Draw(pDC, rect, nRow, nCol, style, pStandardStyle);

}
}

You could register this control in any of your projects and use it as shown below:

CGXIconControl* iconControl = new CGXIconControl(this);
RegisterControl(GX_IDS_CTRL_ICON, iconControl);
SetStyleRange(CGXRange(7,7),
CGXStyle()
.SetControl(GX_IDS_CTRL_ICON)
.SetValue("#ICO(149)")
);

Of course, setup a GX_IDS_CTRL_ICON string resource first. Note that the above code will draw the icon in the resource with the id 149.

This article was:   Helpful | Not helpful
Report an issue
Article ID: 466
Last updated: 25 Apr, 2018
Revision: 4
Views: 2041
Posted: 19 Jan, 2001 by Meltreger B.
Updated: 25 Apr, 2018 by Meltreger B.

Others in this category