Einzelnen Beitrag anzeigen

JDommi

Registriert seit: 20. Sep 2007
9 Beiträge
 
Delphi 7 Enterprise
 
#1

Text in StringGrid suchen und markieren

  Alt 8. Jun 2019, 22:46
Hallo zusammen!

Ich brauche nach langer Zeit mal wieder Hilfe. Diesmal in Sachen StringGrid unter (Uralt-)Delphi 7 Enterprise.

Voraussetzung: Ich durchsuche eine bestimmte Spalte eines StringGrids nach einer Textphrase (kann auch mehrmals in der gleichen Zelle vorhanden sein) und will jedes Vorkommen markieren. Wordwrap für die Spalte ist aktiviert.

Was bisher funktioniert ist das Markieren jedes Fundes in der ersten Zeile. Aber wie bekomme ich das für alle folgenden Zeilen ebenfalls hin? Erschwerend kommt hinzu, dass ich einen manuellen Zeilenvorschub mit Hilfe von ^^ realisiere.

Ich hoffe, dass mir jemand hierbei helfen kann.

Mein Code lautet bisher:
Delphi-Quellcode:
  if (ARow > 0) and (ACol = 2) and (Edit2.Text<>'') and (Edit2.Text<>'[Text suchen]') then
  begin
    str:=StringGrid1.Cells[ACol,ARow];
    anz:=countinstring(str,Edit2.Text);
    For x := 1 to anz do
    begin
      sErg[x] := Split(str, Edit2.Text, x);
      StringGrid1.Canvas.Font.Color := clBlack;
      StringGrid1.Canvas.TextOut(Rect.Left,Rect.Top+t2,sErg[x]);
      t := t+StringGrid1.Canvas.TextWidth(sErg[x]);
//Dieser Teil funktioniert nicht
// while t > Rect.Right-Rect.Left do
// begin
// t := t-Rect.Right-Rect.Left;
// t2 := t2 + StringGrid1.Canvas.TextHeight(Edit2.Text);
// end;
      StringGrid1.Canvas.Font.Color := clRed;
      StringGrid1.Canvas.TextOut(Rect.Left+t,Rect.Top+t2,Edit2.Text);
      t := t+(StringGrid1.Canvas.TextWidth(Edit2.Text)*x);
    end;
    StringGrid1.Canvas.Font.Color := clBlack;
    StringGrid1.Canvas.TextOut(Rect.Left,Rect.Top,sErg[anz+1]);
  end;
Und hier die komplette Prozedur:
Delphi-Quellcode:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  Picture: TPicture;
  OutRect: TRect;
  PictWidth, PictHeight, x, y, t, t2: Integer;
  s, str, tmp: string;
  anz: integer;
  sErg: array[1..50] of string;
