0

I have a form. On formshow, I initialize values of a field into stringgrid cells, but it shows a shadow under cell's texts. I've used Persian charaters for field's values.
I did the same with english values, but it works fine.
I appreciate any suggestions.

example of the output:

enter image description here

bummi
  • 27,123
  • 14
  • 62
  • 101
Holy Thirteen
  • 158
  • 2
  • 8
  • 3
    Looks as if you did implement a DrawCell and not filling the Rect before using TextOut. – bummi Jul 21 '13 at 06:20
  • s := (Sender as TStringGrid).Cells[ACol, ARow]; if Length(s) > 0 then begin drawrect := Rect; DrawText((Sender as TStringGrid).Canvas.handle, Pchar(s), Length(s), drawrect, DT_CALCRECT or DT_WORDBREAK or DT_LEFT); if (drawrect.bottom - drawrect.Top) > (Sender as TStringGrid).RowHeights[ARow] then (Sender as TStringGrid).RowHeights[ARow] := (drawrect.bottom - drawrect.Top) else begin drawrect.Right := Rect.Right; (Sender as TStringGrid).Canvas.FillRect(drawrect); DrawText((Sender as TStringGrid).Canvas.handle, Pchar(s), Length(s), drawrect, DT_WORDBREAK or DT_LEFT); end; – Holy Thirteen Jul 21 '13 at 10:56
  • I can not see the need for different handling depending on the lenght of s, however if you add `(Sender as TStringGrid) .Canvas.FillRect(Rect);` as first routine, you problem should have been one, since with enabled DefaultDrawing' the rext will have been already painted to the grid. – bummi Jul 21 '13 at 11:37
  • I used first solution and it worked correctly. Thanks for your solution. – Holy Thirteen Jul 21 '13 at 12:40

1 Answers1

2

With enaabled DefaultDrawing the text will be already rendered if you enter OnDrawCell.

Since you are calculating the needed rowheight in painting using DT_CALCRECT of DrawText you will have to calculated the Rect wich shall be filled/cleared with FillRect.
You can use UnionRect to get the final Rect which has to be filled (FillRect in the example).

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.Cells[1,1] := 'Hallo'#13'World';
  StringGrid1.Cells[2,2] := 'اهای' +13# + 'جهان';
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  S:String;
  drawrect,Fillrect : TRect;
begin
  s := (Sender as TStringGrid).Cells[ACol, ARow];
  drawrect := Rect;
  DrawText((Sender as TStringGrid).Canvas.handle, Pchar(s), Length(s),
      drawrect, DT_CALCRECT or DT_WORDBREAK or DT_LEFT);
  if (drawrect.bottom - drawrect.Top) > (Sender as TStringGrid)
      .RowHeights[ARow] then (Sender as TStringGrid)
      .RowHeights[ARow] := (drawrect.bottom - drawrect.Top);
  UnionRect(FillRect,Rect,DrawRect);
  (Sender as TStringGrid).Canvas.FillRect(FillRect);
  DrawText((Sender as TStringGrid).Canvas.handle, Pchar(s), Length(s),
        drawrect, DT_WORDBREAK or DT_LEFT);
end;
bummi
  • 27,123
  • 14
  • 62
  • 101