怎样控制StringGrid某一个单元格的背景色?

解决方案 »

  1.   

    procedure TfrmGridForm.StringGrid1DrawCell(Sender: TObject; Col, Row:
    Longint;
      Rect: TRect; State: TGridDrawState);
    const
         cFmtCentered =DT_SINGLELINE or DT_VCENTER or DT_NOCLIP or DT_CENTER;
         cFmtLeft = DT_SINGLELINE or DT_VCENTER or DT_NOCLIP or DT_LEFT;
         cFmtRight = DT_SINGLELINE or DT_VCENTER or DT_NOCLIP or DT_RIGHT;
    var
         iFormat : integer;
    begin
          With StringGrid1 do
          begin
             Canvas.Font := Font;
             Canvas.Font.Color := clRed;
             iFormat := cFmtRight;         if ((Col Mod 2) = 1) then
                 Canvas.Brush.Color := clWindow
             else
                 Canvas.Brush.Color := clBlue;         Canvas.FillRect(Rect);
             Rect.Right := Rect.Right - 3;
             StrPCopy(@(TempString[0]), StringGrid1.Cells[Col, Row]);
             DrawText(Canvas.Handle, TempString, -1 , Rect, iFormat);
          end;
    end;
      

  2.   

    同意楼上的,StringGrid1DrawCell事件中编写^^
      

  3.   

    lzybfs(白字先生) 下面是我手动编写的控制具体行列的函数,为什么第0行能用,第一行的某一列只能出现一半背景色?单元格列宽和行高分别为:64,17
    procedure TFrmMain.TurnSGridCellColor(CurrRow:integer;CurrCol:integer);
    //改变gGrid某一行,某一列背景色
    var
      i:integer;
      MyRect:TRect;
      incTop=18;incLeft=65;incRight=65;incBottom=18;//单元格增量
    begin
      //填充MyRect
      MyRect.Left:=CurrCol*incLeft;
      MyRect.Top:=CurrRow*incTop;
      MyRect.Right:=64+CurrCol*incRight;
      MyRect.Bottom:=17+(CurrRow)*incBottom;  MyRect.TopLeft.X:=MyRect.Left;
      MyRect.TopLeft.Y:=MyRect.Top;
      MyRect.BottomRight.X:=MyRect.Right;
      MyRect.BottomRight.Y:=MyRect.Bottom;
      //填充颜色
       with StringGrid1.Canvas  do
      begin
        brush.Color:=claqua;
        Rectangle(MyRect.Left-1,MyRect.Top-1,MyRect.Right+1,MyRect.Bottom+1);
        textout(MyRect.Left,MyRect.Top,stringgrid2.Cells[CurrCol,CurrRow]);
      end;
    end;
      

  4.   

    我又修改了修改,下面这个应该是正确的
    procedure TFrmMain.TurnSGridCellColor(CurrRow:integer;CurrCol:integer);//改变stringGrid某一行,某一列背景色
    var
      i:integer;
      MyRect:TRect;
      //
    const incTop=18;incLeft=65;incRight=65;incBottom=18;//
    begin
      //填充MyRect
      MyRect.Left:=CurrCol*incLeft;
      MyRect.Top:=CurrRow*incTop;
      MyRect.Right:=64+CurrCol*incRight;
      MyRect.Bottom:=17+CurrRow*incBottom;
      MyRect.TopLeft.X:=MyRect.Left;
      MyRect.TopLeft.Y:=MyRect.Top;
      MyRect.BottomRight.X:=MyRect.Right;
      MyRect.BottomRight.Y:=MyRect.Bottom;
      //填充颜色
       with StringGrid2.Canvas  do
      begin
        brush.Color:=claqua;
        Rectangle(MyRect.Left,MyRect.Top,MyRect.Right,MyRect.Bottom);
        textout(MyRect.Left,MyRect.Top,stringgrid2.Cells[CurrCol,CurrRow]);
       // Rectangle
      end;
    end;
      

  5.   

    偷懒的话,可以用advstringgrid,除了细化到单元格,还可以实现很多功能