我在TStringGrid中有报告,需要按空格键时,更改所选水平线单元格的bg颜色。
怎么做呢
答案 0 :(得分:0)
TSelColor = class
public
Color: TColor;
constructor Create(const aColor: TColor);
end;
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
kPressed: boolean;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
yourColor: TColor;
begin
yourColor:= clRed;
if gdFixed in State then
TStringGrid(Sender).Canvas.Brush.Color:= clBtnFace
else if gdSelected in State then
begin
TStringGrid(Sender).Canvas.Brush.Color:= clAqua;
if kPressed and not (TStringGrid(Sender).Objects[ACol, ARow] is TSelColor) then
TStringGrid(Sender).Objects[ACol, ARow]:= TSelColor.Create(yourColor)
else if kPressed and (TStringGrid(Sender).Objects[ACol, ARow] is TSelColor) then
TStringGrid(Sender).Objects[ACol, ARow]:= nil;
end
else
begin
TStringGrid(Sender).Canvas.Brush.Color:= clWindow;
if TStringGrid(Sender).Objects[ACol, ARow] is TSelColor then
TStringGrid(Sender).Canvas.Brush.Color:= TSelColor(TStringGrid(Sender).Objects[ACol, ARow]).Color;
end;
TStringGrid(Sender).Canvas.FillRect(Rect);
TStringGrid(Sender).Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, TStringGrid(Sender).Cells[ACol, ARow]);
end;
procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = 32 then
begin
kPressed:= true;
StringGrid1.Repaint;
kPressed:= false;
end;
end;
{ TSelColor }
constructor TSelColor.Create(const aColor: TColor);
begin
inherited Create;
Color:= aColor;
end;
StringGrid1
DefaultDrawning [false]
Options.goRowSelect [true]