In your DrawCell method of TMcStringGrid put an "exit" after the line "inherited" and run your demo --> you will see a grid - the first drawing action.
Then remove the "exit" and comment the "inherited" - this will execute only your code. But you'll see a grid again.
So, in total the grid is painted twice, by "inherited" and by your code.
I recommend that you place a breakpoint in DrawAllRows of TCustomGrid, then step through the entire code to see which methods are called - that's how I learned it.
This is the sequence of methods being called.
- DrawAllRows (called from Paint)
- DrawRow
- DrawCell
- DefaultDrawCell
- DrawFillRect, DrawColumnCell, DrawTextInCell.
- DrawTextIncell calls DrawCellText which finally paints the text in the cell
You override DrawCell which is very early in the sequence of calls. Your first call is "inherited" - this means your grid does the same as any other standard string grid, i.e. it calls all these methods mentioned.
After the "inherited" you have a boolean condition "if 1=1" - this is always true, and therefore you execute all the steps following. And these steps essentially do: Fill the cell rect (this is what DrawFillRect above is doing), paint a rounded rect over it, and finally paint the text.
In the end, the "if 1=1" part paints the cell a second time.
I don't know what you want to achieve. I suppose you want every cell to have round corners. The correct way to achieve this would be to override the method DrawFillRect. Unfortunately this method is not virtual and thus is not called from an inherited grid. You can turn off the property DefaultDrawing and hook your own code (the one after "if 1=1") into the OnDrawCell event hander - having DefaultDrawing = false means that the sequence of methods after DefaultDrawCell is not executed any more. But I think the merged cells will not work any more (not 100% sure - I did not try it).
The other possibility, since TMcStringGrid is a derived grid anyway, is to copy the code from DefaultDrawCell into the mcGrids units. There must be some corrections because the original method calls other routines not accessible here. Then you can add the DrawRoundRect after the DrawfillRect:
procedure TMCStringGrid.DefaultDrawCell(aCol, aRow: Integer; var aRect: TRect;
aState: TGridDrawState);
begin
if goColSpanning in Options then CalcCellExtent(acol, arow, aRect);
if (TitleStyle=tsNative) and (gdFixed in AState) then
DrawThemedCell(aCol, aRow, aRect, aState)
else
DrawFillRect(Canvas, aRect);
Canvas.RoundRect(ARect, 11, 11);
if (goFixedRowNumbering in Options) and (ARow>=FixedRows) and (aCol=0) and (FixedCols>0) then
DrawCellAutonumbering(aCol, aRow, aRect, IntToStr(aRow-FixedRows+1));
//draw text
if GetIsCellTitle(aCol, aRow) then
DrawColumnText(aCol, aRow, aRect, aState)
else
DrawTextInCell(aCol,aRow, aRect,aState);
end;
With this modification and removing your DrawCell method I get a speedup of the painting time from 0.3 sec to 0.11-0.12 sec
Note that I dropped some internal features of the usual grid here, like checkbox drawing or button cells.