begin
  StringGrid1.ColWidths[0] := 212;

  StringGrid1.Canvas.Font.Color := clBlack;

  if ACol > 2 then
    exit;
  if StringGrid1.RowHeights[ARow] = -1 then
    exit;

  OutRect := Rect;

  if (ARow = edRow) and (ARow > 0) then
  begin
    StringGrid1.Canvas.Brush.Color := $77FFFF;
    StringGrid1.Canvas.FillRect(Rect);
  end
  else
  begin
    StringGrid1.Canvas.Brush.Color := clWindow;
    StringGrid1.Canvas.FillRect(Rect);
  end;

  StringGrid1.Canvas.Font.Style := [fsbold];

  if (ACol = 1) and (ARow > 0) then
  begin
    StringGrid1.Canvas.Font.Size := 14;
    StringGrid1.Canvas.Font.Style := [fsbold];
  end
  else
  begin
    StringGrid1.Canvas.Font.Size := 10;
    StringGrid1.Canvas.Font.Style := [fsbold];
  end;

  if ARow = 0 then
  begin
    StringGrid1.Canvas.Brush.Color := clBtnFace;
    StringGrid1.Canvas.FillRect(Rect);
    StringGrid1.Canvas.Font.Size := 10;
    StringGrid1.Canvas.Font.Style := [fsbold];
    InflateRect(Rect, -2, -2); // sorgt für einen 2 Pixel Abstand zum Rand
  end;

  StringGrid1.Brush.Style := bsClear;
  if ARow > 0 then
    InflateRect(Rect, -6, -6); // sorgt für einen 6 Pixel Abstand zum Rand

  str := StringGrid1.Cells[aCol, ARow];
  str := StringReplace(str, '^^', #13#10, [rfReplaceAll]);
  if (ARow = 0) or (ACol = 1) then
    DrawText(StringGrid1.Canvas.Handle, PChar(str), -1, Rect, dt_singleline or
      dt_vcenter or dt_center or dt_wordbreak)
  else
  begin
    if (ARow>0) and (ACol=2) then
    begin
      //Zeilenumbruch1Click(ARow);
      //str:=trim(Memo5.Text);
    end;
    DrawText(StringGrid1.Canvas.Handle, PChar(str), -1, Rect, dt_wordbreak);
  end;

{  // Version 1: Erstes Vorkommen und nur in Zeile 1
  if (ARow > 0) and (ACol = 2) and (Edit2.Text<>'') and (Edit2.Text<>'[Text suchen]') then
  begin
    str:=StringGrid1.Cells[ACol,ARow];
    x:=pos(Edit2.Text,str);
    s := Copy(str,1,x-1);
    StringGrid1.Canvas.Font.Color := clBlack;
    StringGrid1.Canvas.TextOut(Rect.Left,Rect.Top,s);
    t := StringGrid1.Canvas.TextWidth(s);
    StringGrid1.Canvas.Font.Color := clRed;
    s := Copy(StringGrid1.Cells[ACol,ARow],x,length(Edit2.Text));
    StringGrid1.Canvas.TextOut(Rect.Left+t,Rect.Top,s);
    s := Copy(str,x+length(Edit2.Text),MAXINT);
    t := StringGrid1.Canvas.TextWidth(s);
    StringGrid1.Canvas.Font.Color := clBlack;
    StringGrid1.Canvas.TextOut(Rect.Left+t,Rect.Top,s);
  end; }

  //Version 2: Jedes Vorkommen, allerdings nur in Zeile 1
  if (ARow > 0) and (ACol = 2) and (Edit2.Text<>'') and (Edit2.Text<>'[Text suchen]') then
  begin
    str:=StringGrid1.Cells[ACol,ARow];
    anz:=countinstring(str,Edit2.Text);
    For x := 1 to anz do
    begin
      sErg[x] := Split(str, Edit2.Text, x);
      StringGrid1.Canvas.Font.Color := clBlack;
      StringGrid1.Canvas.TextOut(Rect.Left,Rect.Top+t2,sErg[x]);
      t := t+StringGrid1.Canvas.TextWidth(sErg[x]);
// while t > Rect.Right-Rect.Left do
// begin
// t := t-Rect.Right-Rect.Left;
// t2 := t2 + StringGrid1.Canvas.TextHeight(Edit2.Text);
// end;
      StringGrid1.Canvas.Font.Color := clRed;
      StringGrid1.Canvas.TextOut(Rect.Left+t,Rect.Top+t2,Edit2.Text);
      t := t+(StringGrid1.Canvas.TextWidth(Edit2.Text)*x);
    end;
    StringGrid1.Canvas.Font.Color := clBlack;
    StringGrid1.Canvas.TextOut(Rect.Left,Rect.Top,sErg[anz+1]);
  end;

  if (ARow > 0) and (ACol = 0) then
  begin
    // draw the image
    Picture := TPicture.Create;
    try
      tmp := StringGrid1.Cells[1, ARow];
      if fileexists('Bilder/' + tmp + '.jpg') then
        Image1.Picture.LoadFromFile('Bilder/' + tmp + '.jpg')
      else
      begin
        Image1.Picture.Assign(nil);
        exit;
      end;
      Picture.Assign(Image1.Picture);
      PictWidth := Image1.Picture.Width;
      PictHeight := Image1.Picture.Height;

      if PictWidth > PictHeight then
      begin
        OutRect.Right := 210;
        OutRect.Bottom := (210 * PictHeight) div PictWidth;
      end
      else
      begin
        OutRect.Bottom := 210;
        OutRect.Right := (210 * PictWidth) div PictHeight;
      end;

      x := (Rect.Right - Rect.Left - OutRect.Right) div 2;
      y := (Rect.Bottom - Rect.Top - OutRect.Bottom) div 2;
      OutRect.Left := Rect.Left + x;
      OutRect.Right := Rect.Right - x;
      OutRect.Top := Rect.Top + y;
      OutRect.Bottom := Rect.Bottom - y;
      StringGrid1.Canvas.StretchDraw(OutRect, Picture.Graphic);
    finally
      Picture.Free;
    end;
  end;
end;
  Mit Zitat antworten Zitat