|
//Header file class CMyCustomDraw : public Dapfor::GUI::CCustomDraw { typedef Dapfor::GUI::CCustomDraw BaseClass; public: CMyCustomDraw(bool autoDelete); // Called to draw a cell. virtual void DrawCell(Dapfor::GUI::CGridCell& cell, const Dapfor::GUI::CPaintContext& paintContext, UINT paintFilter); ... };
Generally, implementation of this interface is quite easy. For more details about the drawing, please see the Drawing workflow. The function CCustomDraw::DrawCell() is called each time, when the grid paints a cell. In the overridden function you will receive all information about the row, column, business object, colors, fonts, etc. At any moment and for each cell you can set an appropriate appearance. Below you will find the basic implementation of the CCustomDraw interface:
//Constructor CMyCustomDraw::CMyCustomDraw(bool autoDelete) : CCustomDraw(autoDelete) { } void CMyCustomDraw::DrawCell(Dapfor::GUI::CGridCell& cell, const Dapfor::GUI::CPaintContext& paintContext, UINT paintFilter) { BaseClass::DrawCell(cell, paintContext, paintFilter); }
Finally, let's set the instance of this interface to the grid:
//The boolean in the constructor indicates, that the grid will handle the life //time of the intarface and will destroy it in own destructor. m_Grid.SetCustomDraw(new CMyCustomDraw(true));
void CMyCustomDraw::DrawCell(Dapfor::GUI::CGridCell& cell, const Dapfor::GUI::CPaintContext& paintContext, UINT paintFilter) { //This variable should be visible by the base class CFont font; //Customize the font for each cell if(CFont* initialFont = cell.GetPaintInfo().GetFont()) { //Get the current log font LOGFONT logFont; initialFont->GetLogFont(&logFont); //Change the font size for the column, identified by the 'COrder::FidProduct'. if ( cell.GetColumn() != 0 && (cell.GetColumn()->GetFid() == COrder::FidProduct)) { logFont.lfHeight = -MulDiv(12, GetDeviceCaps(paintContext.GetDC(), LOGPIXELSY), 72); } //Change the font family for other column if( cell.GetColumn() != 0 && (cell.GetColumn()->GetFid() == COrder::FidUnitPrice)) { //Change the font family TCHAR name[] = _T("Arial"); memcpy(logFont.lfFaceName, name, sizeof(name)); logFont.lfItalic = 1; logFont.lfWeight = 600; } //Now, let's create a new font font.CreateFontIndirect(&logFont); //Use the new font to draw in the cell cell.GetPaintInfo().SetFont(&font); } //Don't forget to call the function in the base class //Note that at this point, the value 'font' is visible. BaseClass::DrawCell(cell, paintContext, paintFilter); //And here, the font will be destroyed }
//Set a custom color for all the line void CMyCustomDraw::DrawCell(Dapfor::GUI::CGridCell& cell, const Dapfor::GUI::CPaintContext& paintContext, UINT paintFilter) { ... if(cell.GetColumn() && cell.GetGridLine().GetDataObject()) { UINT color = cell.GetPaintInfo().GetForeColor(); switch(cell.GetGridLine().GetDataObject()->GetLong(COrder::FidStatus)) { case COrder::Ordered: color = RGB(11, 146, 63); break; case COrder::Payed: color = RGB(106, 120, 133); break; case COrder::Refunded: color = RGB(162, 125, 157); break; case COrder::Cancelled: color = RGB(165, 185, 21); break; } cell.GetPaintInfo().SetForeColor(color); } BaseClass::DrawCell(cell, paintContext, paintFilter); }
//file MyCustomDraw.h class CMyCustomDraw : public Dapfor::GUI::CCustomDraw { CMyCustomDraw(bool autoDelete); ... private: CImageList m_Bullets; }; //file MyCustomDraw.cpp #include "Dapfor/GUI/MsImg32.h" //Constructor CMyCustomDraw::CMyCustomDraw(bool autoDelete) : CCustomDraw(autoDelete) { //The method CImageList::Create(...) does not permit to load 32-bit color image //To show full-colored images in cells of the grid, you can use the helper from the Dapfor::GUI library Dapfor::GUI::CMsImg32::Load32BitImage(m_Bullets, IDB_BULLETS, true, RGB(255, 255, 255)); } void CMyCustomDraw::DrawCell(Dapfor::GUI::CGridCell& cell, const Dapfor::GUI::CPaintContext& paintContext, UINT paintFilter) { //Set an image for the 'Unit price' column if( cell.GetColumn() && (cell.GetColumn()->GetFid() == COrder::FidUnitPrice) && cell.GetGridLine().GetDataObject()) { cell.SetImageList(&m_Bullets); //Select some index in the image list int iconIndex = ((GetTickCount() % 2000) / 300) % 6; cell.SetImageId(iconIndex); cell.SetStretchImage(false); //request for the next drawing routine for the given cell in order to change the icon in few milliseconds cell.RequestNextPaint(30); } BaseClass::DrawCell(cell, paintContext, paintFilter); }
// Computes an optimal width of a cell taking into account a font, hierarchy, text size... int CMyCustomDraw::GetOptimalCellWidth(Dapfor::GUI::CGridCell& cell, const Dapfor::GUI::CPaintContext& paintContext, UINT paintFilter) const { //This variable should be visible by the base class CFont font; //Customize the font for each cell if(CFont* initialFont = cell.GetPaintInfo().GetFont()) { //Get the current font LOGFONT logFont; initialFont->GetLogFont(&logFont); //Change the font's size for the 'Product' column if(cell.GetColumn() != 0 && (cell.GetColumn()->GetFid() == COrder::FidProduct)) { logFont.lfHeight = -MulDiv(12, GetDeviceCaps(paintContext.GetDC(), LOGPIXELSY), 72); } if(cell.GetColumn() != 0 && (cell.GetColumn()->GetFid() == COrder::FidUnitPrice)) { TCHAR name[] = _T("Arial"); memcpy(logFont.lfFaceName, name, sizeof(name)); logFont.lfItalic = 1; logFont.lfWeight = 600; } font.CreateFontIndirect(&logFont); cell.GetPaintInfo().SetFont(&font); } //The base class will do the computing... return BaseClass::GetOptimalCellWidth(cell, paintContext, paintFilter); } int CMyCustomDraw::GetOptimalRowHeight(CFont* font, CDC& dc) const { //return the height return 16; }
| Copyright Dapfor 2007-2009 | Generated on Sat Jan 30 15:01:23 2010 for MFCGrid by 1.5.5 |