15

FillRect doesn't paint the complete TStringGrid cell in Delphi XE2. There is a gap of 3 pixels on the left side in the default color (with BiDiMode set to bdLeftToRight). This problem doesn't exist in Delphi 6 which I used before.

procedure TShapeline.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  Stringgrid1.Canvas.Brush.Color:=$00FF80FF;
  StringGrid1.Canvas.FillRect(Rect);
end;

I tried to change all properties (including the DrawingStyle) and different brush styles, the painted rectangle doesn't fill the complete cell.

bluish
  • 26,356
  • 27
  • 122
  • 180
Finike
  • 151
  • 1
  • 4

4 Answers4

18

This is expected behaviour in XE2 when DefaultDrawing = true and themes are enabled (I'm not going to argue about good or bad here - as you might have noticed, the behaviour is different for RigthToLeft mode...).

A workaround is to check for this condition and decrement Rect.Left by 4 pixel before calling FillRect.

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
  • 1
    Isn't there a way to find out how many pixels to decrement? I really don't like hard coded values for these situations. It will break as soo many code broke going from XP to Vista and the new border thicknesses. – Marjan Venema Jan 10 '12 at 17:34
  • 2
    @Marjan - That's a hardcoded '4' in the VCL source (TStringGrid.DrawCell). It is not a property of a style, not a constant,.. just '4'. – Sertac Akyuz Jan 10 '12 at 17:45
  • Thank you - decrementing Rec.Left by 4 pixels works and solves my problem! – Finike Jan 10 '12 at 21:32
  • 1
    Stumbled across this after we had run into this issue and had hard-coded a fix that turned out to NOT work on Win2008 server or XP but did work on Win 7. Adding a check for default drawing AND style services enabled allowed us to appropriately apply the 4 pixel shift to the left when necessary. if TStringGrid(Sender).DefaultDrawing and (StyleServices.Enabled) then... – TJ Asher Dec 14 '12 at 15:48
  • I would rather draw my own grid than to rely on this Windows drawing madness! Which is exactly what I do now in all my grids, even when I'm using VCL styles. I should probably encapsulate this drawing in my own grid control... – Jerry Dodge May 08 '14 at 01:53
4

You can use the StringGrid1.CellRect(ACol, ARow) that returns the actual TRect of the cell instead of using the parameter Rect.

0

Since you're drawing the grid cell yourself then just turn off the grid property DefaultDrawing, set it to false.

Mike
  • 1
  • 1
    The code shown does not draw the grid cell; it draws the *content* of the grid cell. If you turn off DefaultDrawing, you have to output the text and grid lines as well, which is a lot more code. It's easier to just adjust the rect by a few pixels to adjust for the padding added by themed drawing, as mentioned in [Uwe's answer](http://stackoverflow.com/a/8807002/62576) above. – Ken White May 15 '15 at 20:35
0

Turn off the first 4 options in TStringGrid:

  • goFixedVertLine
  • goFixedHorizLine
  • goVertLine
  • goHorizLine

Then it won't paint the grid lines, and your grid cells will paint right to the edges. Just tried it with XE.

bluish
  • 26,356
  • 27
  • 122
  • 180
Bruce McGee
  • 15,076
  • 6
  • 55
  • 70
  • 2
    What if OP does want the gridlines? – Marjan Venema Jan 10 '12 at 17:34
  • Then my answer probably won't help, but just turning off the fixed lines seems to help. – Bruce McGee Jan 10 '12 at 17:36
  • @Bruce, your fix is correct for XE but not XE2. There's literally code in TStringGrid.DrawCell that does ARect.Left := ARect.Left + 4 if DefaultDrawing is true and StyleServices are enabled. – Mike W Jan 10 '12 at 17:44
  • @Mike, I tested against XE because that's how the original question was tagged (didn't read the description closely enough). It didn't occur to me that it would be different in XE2. Yikes, indeed. – Bruce McGee Jan 10 '12 at 18:42
  • @Bruce, yikes is right. It really seems like this will break a lot of existing owner draw code. I added the delphi-xe2 tag for future reference. – Mike W Jan 10 '12 at 18:51