| | | | Browse by category |
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() } |
|||
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, ... |
|||
} | |||
} |
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.