FillRect doesn't paint the complete TStringGrid cell in Delphi XE2
Asked Answered
G

4

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.

Gilbertegilbertian answered 10/1, 2012 at 15:54 Comment(0)
A
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.

Appellant answered 10/1, 2012 at 16:45 Comment(5)
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.Hussite
@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'.Kianakiang
Thank you - decrementing Rec.Left by 4 pixels works and solves my problem!Gilbertegilbertian
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...Hell
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...Waddell
C
4

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

Chromolithograph answered 11/1, 2012 at 13:21 Comment(1)
Oops, that's a heavy routine within a OnDrawCell event handler!Torino
O
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.

Obstetrics answered 10/1, 2012 at 16:42 Comment(5)
What if OP does want the gridlines?Hussite
Then my answer probably won't help, but just turning off the fixed lines seems to help.Obstetrics
@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.Frunze
@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.Obstetrics
@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.Frunze
H
0

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

Helton answered 15/5, 2015 at 20:28 Comment(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 above.Trilbie

© 2022 - 2024 — McMap. All rights reserved.