unit NormalForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, BaseForm, AnyPrint, AnyGrids, ComCtrls, Buttons, ExtCtrls,
  StdCtrls, ADODB;

type
  TfrmNormal = class(TfrmBase)
    imgPicture: TImage;
    Label1: TLabel;
    Edit1: TEdit;
    imgPhone: TImage;
    procedure FormCreate(Sender: TObject);
    procedure AnyGridDrawCell(Sender: TObject; Row, Col: Integer;
      Canvas: TCanvas; R: TRect; var Handled: Boolean);
    procedure FormDestroy(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure btnPrintClick(Sender: TObject);
  private
    FRecCount:integer;
    FLines:TStrings;
    FKeyWord:string;
    FADOQuery:TADOQuery;
  public
    { Public declarations }
  end;

var
  frmNormal: TfrmNormal;

implementation

{$R *.dfm}

uses Share;

type
  TOutTextInfo=record
    Index:integer;
    StartPos:integer;
    EndPos:integer;
    FontColor:TColor;  //前景色
    BackColor:TColor;  //背景色
    Level:integer;     //关键字的级别，高级别的关键字在界面上会优先显示
    CanShow:Boolean;   //等于True才可以在界面上进行绘制
  end;
  POutTextInfo=^TOutTextInfo;
  THackStrings=class(TStrings);


procedure TfrmNormal.FormCreate(Sender: TObject);
var
  i, j: integer;
begin
  inherited;
  WindowState:=wsNormal;
  FADOQuery:=TADOQuery.Create(Self);
  FADOQuery.ConnectionString:=Format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%sdb.mdb;Persist Security Info=False',
    [ExtractFilePath(Application.ExeName)]);
  FADOQuery.SQL.Text:='select content,addtime,rowindex from TruckInfo';
  FADOQuery.Open;
  FLines:=TStringList.Create;
  FKeyWord:='天津';
  with FADOQuery do
  begin
    FRecCount := RecordCount;
    j:=FRecCount;
    AnyGrid.Rows:=j; //指定行数
    AnyGrid.BeginUpdate;
    DisableControls;
    AnyGrid.ColWidth[0] := 50;
    for i := 0 to j - 1 do
    begin
      with AnyGrid do
      begin
        with Cells(i,1) do
        begin
          WordWrap:=True;
          Text:=Format('%s[%s]',[Fields[0].AsString,Fields[1].AsString]);
          Obj:=Pointer(0); //设置为0，表示要重新计算一次行高
          TextTMargin:=5;
          TextLMargin:=4;
        end;
        with Cells(i, 0) do
        begin
          Cells(i, 0).Text := IntToStr(j);
          Cells(i, 0).Font.Color := clBlue;
          Cells(i, 0).Font.Size := 10;
          Cells(i, 0).HorAlignment := taCenter; //文本水平居中
          Cells(i, 0).VerAlignment := vtaCenter; //文本垂直居中
          Cells(i, 0).TextTMargin := imgPhone.Picture.Height + 1;//设置文本的上边距
          Cells(i, 0).Graphic.Assign(imgPhone.Picture);
          Cells(i, 0).GraphicVerAlign := vtaTop; //图形上居中
          Cells(i, 0).Transparent := True; //图形透明
          Cells(i, 0).TransparentColor := clWhite; //图形透明色
        end;
        Dec(j);
        Next;
      end;
    end;
    AnyGrid.EndUpdate;
    EnableControls;
  end;
end;

procedure TfrmNormal.AnyGridDrawCell(Sender: TObject; Row, Col: Integer;
  Canvas: TCanvas; R: TRect; var Handled: Boolean);
var
  H,i,T,L:integer;
  S,Text:WideString;
  OutTexts:TList;
  P:POutTextInfo;
  j,Index,Len:integer;
  Find:Boolean;
