procedure TForm1.StringGrid1DblClick(Sender: TObject); var str:String; FRect:TRect; begin Str:= StringGrid1.Cells[StringGrid1.Col,StringGrid1.Row]; FRect:=StringGrid1.CellRect(StringGrid1.Col,StringGrid1.Row); StringGrid1.Canvas.Brush.Color:=clRed; StringGrid1.Canvas.Font.Color:=clWhite; StringGrid1.Canvas.FillRect(FRect); StringGrid1.Canvas.TextRect(FRect,Str); end;
//前提:StringGrid默认设置 //以下是原打算为你写双击变色的(注掉的部分),但实践了一下,要三击才会变(我不打算试下去了) //所以,把一些代码注掉了,就成了单击变色procedure TForm1.StringGrid1DblClick(Sender: TObject); begin //TStringGrid(Sender).Tag := 1; end;procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var tempS: string; BkColor,FtColor: TColor; begin with TStringGrid(Sender) do begin //if Tag = 1 then begin // Tag := 0; if (ACol>=FixedCols) and (ARow>=FixedRows) then begin tempS := Cells[ACol,ARow]; if (ACol=Col) and (ARow=Row) then begin BkColor := clRed; FtColor := clWhite; end else begin BkColor := clWhite; FtColor := clBlack; end; Canvas.Brush.Color := BkColor; Canvas.FillRect(Rect); SetTextColor(Canvas.Handle, FtColor); DrawText(Canvas.Handle, PChar(tempS), Length(tempS), Rect, 0); end; end; //end; end;
//呵呵,我犯晕了。 //参考2楼的代码改了一下。 //分应属于2楼!procedure TForm1.StringGrid1DblClick(Sender: TObject); var str:String; FRect:TRect; begin with TStringGrid(Sender) do begin Str:= StringGrid1.Cells[StringGrid1.Col,StringGrid1.Row]; FRect:=StringGrid1.CellRect(StringGrid1.Col,StringGrid1.Row); Canvas.Brush.Color:=clRed; Canvas.FillRect(FRect); SetTextColor(Canvas.Handle, clWhite); DrawText(Canvas.Handle, PChar(Str), Length(Str), FRect, 0); end; end;
override DrawCell方法,具体参考下面procedure sGrid.DrawCell(ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var DefaultColor:TColor; sText:String; begin inherited; if (gdSelected in State) then DefaultColor:=clWhite//缺省为白色 else DefaultColor:=clBlack;//缺省为黑色 SetTextColor(Canvas.Handle,DefaultColor);//缺省为黑色 if FIsRedBill then //对显示红字进行处理 begin if (ACol<>0) and (ARow<>0) then //标题区不为红字 SetTextColor(Canvas.Handle,clRed) else SetTextColor(Canvas.Handle,DefaultColor); end; if ChkColIsNumberColumn(ACol) then begin SetTextAlign(Canvas.Handle,ta_Right ); if not FIsRedBill then //对显示红字不进行处理 begin if (Copy(Trim(Cells[ACol, ARow]),1,1)='-') AND (Trim(Cells[ACol, ARow])<>'-') and (Trim(Cells[ACol, ARow])<>'--') then SetTextColor(Canvas.Handle,clRed) else SetTextColor(Canvas.Handle,DefaultColor) ; end; sText:=Trim(Cells[ACol,ARow]); if FFinBill and (GetTextColor(Canvas.Handle)=clRed) and (Copy(sText,1,1)='-') then Canvas.TextRect(Rect,Rect.Right-2,Rect.Top+3,Copy(sText,2,Length(sText)-1)) else Canvas.TextRect(Rect,Rect.Right-2,Rect.Top+3,sText); end else begin SetTextAlign(Canvas.Handle,ta_Left ); if Trim(Cells[1,ARow])='√' then SetTextColor(Canvas.Handle,clRed) else SetTextColor(Canvas.Handle,DefaultColor) ; Canvas.TextRect(Rect,Rect.Left+2,Rect.Top+3,Cells[ACol,ARow]); end;end;
var
str:String;
FRect:TRect;
begin
Str:= StringGrid1.Cells[StringGrid1.Col,StringGrid1.Row];
FRect:=StringGrid1.CellRect(StringGrid1.Col,StringGrid1.Row);
StringGrid1.Canvas.Brush.Color:=clRed;
StringGrid1.Canvas.Font.Color:=clWhite;
StringGrid1.Canvas.FillRect(FRect);
StringGrid1.Canvas.TextRect(FRect,Str);
end;
//以下是原打算为你写双击变色的(注掉的部分),但实践了一下,要三击才会变(我不打算试下去了)
//所以,把一些代码注掉了,就成了单击变色procedure TForm1.StringGrid1DblClick(Sender: TObject);
begin
//TStringGrid(Sender).Tag := 1;
end;procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
tempS: string;
BkColor,FtColor: TColor;
begin
with TStringGrid(Sender) do begin
//if Tag = 1 then begin
// Tag := 0;
if (ACol>=FixedCols) and (ARow>=FixedRows) then begin
tempS := Cells[ACol,ARow];
if (ACol=Col) and (ARow=Row) then begin
BkColor := clRed;
FtColor := clWhite;
end else begin
BkColor := clWhite;
FtColor := clBlack;
end;
Canvas.Brush.Color := BkColor;
Canvas.FillRect(Rect);
SetTextColor(Canvas.Handle, FtColor);
DrawText(Canvas.Handle, PChar(tempS), Length(tempS), Rect, 0);
end;
end;
//end;
end;
//呵呵,我犯晕了。
//参考2楼的代码改了一下。
//分应属于2楼!procedure TForm1.StringGrid1DblClick(Sender: TObject);
var
str:String;
FRect:TRect;
begin
with TStringGrid(Sender) do begin
Str:= StringGrid1.Cells[StringGrid1.Col,StringGrid1.Row];
FRect:=StringGrid1.CellRect(StringGrid1.Col,StringGrid1.Row);
Canvas.Brush.Color:=clRed;
Canvas.FillRect(FRect);
SetTextColor(Canvas.Handle, clWhite);
DrawText(Canvas.Handle, PChar(Str), Length(Str), FRect, 0);
end;
end;
State: TGridDrawState);
var DefaultColor:TColor;
sText:String;
begin
inherited;
if (gdSelected in State) then
DefaultColor:=clWhite//缺省为白色
else
DefaultColor:=clBlack;//缺省为黑色
SetTextColor(Canvas.Handle,DefaultColor);//缺省为黑色
if FIsRedBill then //对显示红字进行处理
begin
if (ACol<>0) and (ARow<>0) then //标题区不为红字
SetTextColor(Canvas.Handle,clRed)
else
SetTextColor(Canvas.Handle,DefaultColor);
end; if ChkColIsNumberColumn(ACol) then
begin SetTextAlign(Canvas.Handle,ta_Right ); if not FIsRedBill then //对显示红字不进行处理
begin
if (Copy(Trim(Cells[ACol, ARow]),1,1)='-') AND
(Trim(Cells[ACol, ARow])<>'-') and (Trim(Cells[ACol, ARow])<>'--') then
SetTextColor(Canvas.Handle,clRed)
else
SetTextColor(Canvas.Handle,DefaultColor) ;
end;
sText:=Trim(Cells[ACol,ARow]);
if FFinBill and (GetTextColor(Canvas.Handle)=clRed) and (Copy(sText,1,1)='-') then
Canvas.TextRect(Rect,Rect.Right-2,Rect.Top+3,Copy(sText,2,Length(sText)-1))
else
Canvas.TextRect(Rect,Rect.Right-2,Rect.Top+3,sText); end
else
begin SetTextAlign(Canvas.Handle,ta_Left );
if Trim(Cells[1,ARow])='√' then
SetTextColor(Canvas.Handle,clRed)
else
SetTextColor(Canvas.Handle,DefaultColor) ;
Canvas.TextRect(Rect,Rect.Left+2,Rect.Top+3,Cells[ACol,ARow]);
end;end;