procedure TFrmMain.StringGridDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var Area:TRect;
begin  if not (TObject(MyList.Items[ListNo[1]]) is TStringGrid) then exit;
  With Tstringgrid(MyList.Items[ListNo[1]]) do
  begin
    If  ARow= 0  then
    begin
       Canvas.Brush.Color :=TempColor;// ClBlue;
       Canvas.FillRect(Rect);
       Canvas.font.color:=ClBlack;
       Canvas.TextOut(rect.left , rect.top, cells[acol, arow]);
    end;
  end;
  with Tstringgrid(MyList.Items[ListNo[1]]),Tstringgrid(MyList.Items[ListNo[1]]).Canvas do
  begin
    Brush.Style:=bsClear;
    FillRect(Rect);
    Area:= Rect;
    InflateRect(Area, -2, -2);    if (ARow=0) and (DT_flag=2) then
     DrawText(Handle, PChar(Cells[ACol, ARow]),Length(Cells[ACol, ARow]), Area, DT_CENTER)
  .............................我的Grid是动太生成的,OnDrawCell:=StringGridDrawCell;但运行时,Cell上的文字显示了两个,一次就是我设的居中,另外一次,可能是系统本身的DrawCell;
另外如果动态生成多个GRID的话,点到那个那个的标题栏色为黄色(我的DrawCell中设置的),不点为白色。
我想用我自己的DrawCell去override系统本身的。。告诉我这不是一个Base..不成功
请高手帮我。

解决方案 »

  1.   

    1.我觉得你既然要动态生成有自己特性的StringGrid,那干脆直接从StringGrid派生一个新类来得更简单,也可获得更多的控制。
    2.在派生类中重写DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);
    3.不要用 DrawText(Handle, PChar(Cells[ACol, ARow]),Length(Cells[ACol, ARow]), Area, DT_CENTER)
    改用这样的方式
    var
      si : tagSize;
      cx : integer;
    begin
    ....
      si := Canvas.TextExtent(str);
      cx := ARect.Left + (ARect.Right - ARect.Left - si.cx) div 2;
      
      Canvas.TextRect(ARect, cx , ARect.Top + (ARect.Bottom - ARect.Top -si.cy) div 2 , str);
    ....
    end;
    这样就不会有两重字的出现了,(其实还是会每个格子画两次的,只是TextRect把上一次的东西清干净而已)。