Ver Mensaje Individual
  #2  
Antiguo 05-06-2007
Avatar de ariefez
ariefez ariefez is offline
Miembro
 
Registrado: sep 2005
Ubicación: Perú - Lima
Posts: 63
Reputación: 19
ariefez Va por buen camino
Hola... Espero no te moleste pero cambie un poquito tu codigo. No lo he probado mucho pero ahi tienes la idea

Código Delphi [-]
 unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEditSQLConsulta: TRichEdit;
    procedure RichEditSQLConsultaChange(Sender: TObject);
  private
    procedure SetTextFormat(SelStart, SelLength: Integer;
      Color: TColor; Style: TFontStyles);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const
  //Palabras reservadas del lenguaje SQL
  SQL_RESERVED_WORDS =
    '|SELECT|DISTINC|TOP|PERCENT|AS|FROM|WHERE|LIKE|BETWEEN|AND|OR|LEFT|RIGHT|ORDER|GROUP|BY|';

  //Caracter q separa cada palabra
  SQL_SEPARATOR_CHAR = [' ', #13, #10];
  
  procedure TForm1.SetTextFormat(SelStart, SelLength: Integer;
    Color: TColor; Style: TFontStyles);
  begin
    RichEditSQLConsulta.SelStart := SelStart;
    RichEditSQLConsulta.SelLength := SelLength;
    RichEditSQLConsulta.SelAttributes.Color := Color;
    RichEditSQLConsulta.SelAttributes.Style := Style;
  end;


procedure TForm1.RichEditSQLConsultaChange(Sender: TObject);
var
  TmpSelStart: Integer;
  P, SelStart, SelFinish, SelLength: Integer;
  WordIn: string;
  I: Integer;
begin
  TmpSelStart := RichEditSQLConsulta.SelStart;

  { Busco un caracter separador a partir de SelStart - Hacia atras) }
  SelStart := 0; //Valor por defecto, si SelStart es el inicio
  for I := RichEditSQLConsulta.SelStart downto 1 do
    if RichEditSQLConsulta.Text[i] in SQL_SEPARATOR_CHAR then
    begin // Si lo encuentro almaceno la posicion y termino el bucle
      SelStart := I;
      Break;
    end;

  { Busco un caracter separador a partir de SelStart - Hacia adelante}
  SelFinish := RichEditSQLConsulta.SelStart; //Valor por defecto, si SelStart es el final
  for I := RichEditSQLConsulta.SelStart + 1 to RichEditSQLConsulta.GetTextLen do
    if RichEditSQLConsulta.Text[i] in SQL_SEPARATOR_CHAR then
      Break // Si lo encuentro termino el bucle
    else
      SelFinish := I; // Sino almaceno la posicion

  { Longitud de la palabra encontrada }
  SelLength := SelFinish - (SelStart + 1) + 1; // (SelStart + 1) Es xq SelStart inicia de 0
  { Palabra encontrada }
  WordIn := Copy(RichEditSQLConsulta.Text, SelStart + 1, SelLength);
  { Compruebo si la palabra es reservada}
  P := Pos('|' + UpperCase(WordIn) + '|', SQL_RESERVED_WORDS);
  if 0 < P then
    SetTextFormat(SelStart, SelLength, clBlue, [fsBold]) // Cambio el formato
  else
    SetTextFormat(SelStart, SelLength, clWIndowText, []);

  RichEditSQLConsulta.SelStart := TmpSelStart;
end;

end.

Me olvidaba sobre controlar el portapapeles (esta ultima parte no la probe pero ahi ta como deberia de implementarse)

Código Delphi [-]

...

  private
    procedure WMDrawClipboard (var message : TMessage); message WM_DRAWCLIPBOARD;
    procedure WMChangeCBCHain (var message : TMessage); message WM_CHANGECBCHAIN;

...


var
  Form1: TForm1;

  hClipboardViewer : THandle;

...

  procedure TForm1.WMDrawClipboard (var message : TMessage);
  begin
    message.Result := SendMessage(WM_DRAWCLIPBOARD, hClipboardViewer, 0, 0);
    {Esto se ejecutará cuando haya un cambio en el contenido del portapapeles}
    if Clipboard.HasFormat(CF_TEXT) then
    begin
      { Solo quedaria dale el formato a Clipboard.AsText y despues insertarlo 
        en la posicion SelStart del RichEditSQLConsulta, teniendo cuidado a la hora 
        de la insercion }
    end;
  end;

  procedure TForm1.WMChangeCBCHain (var message : TMessage);
  begin
    if message.wParam = Integer(hClipboardViewer) then
    begin
      hClipboardViewer := message.lParam;
      message.Result := 0;
    end else
    begin
      message.Result := SendMessage(hClipboardViewer, WM_CHANGECBCHAIN,
        message.wParam, message.lParam);
    end;
  end;

Última edición por ariefez fecha: 05-06-2007 a las 05:36:37.
Responder Con Cita