如何将简单的RichText转换为Delphi中的HTML标签? [英] How to Convert Simple RichText to HTML tags in Delphi?

查看:1113
本文介绍了如何将简单的RichText转换为Delphi中的HTML标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以说在stackOverflow中有很多关于这个问题的讨论,但是大部分都比我所需要的更为复杂,主要用于其他语言。



有一个MySQL远程数据库,其中有一个帮助表,其中包含填写使用此数据库的动态网站的帮助页面的代码。



我决定使一个Delphi应用程序来管理该网站,而不是通过网站本身来实现更高的速度和安全性。



我想放一个 TRichEdit 来帮助文本,并使用简单的东西,如对齐,粗体,斜体和带下划线的样式。我不想使用图片和字体。



如何选择丰富的样式文本并将其转换为HTML 以放入我的BLOB字段在远程数据库中,然后重新转换为富文本,如果我想再次编辑

解决方案

在尝试了许多不具备准确结果的不同解决方案之后,我受到了这种解决方案的启发:将RTF转换为HTML和HTML到RTF



这个想法是 TWebBrowser 控件(在设计/编辑模式下)可以在剪贴板粘贴时正确处理和转换富文本格式。

 使用SHDocVw,MSHTML; 

函数ClipboardToHTML(AParent:TWinControl):WideString;
var
wb:TWebBrowser;

函数WaitDocumentReady:Boolean;
var
StartTime:DWORD;
begin
StartTime:= GetTickCount;
,而wb.ReadyState<> READYSTATE_COMPLETE do
begin
Application.HandleMessage;
如果GetTickCount> = StartTime + 2000 then //超时最大2秒
begin
结果:= False; //超时
退出;
结束
结束
结果:= True;
结束
begin
结果:='';
wb:= TWebBrowser.Create(nil);
try
wb.Silent:= True;
wb.Width:= 0;
wb.Height:= 0;
wb.Visible:= False;
TWinControl(wb).Parent:= AParent;
wb.HandleNeeded;
如果wb.HandleAllocated然后
begin
wb.Navigate('about:blank');
(wb.Document as IHTMLDocument2).designMode:='on';
如果WaitDocumentReady然后
begin
(wb.Document as IHTMLDocument2).execCommand('Paste',False,0);
结果:=(wb.Document as IHTMLDocument2).body.innerHTML;
结束
结束
finally
wb.Free;
结束
结束

procedure TForm1.Button1Click(Sender:TObject);
begin
RichEdit1.SelectAll;
RichEdit1.CopyToClipboard;

ShowMessage(ClipboardToHTML(Self));
结束


You may say that there are lots of discussions about this in stackOverflow, but most of them are more complicated than what I need and mostly for other languages.

I have a MySQL remote database in which I have a "Help" table with the code for filling the help pages of the dynamic web site that uses this database.

I decided to make a Delphi application to manage that website instead of doing by the web site itself for more speed and security.

I want to put a TRichEdit to make that help text and use simple things like alignment, bold, italic and underlined styles. I don't want to use pictures and fonts.

How to pick that rich styled text and convert it to HTML to put to my BLOB field in the remote database and then reconvert to rich text if I want to edit it again?

解决方案

After trying many different solutions which did not gave accurate results, I was inspired by this solution: Convert RTF to HTML and HTML to RTF.

The idea is that TWebBrowser control (in design/edit mode) can handle and convert correctly Rich text format when it was pasted from the clipboard.

uses SHDocVw, MSHTML;

function ClipboardToHTML(AParent: TWinControl): WideString;
var
  wb: TWebBrowser;

  function WaitDocumentReady: Boolean;
  var
    StartTime: DWORD;
  begin
    StartTime := GetTickCount;
    while wb.ReadyState <> READYSTATE_COMPLETE do
    begin
      Application.HandleMessage;
      if GetTickCount >= StartTime + 2000 then // time-out of max 2 sec
      begin
        Result := False; // time-out
        Exit;
      end;
    end;
    Result := True;
  end;
begin
  Result := '';
  wb := TWebBrowser.Create(nil);
  try
    wb.Silent := True;
    wb.Width := 0;
    wb.Height := 0;
    wb.Visible := False;
    TWinControl(wb).Parent := AParent;
    wb.HandleNeeded;
    if wb.HandleAllocated then
    begin
      wb.Navigate('about:blank');
      (wb.Document as IHTMLDocument2).designMode := 'on';
      if WaitDocumentReady then
      begin
        (wb.Document as IHTMLDocument2).execCommand('Paste', False, 0);
        Result := (wb.Document as IHTMLDocument2).body.innerHTML;
      end;
    end;
  finally
    wb.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  RichEdit1.SelectAll;
  RichEdit1.CopyToClipboard;

  ShowMessage(ClipboardToHTML(Self));
end;

这篇关于如何将简单的RichText转换为Delphi中的HTML标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