1

私はこれをやっています:

procedure TForm1.BitBtn1Click(Sender: TObject);
 var dtStart: TDateTime;
  I: Integer;
begin
  dtStart := DateTimePicker1.Date;
  for I := 0 to 7 do
    AdvStringGrid1.Cells[I+1, 0] := uppercase(FormatDateTime('DD/MM/YYYY     DDD', dtStart + I));
   end;

(例)日曜日(SUN)が表示されたときに列に色を付ける方法はありますか? SUN 列 (ずっと下) を他の列とは異なる色で表示したいと思います。

4

2 に答える 2

4

OnDrawCellこれは、イベントを使用して行うことができます( False に設定しないでください)。DefaultDraw通常の例を次に示しTStringGridます。

// Sample to populate the cells with the days of the week
procedure TForm1.FormShow(Sender: TObject);
var
  r, c: Integer;
begin
  StringGrid1.ColCount := 8;  // Ignore fixed column and row for this example
  StringGrid1.RowCount := 8;

  for c := 1 to StringGrid1.ColCount - 1 do
    for r := 1 to StringGrid1.RowCount - 1 do
      StringGrid1.Cells[c, r] := FormatSettings.ShortDayNames[c];
end;

// Assign this to the StringGrid's OnDrawCell using the Object Inspector 
// Events tab.
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  CellText: string;
begin
  if (ARow > 0) and (ACol > 0) then
  begin
    CellText := StringGrid1.Cells[ACol, ARow];
    if Pos('Sun', CellText) > 0 then
    begin
      StringGrid1.Canvas.Brush.Color := clRed;
      StringGrid1.Canvas.FillRect(Rect);
    end
    else
      StringGrid1.Canvas.Brush.Color := clWindow;
  end;

  // The '+ 4' is from the VCL; it's hard-coded when themes are enabled. 
  // You should probably check the grid's DrawingStyle to see if it's 
  // gdsThemed, and adjust as needed. I leave that as an exercise for you.
  StringGrid1.Canvas.TextOut(Rect.Left + 4, Rect.Top + 4, CellText);
end;

上記の正確なコードのサンプル出力:

ここに画像の説明を入力

これは、まさにあなたが望むものを出力する2番目の例です(SUNを大文字に変換しなかったことを除いて):

procedure TForm1.FormShow(Sender: TObject);
var
  r, c: Integer;
begin
  StringGrid1.DefaultColWidth := 100;
  StringGrid1.ColCount := 8;
  StringGrid1.RowCount := 8;

  for c := 1 to StringGrid1.ColCount - 1 do
    for r := 1 to StringGrid1.RowCount - 1 do
      StringGrid1.Cells[c, r] := FormatDateTime('mm/dd/yyyy ddd', 
                                                Date() + c + r - 1);
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  CellText: string;
begin
  if (ARow > 0) and (ACol > 0) then
  begin
    CellText := StringGrid1.Cells[ACol, ARow];
    if Pos('Sun', CellText) > 0 then
      StringGrid1.Canvas.Brush.Color := clRed
    else
      StringGrid1.Canvas.Brush.Color := clWindow;
    StringGrid1.Canvas.FillRect(Rect);
  end;
  StringGrid1.Canvas.TextOut(Rect.Left + 4, Rect.Top + 4, CellText);
end;

2 番目のサンプルに一致するキャプチャを次に示します。

ここに画像の説明を入力

于 2012-05-10T13:24:41.030 に答える
0

DrawCell プロシージャを使用する方法です。この例では、「Sun」を含む列が任意の列に含まれる可能性を処理しています。DefaultDrawing をデフォルトの true のままにします。


    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
         Rect: TRect; State: TGridDrawState);
    begin
      with Sender as TStringGrid do
       if Pos('Sun', Cells[ACol, 0])>0 then begin
         Canvas.Brush.Color := clRed;
         Canvas.FillRect(Rect);
         Canvas.Font.Color := clwhite;
         Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, Cells[ACol, ARow]);
       end;
      end;
    

于 2012-05-12T17:03:43.633 に答える