Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > OOP
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

 
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 11-12-2015
Avatar de AgustinOrtu
[AgustinOrtu] AgustinOrtu is offline
Miembro Premium
NULL
 
Registrado: ago 2013
Ubicación: Argentina
Posts: 1.858
Poder: 15
AgustinOrtu Es un diamante en brutoAgustinOrtu Es un diamante en brutoAgustinOrtu Es un diamante en brutoAgustinOrtu Es un diamante en bruto
TDBGrid, pintar/negrita texto celda parcial

Saludos

Estoy usando Delphi 2010, y el efecto que quiero lograr es poner en negrita (o colorear, en fin cualquier forma de resaltar texto me vendria bien) parte del contenido de una celda de un TDBGrid; la idea es lanzar una busqueda y resaltar todas los campos en donde coincide el texto buscado

Ejemplo: busco "agustin" entonces el DBGrid deberia mostrar

Código Delphi [-]
Nombre             Direccion                           ...
Agustin    Calle agustincito        ...

Al grid ademas le asigno estas propiedades:

Código Delphi [-]
DefaultDrawing := False 
Options := [dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgRowSelect,
    dgAlwaysShowSelection, dgCancelOnExit, dgTitleClick, dgTitleHotTrack];

Lo que hice fue agregar un manejador al evento OnDrawColumnCell, este es el codigo:

Código Delphi [-]
TForm1.(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
var
  R: TRect;
  LCanvas: TCanvas;
  LFieldValue, LChar: string;
  I: Integer;
begin
  LCanvas := TDBGrid(Sender).Canvas;
  if DataCol in [0 .. 1] then
  begin
    LFieldValue := Column.Field.AsString;

    if not AnsiContainsText(LFieldValue, FSearchString) then
    begin
      LCanvas.Font.Style := LCanvas.Font.Style - [fsBold];
      TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, State);
      Exit;
    end;

    R := Rect;
    for I := 1 to Length(LFieldValue) do
    begin
      LChar := LFieldValue[i];

      // desplazamiento del rectangulo
      if I > 1 then
        R.Left := R.Left + LCanvas.TextWidth(LChar);

      if (Length(FSearchString) >= I) and (AnsiSameText(LChar, FSearchString[i])) then
        LCanvas.Font.Style := LCanvas.Font.Style + [fsBold]
      else
        LCanvas.Font.Style := LCanvas.Font.Style - [fsBold];

      // dibujar cada caracter
      LCanvas.TextRect(R, LChar);
    end;
  end
  else
  begin
    LCanvas.Font.Style := LCanvas.Font.Style - [fsBold];
    TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, State);
  end;
end;

Basicamente voy pintando caracter a caracter, en negrita o no, segun se vaya cumpliendo la coincidencia parcial

Pero me queda "feo" el texto, asi es como sale:



Curiosamente para numeros parece "ir bien", pero el texto sale horrible

Alguien puede arrojar algo del luz?

PD: Otras alternativas de haberlas, encantado de oirlas; por ejemplo, si con otro control VCL es facil lograr el efecto, aunque no sea data-aware, podria escribir la parte de llenar el contenido (un ListView por ejemplo). En lo posible prefiero no usar componentes de terceros
Responder Con Cita
 



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
texto en negrita NEG1414 C++ Builder 2 05-02-2009 16:22:28
Pintar filas TDBGrid ManuelPerez Varios 11 12-08-2008 11:09:47
pintar una celda... eli.vidal Impresión 1 14-11-2005 16:31:27
pintar una celda... eli.vidal Gráficos 1 14-11-2005 16:30:49
Como Pintar Solo la Celda y No Toda la Columna de la Celda de un dbGrid?? AGAG4 Varios 11 15-11-2004 20:53:28


La franja horaria es GMT +2. Ahora son las 05:14:05.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi