TAdvStringGrid cells with brush styles get corrupted when window is resized.

I am decorating various cells with background cross-hatching in a TAdvStringGrid based on specific conditions. A simple example follows.

procedure TMyTestForm.onGridGetCellColor(Sender: TObject;
ARow, ACol: Integer; AState: TGridDrawState; ABrush: TBrush; AFont: TFont);
if someConditionIn( ARow, ACol ) then
ABrush.Color := clMoneyGreen;
ABrush.Style := bsSolid;
else if someOtherConditionIn(ARow, ACol) then
ABrush.Color := clGray;
ABrush.Style := bsDiagCross;
// else retain current color... nothing else to do!

It works great. However, if I reduce the height (or width) of the window by grabbing its edges (or corners) to reduce the size of the window enough to hide the grid, and then resizing the window back to its original size (thus making the grid visible again), the cross-hatching (bsDiagCross in the above example) appears corrupted as well as having extraneous text in them that wasn't intended to be there.

I attempted to find an example in the TMS demo files, but there are so many sample directories, I'm unable to find any demo project that can clarify what I am forgetting to do.

Hope this makes sense.


This is because the grid performs partial redraw by default and drawing with a brush style in VCL uses the pattern from the coordinates of redraw rather than the cell coordinates.
The fastest solution is to set grid.DoubleBuffered = true to avoid such partial redraw.

1 Like

Thanks Bruno. Works beautifully.

(Let me know if I should have posted this as a brand new question. But being in the same area of code, figured this fit right in...)

Regarding TAdvStringGrid's OnGetCellColor, when applying a brush style that is not bsSolid (in my case, I'm using bsDiagCross), I realize that the ABrush.Color is used for the line color. But how do you control the background color of the cell? It's always being painted as black.

A simple code example:

procedure myGrid.scheduleGridGetCellColor(Sender: TObject; ARow, ACol: Integer;
AState: TGridDrawState; ABrush: TBrush; AFont: TFont);
ABrush.Color := clMaroon; // this is the line color
ABrush.Style := bsDiagCross; // the cross-hatching style
// How do I control the cell's background color? I'm always seeing black.

Thanks for reading.

In Delphi, the brush background color for this types of brushes is set by SetBkColor()

This brings back memories of the wierdness with setting colors when using complex brush styles (i.e., not bsSolid). Thank you for that link, which I recall seeing myself. With that and another link I found here, I got it to work.

type THackAdvStringGrid = class(TAdvStringGrid);

procedure TForm1.myOnGetCellColor(Sender: TObject; ARow, ACol: Integer;
AState: TGridDrawState; ABrush: TBrush; AFont: TFont);
var cellRect : TRect;
if ( some-condition-for-using-this-effect ) then
// Set clRed cross lines over a clSilver background
ABrush.Style := bsDiagCross;
ABrush.Color := clRed ; // RED LINES

SetBkColor( myGrid.Canvas.Handle, ColorToRGB(clSilver) ); // SILVER BACKGROUND
cellRect := THackAdvStringGrid( scheduleGrid ).CellRect(ACol,ARow);
myGrid.Canvas.FillRect( cellRect );

It's too bad the only way to get the desired effect is with a "hack" to expose a needed function for getting the rect coordinates from the ARow:ACol values. I wonder, however, why this isn't done automatically in TAdvStringGrid code.

Thanks for your assistance.

It is not automatically done for performance reasons.

Note to anyone reading my post above: scheduleGrid in above example code block should be: myGrid:

cellRect := THackAdvStringGrid( myGrid ).CellRect(ACol,ARow);