begin
  if Row<FRecCount then
  begin
    if (Col=1) then
    begin
      //由于自动行高功能速度很慢，所以只在需要的时候才去设置，以便提高速度
      if (integer(AnyGrid.Cells(Row,Col).Obj)=0) then  //等于0表示还没有计算过行高
      begin
        H:=AnyGrid.GetRowAutoHeight(Row,1,AnyGrid.Cells(Row,Col).Text);
        if H<>AnyGrid.RowHeight[Row] then
          AnyGrid.RowHeight[Row]:=H;
        AnyGrid.Cells(Row,Col).Obj:=Pointer(1);  //设置为1，表示已经设置过行高
      end;
      //else
      begin
        //改变颜色输出文字       TFont
        S:=AnyGrid.Cells(Row,Col).Text;
        Canvas.Brush.Style:=bsClear;
        FLines.Clear;
        GetDrawTextLines(Canvas,S,R,FLines);
        OutTexts:=TList.Create;
        HandleKeyColor(FLines,FKeyWord,OutTexts);
        Canvas.Brush.Style:=bsClear;
        T:=R.Top+AnyGrid.Cells(Row,Col).TextTMargin;
        for i:=0 to FLines.Count-1 do
        begin
          S:=FLines[i];
          Index:=1;
          L:=R.Left+AnyGrid.Cells(Row,Col).TextLMargin;
          Len:=Length(S);
          while Index<=Len do
          begin
            Find:=False;
            for j:=0 to OutTexts.Count-1 do
            begin
              P:=OutTexts.Items[j];
              if (P.CanShow) and (P.Index=i) then
              begin
                Text:=Copy(S,Index,P.StartPos-Index);
                Canvas.Font.Color:=clBlack;
                Canvas.TextRect(R,L,T,Text);
                Inc(L,Canvas.TextWidth(Text));
                Text:=Copy(S,P.StartPos,P.EndPos-P.StartPos+1);
                Canvas.Font.Color:=P.FontColor;
                if P.BackColor<>clWhite then
                begin
                  Canvas.Brush.Style:=bsSolid;
                  Canvas.Brush.Color:=P.BackColor;
                  Canvas.FillRect(Rect(L,T,L+Canvas.TextWidth(Text),T+Canvas.TextHeight(Text)));
                  Canvas.Brush.Style:=bsClear;
                end;
                Canvas.TextRect(R,L,T,Text);
                Inc(L,Canvas.TextWidth(Text));
                Index:=P.EndPos+1;
                Dispose(P);
                OutTexts.Delete(j);
                Find:=True;
                Break;
              end;
            end;
            if not Find then
            begin
              Canvas.Font.Color:=clBlack;
              Canvas.TextRect(R,L,T,Copy(S,Index,MaxInt));
              Break;
            end;
          end;
          Inc(T,Canvas.TextHeight(S));
        end;
        OutTexts.Free;
        Handled:=True;
      end;
    end;
  end
  else
    Handled:=True;
end;

procedure TfrmNormal.FormDestroy(Sender: TObject);
begin
  inherited;
  FLines.Free;
  FADOQuery.Free;
end;

procedure TfrmNormal.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if FKeyWord<>Edit1.Text then
    begin
      FKeyWord:=Edit1.Text;
      AnyGrid.Refresh;
    end;
  end;
end;

procedure TfrmNormal.btnPrintClick(Sender: TObject);
var
  i,H:integer;
begin
  AnyGrid.BeginUpdate;
  Screen.Cursor:=crAppStart;
  try
    for i:=0 to AnyGrid.Rows-1 do
    begin
      if (integer(AnyGrid.Cells(i,1).Obj)=0) then  //等于0表示还没有计算过行高
      begin
        H:=AnyGrid.GetRowAutoHeight(i,1,AnyGrid.Cells(i,1).Text);
        if H<>AnyGrid.RowHeight[i] then
          AnyGrid.RowHeight[i]:=H;
        AnyGrid.Cells(i,1).Obj:=Pointer(1);  //设置为1，表示已经设置过行高
      end;
    end;
  finally
    AnyGrid.EndUpdate;
    Screen.Cursor:=crDefault;
  end;
  Report.ShowReport;
end;

end.
