Board index » delphi » TStringGrid Cell width question

TStringGrid Cell width question

I seem to be seeing a discrepancy in the actual width of a column and the
DefaultColWidth in TStringGrid.
Here is the problem:  I set the DefaultColWidth to 100.  Then I use the
following DrawCell handler:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
   FMode:       Integer;
   Msg:         String;
begin
     with (Sender as TStringGrid).Canvas do
       begin
       Msg := 'L=' + IntToStr(Rect.Left) + ' R=' + IntToStr(Rect.Right);
       FMode := DT_CENTER or DT_SINGLELINE or DT_VCENTER;
       DrawText(Handle,PChar(Msg),Length(Msg),Rect,FMode);
       end;
end;

This handler draws a text string that looks like this for the cells in Col
0:
L=0 R=100

The problem I have with this is that this seems to indicate that the cell
width is actually 101.
Wouldn't you calculate the width with this formula?
width := Rect.Right - Rect.Left + 1

Thanks
Ken McClain
kenmccl...@mindspring.com

 

Re:TStringGrid Cell width question


In article <7ph4o6$4k...@forums.borland.com>, Ken McClain stated:
Quote
> The problem I have with this is that this seems to indicate that the cell
> width is actually 101.
> Wouldn't you calculate the width with this formula?
> width := Rect.Right - Rect.Left + 1

No. TRect wraps the windows RECT structure. From the API help:

"When RECT is passed to the FillRect function, the rectangle is filled up
to, but not including, the right column and bottom row of pixels. This
structure is identical to the RECTL structure."

IOW Right and Bottom are not drawn so the "+ 1" in your calculation is what
is causing the (apparent) error.
--
Regards
Ralph (TeamB)
--

Other Threads