Dapfor home  

Drawing workflow

The grid offers an interface to customize the data presentation. Everything: colors, fonts, images, selection, focusing, etc... can be customized. The grid itself draws nothing. Instead, it asks the drawing interface to perform the painting. Therefore, the programmer has the full control on the data painting. This drawing interface is called ICustomDraw. There is a default implementation of this interface: CCustomDraw. If you don't set your own implementation, then the grid will use the implementation by default.

The best way to configure the painting is to derive from the CCustomDraw class and set your implementation it to the grid by calling:

class CMyCustomDraw : public Dapfor::GUI::CCustomDraw
{
...
    // Called to draw a cell. 
    virtual void DrawCell(Dapfor::GUI::CGridCell& cell, const Dapfor::GUI::CPaintContext& paintContext, UINT paintFilter);

}

//To set your custom drawing implementation, just call:
CMyCustomDraw* customDraw = new CMyCustomDraw(...);
m_Grid.SetCustomDraw(customDraw);

To understand how does the drawing work, we will describe the drawing workflow. When WM_PAINT comes, the grid looks for the implementation of the painting interface (or takes default one) and then calls the ICustomDraw::DrawLine function. This function is called with three parameters:

The default implementation CCustomDraw doesn't perform the drawing. It looks for the implementation of ILinePaintFormat or takes default one, implemented in the class CLinePaintFormat, and forwards the call into the line paint format. The default implementation of this format draws nothing, but it takes all header's columns, constructs the CGridCell object with the cell dimention and appearance and calls the ICustomDraw::DrawCell with these parameters.

The next step is to find the default implementation of the ICellPaintFormat. Firstly, the CCustomDraw looks for the format in the CColumn. If the column does not have the format, the default implementation CCellPaintFormat will be taken. Then, the CCustomDraw calls the ICellPaintFormat::DrawCell of the found object. Finally, the cell format performs all drawings in the specified bounds with the specified appearence.

So, the ICustomDraw interface is called twice:

The interface ICustomDraw is the best point to define the drawing behaviour. For example at any moment you can change foreground or background colors:

void CMyCustomDraw::DrawCell(Dapfor::GUI::CGridCell& cell, const Dapfor::GUI::CPaintContext& paintContext, UINT paintFilter)
{
    if(cell.GetColumn() && cell.GetGridLine().GetDataObject())
    {
        //Get initial color.
        UINT color = cell.GetPaintInfo().GetForeColor();
        

        if(cell.GetColumn()->GetFid() == ...)
        {
            //Set the red color for the whole column
            cell.GetPaintInfo().SetForeColor(RGB(255, 0, 0));
        }
    }
    //Don't forget to call the function of the base class to perform drawing with new parameters
    BaseClass::DrawCell(cell, paintContext, paintFilter);
}

Another example can give some ideas how to merge the cells and draw the text in the whole line:

void CMyCustomDraw::DrawLine(Dapfor::GUI::CGridLine& line, const Dapfor::GUI::CPaintContext& paintContext, UINT paintFilter)
{
    ...

    //Prevent from drawing the text and images in cells.
    paintFilter &= paintFilter^(Dapfor::GUI::drawText|Dapfor::GUI::drawImage);

    //Draw the background without the text and images
    BaseClass::DrawLine(line, paintContext, paintFilter);


    //Save old parameters of the CDC and set the transparent mode
    int oldMode = paintContext.GetDC().GetBkMode();
    paintContext.GetDC().SetBkMode(TRANSPARENT);


    //Create some string.
    CString s(_T("This text is drawn in the whole line")); 

    //Draw the text above the line 
    CRect rc(&line.GetPaintInfo().GetVisibleRect());
    paintContext.GetDC().DrawText(s, rc, DT_CENTER|DT_END_ELLIPSIS);

    //Restore the original mode
    paintContext.GetDC().SetBkMode(oldMode);
}

Note, that this implementation can be easily put to the custom implementation of the ILinePaintFormat


Copyright Dapfor 2007-2009
Generated on Sat Jan 30 15:01:23 2010 for MFCGrid by doxygen 1.5.5