我用如下方法画了颜色
StringGrid1.Canvas.brush.Color:=clred;                         
StringGrid1.Canvas.FillRect(stringGrid1.CellRect(1,1);
当有一个窗口刷过StringGrid1上面画的颜色就没有了,有什么好办法可以使上面的颜色不会被刷取

解决方案 »

  1.   

    //处理StringGrid::OnDrawCell事件,参考如下代码
    type
      TStringGridEx = class(TStringGrid);procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      if not ((ACol = 1) and (ARow = 1)) then Exit;
      TStringGridEx(Sender).OnDrawCell := nil;
      try
        TStringGridEx(Sender).Canvas.Brush.Color := clYellow;
        TStringGridEx(Sender).DrawCell(ACol, ARow, Rect, State);
      finally
        TStringGridEx(Sender).OnDrawCell := StringGrid1DrawCell;
      end;
    end;
      

  2.   

    不行颜色还是被刷掉了我是在一个click事件中根据判断条件刷stringgrid的颜色
      

  3.   

    首先用我的代码调试过没有?如果调试过,哪里不符号要求?
    如果看不到效果,就贴出调试的代码,好给你分析StringGrid1.Canvas.brush.Color:=clred;                         
    StringGrid1.Canvas.FillRect(stringGrid1.CellRect(1,1);
    两条代码我还花时间问吗?
      

  4.   

    多谢zswang 
    我是根据一个比较耗时的查询语句检测是否标为红色,而Stringgrid的OnDrawCell事件每次刷新都要重绘,要是每次重绘都执行查询语句会影响速度所以我将重绘颜色放在Button的clck事件中请问有什么好的解决办法吗
      

  5.   

    var
      vCol, vRow: Integer;procedure TForm1.Button1Click(Sender: TObject);
    begin
      { TODO : 执行查询语句 }
      { TODO : 得到vCol, vRow }
      StringGrid1.Repaint;
    end;type
      TStringGridEx = class(TStringGrid);procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      if not ((ACol = vCol) and (ARow = vRow)) then Exit;
      TStringGridEx(Sender).OnDrawCell := nil;
      try
        TStringGridEx(Sender).Canvas.Brush.Color := clYellow;
        TStringGridEx(Sender).DrawCell(ACol, ARow, Rect, State);
      finally
        TStringGridEx(Sender).OnDrawCell := StringGrid1DrawCell;
      end;
    end;