Delphi StringGrid高亮显示行和列

Delphi StringGrid高亮显示行和列,delphi,Delphi,我有一个StringGrid,我在固定的行和列上为单击的单元格的位置着色。到目前为止看起来是这样的: 为此,我使用了以下代码: procedure TForm2.sgDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var md: integer; begin with sg do begin Canvas.Brush.Color:= clwhite;

我有一个StringGrid,我在固定的行和列上为单击的单元格的位置着色。到目前为止看起来是这样的:

为此,我使用了以下代码:

procedure TForm2.sgDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  md: integer;
begin
  with sg do
    begin
   Canvas.Brush.Color:= clwhite;
   if ((sg.Row = ARow)and(ACol=0)) or ((sg.Col = ACol)and(ARow=0)) then
      Canvas.Brush.Color:= $00FFDE9B; //your highlighted color
   Canvas.FillRect(Rect);
   Canvas.TextOut(0, Rect.top + 4, cells[ACol, ARow]);
  end;
  if gdSelected in State then
    sg.Canvas.DrawFocusRect(Rect);
end;
当然还有在OnMouseDown中失效

procedure TForm2.sgMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  sg.invalidate;
end;
现在,我想给顶部的所有选定行上色。因此,像在图像中一样,选择了4个单元格,但在固定区域中只有一个单元格为蓝色(第4列)。我希望所有相应的固定单元格都是蓝色的。(在本例中:第4列、第5列、第6列、第7列)

有什么想法吗

编辑

这样做的目的是在使用鼠标选择时显示选择,而不是使用SHIFT+单击来显示选择

procedure TForm3.sgDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  md: integer;
begin
  with sg do
    begin

   Canvas.Brush.Color:= clwhite;

   if ( (Arow >=selection.Top) and (Arow<=selection.Bottom) and(ACol=0)) or
      ( (ACol>=selection.Left) and (Acol<=selection.Right) and(ARow=0)) then
         Canvas.Brush.Color:= $00FFDE9B; //your highlighted color
   Canvas.FillRect(Rect);
   Canvas.TextOut(0, Rect.top + 4, cells[ACol, ARow]);
  end;
  if gdSelected in State then
    sg.Canvas.DrawFocusRect(Rect);
end;

procedure TForm3.sgMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
      if (ssLeft in shift) then sg.Invalidate;
end;
procedure TForm3.sgDrawCell(发送方:TObject;ACol,ARow:Integer;Rect:TRect;State:TGridDrawState);
变量
md:整数;
开始
用sgdo
开始
Canvas.Brush.Color:=clwhite;

if((Arow>=selection.Top)和(Arow=selection.Left)和(Acol
if((Acol=0)和InRange(Arow,selection.Top,selection.Bottom))或((Arow=0)和InRange(Acol,selection.Left,selection.Right))然后Canvas.Brush.Color:=$00FFDE9B;
?在JediVCL中,有一个具有DefaultDrawCell方法的网格-因此您只需在事件处理程序中设置颜色,并在(gdFixed in State)和(InRange(ARow,Selection.Top,Selection.Bottom)或InRange(ACol,Selection.Left,Selection.Right)中调用itOr然后Canvas.Brush.Color:=$00FFDE9B;
。伙计们,我的问题问错了……我正在寻找一种方法,当鼠标用于选择多个单元格时,对所述固定单元格进行着色。该问题会使我的答案过时。请不要这样做。最好对控件进行子类化,并覆盖
SelectionMoved
方法。这种方法不起作用我会考虑。它听起来很棒,但我不能:方法“选择移动”没有在基类中找到。记住我的基类是TSTRGRIDGRID,我使用Delphi xPesith.To必须在以后的版本中有非私有的。然后我想你需要一些丑陋的黑客。丑陋的黑客……你能帮忙吗?e可能性。