假设有一stringgrid,设置defaultdrawing:=false;
有2行,第0行为fixedrows,要求单元格的文字为竖行显示(一个字一行),并要第0行的背景颜色为设置的FixedColor的颜色,比如红色.
第1行要求文字上下左右都居中.背景色为默认的白色.字的颜色为默认的黑色.不知怎么写代码,重绘的时候似乎总有问题.单独一个有求做似乎都能实现,但是几个要求搞在一起做,就不知道怎么重绘了,请指教.
在procedure TMainFrm.StringGrid1DrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);begin
  if arow=0 then
  begin
    with stringgrid1 do
    begin
      canvas.Brush.color:=StringGrid1.FixedColor;
      canvas.FillRect(Rect); 
      DrawText(StringGrid1.Canvas.Handle,
     pChar(StringGrid1.Cells[ACol,ARow]),
     Length(StringGrid1.Cells[ACol,ARow]),
     Rect,  DT_CENTER or DT_WORDBREAK or DT_VCENTER); // 折行垂直居中
    end;
  end; if arow=1 then
  begin 
    canvas.FillRect(Rect);
    DrawText(StringGrid1.Canvas.Handle,
    pChar(StringGrid1.Cells[ACol,ARow]),
    Length(StringGrid1.Cells[ACol,ARow]),
    Rect, // 包含文字的矩形
    DT_CENTER or // 水平居中
    DT_SINGLELINE or // 不折行
    DT_VCENTER); // 垂直居中
  end;end;

解决方案 »

  1.   

    这样写的话,会发现第0行没问题.但是第一行的字会出现红色的背景.
    因为第一行允许修改,修改后会出现重影.重复的字体出现在cell的左上角的位置.
      

  2.   

    把DefaultDrawing 设置为Falseprocedure TMainFrm.StringGrid1DrawCell(Sender: TObject; ACol, 
      ARow: Integer; Rect: TRect; State: TGridDrawState); begin 
      if arow=0 then 
      begin 
        with stringgrid1 do 
        begin 
          canvas.Brush.color:=StringGrid1.FixedColor; 
          canvas.FillRect(Rect); 
          DrawText(StringGrid1.Canvas.Handle, 
        pChar(StringGrid1.Cells[ACol,ARow]), 
        Length(StringGrid1.Cells[ACol,ARow]), 
        Rect, DT_CENTER or DT_WORDBREAK or DT_VCENTER); // 折行垂直居中 
        end; 
      end; if arow=1 then 
      begin 
        canvas.Brush.color:=clwhite;    
        canvas.FillRect(Rect);     
        DrawText(StringGrid1.Canvas.Handle, 
        pChar(StringGrid1.Cells[ACol,ARow]), 
        Length(StringGrid1.Cells[ACol,ARow]), 
        Rect, // 包含文字的矩形 
        DT_CENTER or // 水平居中 
        DT_SINGLELINE or // 不折行 
        DT_VCENTER); // 垂直居中 
      end; end;
      

  3.   

    不行啊
    arow=1行的cell里的字还是有背景色啊,点击修改后还是会出现重影啊
      

  4.   

    StringGrid1DrawCell事件中加入对State参数的判断gdSelected表示是选择状态,这时候你自己设置背景与前景色应该就可以了其它的参照4楼的方法.
      

  5.   

    应该是这样了。procedure TForm3.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      with stringgrid1 do
      begin
        if arow=0 then
        begin
           canvas.Brush.color:=StringGrid1.FixedColor;
          canvas.FillRect(Rect); 
          DrawText(StringGrid1.Canvas.Handle,
          pChar(StringGrid1.Cells[ACol,ARow]),
          Length(StringGrid1.Cells[ACol,ARow]),
          Rect, DT_CENTER or DT_WORDBREAK or DT_VCENTER); // 折行垂直居中
        end;
        if gdSelected in State then
        begin
         canvas.Brush.color:=clwhite;
         canvas.FillRect(Rect);
         Canvas.TextOut(Rect.Left , Rect.Top , Cells[ACol, ARow]);
        end;
        if arow=1 then
        begin
          canvas.Brush.color:=clwhite;
          canvas.FillRect(Rect);
          DrawText(StringGrid1.Canvas.Handle,
          pChar(StringGrid1.Cells[ACol,ARow]),
          Length(StringGrid1.Cells[ACol,ARow]),
          Rect, // 包含文字的矩形
          DT_CENTER or // 水平居中
          DT_WORDBREAK or // 不折行
          DT_VCENTER); // 垂直居中
        end;
      end;
    end;