unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids;Const WM_MYMESSAGE = WM_USER+1;type TForm1 = class(TForm) DrawGrid1: TDrawGrid; procedure DrawGrid1GetEditText(Sender: TObject; ACol, ARow: Integer; var Value: String); procedure DrawGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean); procedure DrawGrid1SetEditText(Sender: TObject; ACol, ARow: Integer; const Value: String); procedure DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); private { Private declarations } Function GetCellText(ACol,ARow:Integer):String; Protected public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DrawGrid1GetEditText(Sender: TObject; ACol, ARow: Integer; var Value: String);begin//获得当前单元格内容end;procedure TForm1.DrawGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);begin//CanSelect用于控制是否可以选中当前单元格end;procedure TForm1.DrawGrid1SetEditText(Sender: TObject; ACol, ARow: Integer; const Value: String);begin//保存当前单元格内容end;procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);Const CHARARR:ARRAY [1..2] OF String=('你','好'); COLARR:ARRAY [1..2] OF TColor=(clRed,clBlue);Var X,Y,I:Integer; iPos:Integer; Flags:integer; TmpRect:TRect; S,Head:String;begin //绘制内容 S := GetCellText(ACol,ARow); //汇总默认值 X:=Rect.Left+2; Y:=Rect.Top+2; Flags:=DT_WORDBREAK ; TmpRect:=Rect; OffsetRect(TmpRect,2,2); TmpRect.Bottom:=TmpRect.Bottom-2; TmpRect.Right:=TmpRect.Right-2; if (ARow Mod 2) = 0 then TDrawGrid(Sender).Canvas.Brush.Color:=clblue else TDrawGrid(Sender).Canvas.Brush.Color:=clYellow; TDrawGrid(Sender).Canvas.FillRect(TmpRect); DrawText(TDrawGrid(Sender).Canvas.Handle, PChar(S), Length(S), TmpRect, Flags); //处理特殊颜色 For I:=Low(CHARARR) to High(CHARARR) do Begin iPos := Pos( CHARARR,S); Y:=Rect.Top+2; if iPos>0 then begin Head:=Copy(S,1,iPos-1); X:=Rect.Left+2+ TStringGrid(Sender).Canvas.TextWidth(Head); TDrawGrid(Sender).Canvas.Font.Color:=COLARR; TDrawGrid(Sender).Canvas.TextOut(X,Y,CHARARR); end; End;end;function TForm1.GetCellText(ACol, ARow: Integer): String;begin result:=inttostr(ACol)+',好'+#13#10+Inttostr(ARow); //测试案例end;end.