-1

データのリストに従って、delphiアプリケーション内のstringgridの個々のセルに色を付けるコードをいくつか作成しました。

OnDblClick次に、stringgridのイベントにコードを記述して、セルが色付けされているかどうかを推測し、見つかった結果に従って続行する必要があります。例えば:

DOUBLE CLICK CELL  
IS CELL COLOURED  
  YES > PROCEED A  
  NO > PROCEED B
4

1 に答える 1

1

描画時の色を定義済みのTStringGrid.Objectsプロパティに保存します。Column取得する必要がある場合は、とRow座標から取得できます。これは、奇数列であるかどうかに基づいてセルに or を格納し、セルが選択されたときに格納された値を文字列として表示する単純なclWhite例です。それはあなたを始めるはずです。clBlackObjects

procedure TForm1.FormCreate(Sender: TObject);
var
  r, c: Integer;
const
  ColorSel: array[Boolean] of TColor = (clWhite, clBlack);
begin
  StringGrid1.RowCount := 10;
  StringGrid1.ColCount := 6;
  for c := 1 to StringGrid1.ColCount - 1 do
    for r := 1 to StringGrid1.RowCount - 1 do
    begin
      StringGrid1.Cells[c, r] := Format('C: %d R: %d', [c, r]);
      StringGrid1.Objects[c, r] := TObject(ColorSel[Odd(c)]);
    end;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
begin
  ShowMessage(ColorToString(TColor(StringGrid1.Objects[ACol, ARow])));
end;

イベントでこれを使用してOnMouseUp、セル内の色を簡単に検出できます。を削除しStringGrid1SelectCell(オブジェクト インスペクタを使用して、イベントの値を削除するだけです)、OnMouseUp代わりにこれをグリッドのイベントとして追加します。

procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  Col, Row: Integer;
begin
  StringGrid1.MouseToCell(X, Y, Col, Row);
  if (Col > -1) and (Row > -1) then
    ShowMessage(ColorToString(TColor(StringGrid1.Objects[Col, Row])));
end;

ダブルクリックの処理は非常に簡単になります (@TLama の大きな支援に感謝します):

procedure TForm1.StringGrid1DblClick(Sender: TObject);
var
  IsDefaultColor: Boolean;
  CurrCellColor: TColor;
  CurrCol, CurrRow: Integer;
begin
  // Save typing by grabbing the currently selected cell col/row
  CurrCol := StringGrid1.Col;   
  CurrRow := StringGrid1.Row;

  // Get the stored color for the selected cell
  CurrCellColor := TColor(StringGrid1.Objects[CurrCol, CurrRow]);

  // See if it's been painted a different color than the default
  IsDefaultColor := (CurrCellColor = StringGrid1.Color);

  if not IsDefaultColor then
    HandleDifferentColorCell
  else
    HandleNormalColorCell;
end;

セルの色を変更しないことを選択した場合でも、セルのデフォルトの色を に割り当ててObjects[Column, Row]、値を取得するときに不適切な変換を回避するために意味のあるものにする必要があることに注意してください。

于 2013-03-21T22:41:04.677 に答える