Buscar

Convertendo RichEdit em HTML sem precisar de componentes

Código

/*

Função básica, mas muito útil para quem quer enviar e-mail formatado com RichEdit, entre muitas outras utilidades. Esta função converte o conteúdo de um RichEdit em HTML.

--------------------------------------------------
*/

function TfrmPrincipal.RICH_HTML(RICH: TRichEdit): string;
var
  I,J,MAX_ARRAY: integer;
  HTML: string;
  F,FO: array [0..5] of string;
  STR,LGT: integer;
  SIZE: integer;
  COR : string;
begin
  HTML := '';
  STR := RICH.SelStart;
  LGT := RICH.SelLength;
  MAX_ARRAY := 5;

  for J := 0 to MAX_ARRAY do
  begin
    F[J] := '';
    FO[J] := '';
  end;

  for I := 0 to length(RICH.text) do
  begin
    RICH.SelStart := I;
    RICH.SelLength := 1;

    for J := 0 to MAX_ARRAY do
      FO[J] := F[J];

    if (RICH.SelAttributes.Style - [fsItalic] - [fsUnderline] - [fsStrikeOut] = [fsBold]) then
      F[0] := '<b>'
    else
      F[0] := '</b>';

    if (RICH.SelAttributes.Style - [fsItalic] - [fsBold] - [fsStrikeOut] = [fsUnderline]) then
      F[1] := '<u>'
    else
      F[1] := '</u>';

    if (RICH.SelAttributes.Style - [fsUnderline] - [fsBold] - [fsStrikeOut] = [fsItalic]) then
      F[2] := '<i>'
    else
      F[2] := '</i>';

    if RICH.SelAttributes.size < 10 then
      SIZE := 1
    else if RICH.SelAttributes.size < 12 then
      SIZE := 2
    else if RICH.SelAttributes.size < 14 then
      SIZE := 3
    else if RICH.SelAttributes.size < 18 then
      SIZE := 4
    else if RICH.SelAttributes.size < 22 then
      SIZE := 5
    else if RICH.SelAttributes.size < 32 then
      SIZE := 6
    else
      SIZE := 7;
    COR := ColorToString(RICH.SelAttributes.Color);
    if (COR = 'clWindowText') or (COR = 'clBlack') then
      COR := '#000000'
    else if COR = 'clWite' then
      COR := '#FFFFFF'
    else if COR = 'clAqua' then
      COR := '#00FFFF'
    else if COR = 'clFuchsia' then
      COR := '#FF00FF'
    else if COR = 'clBlue' then
      COR := '#0000FF'
    else if COR = 'clYellow' then
      COR := '#FFFF00'
    else if COR = 'clLime' then
      COR := '#00FF00'
    else if COR = 'clRed' then
      COR := '#FF0000'
    else if COR = 'clSilver' then
      COR := '#C0C0C0'
    else if COR = 'clGray' then
      COR := '#808080'
    else if COR = 'clTeal' then
      COR := '#008080'
    else if COR = 'clPurple' then
      COR := '#800080'
    else if COR = 'clNavy' then
      COR := '#000080'
    else if COR = 'clOlive' then
      COR := '#808000'
    else if COR = 'clGreen' then
      COR := '#008000'
    else if COR = 'clMaroon' then
      COR := '#800000'
    else if copy(COR,1,1) = '$' then
      COR := '#'+copy(COR,length(COR)-1,2)
                +copy(COR,length(COR)-3,2)
                +copy(COR,length(COR)-5,2)
    else
      COR := '#000000';
    F[3] := '</font><font face="'+RICH.SelAttributes.Name+'" size='+inttostr(SIZE)+' color="'+COR+'">';

    if RICH.Paragraph.Alignment = taCenter then
      F[4] := '<center>'
    else
      F[4] := '</center>';

    if RICH.Paragraph.Alignment = taRightJustify then
      F[5] := '<div align="right">'
    else
      F[5] := '</div>';

    for J := 0 to MAX_ARRAY do
      if FO[J] <> F[J] then
        HTML := HTML + F[J];

    if copy(RICH.text,I+1,1) = #13 then
      HTML := HTML + '<br>';

    HTML := HTML + copy(RICH.text,I+1,1);
  end;
  RICH.SelStart := STR;
  RICH.SelLength := LGT;

  result := HTML;
end;

/*

--------------------------------------------------

Segue um exemplo de como ultilizar a função:

Memo1.text := RICH_HTML(RichEdit1);

*/

Publicidade

Vote na dica




Quantidade de votos: 2 votos
Aceitação: 20%


Detalhes da dica

Categoria: Arquivos
Adicionada dia: 29/10/09
Por: Mayco Willian
Visualizada: 12797 vezes

Planeta Delphi - Tudo sobre programação Delphi Planeta Delphi - www.planetadelphi.com.br - Todos os direitos reservados | Copyright 2001-2009