我需要为 C++Builder 5 找到基本的 WYSIWYG HTML 编辑器组件,让用户创建一些我将粘贴到现有 HTML 页面模板中的简单文本。只是对创建链接、添加图像、使用标题/粗体/斜体的简单支持。
4 回答
您可以将 TWebBrowser 拖放到表单上并在其上启用设计模式,如下所示:
// Delphi code..
(WebBrowser1.Document as IHTMLDocument2).designMode := 'on';
执行上述行后,页面将是可编辑的。您可以键入额外的文本、删除等。如果您想将选择设置为粗体或插入图像,您将不得不添加一些按钮来对其进行编程。很酷的事情是,您可以从 Delphi(或您的情况下的 C++ 构建器)执行此操作,也可以在页面上添加 javascript 进行编辑。
页面的内容可以从
(WebBrowser.Document as IHTMLDocument2).body.innerHTML;
请记住 (WebBrowser.Document as IHTMLDocument2) 可能为零。
无论如何,我可以想象周围有一些组件可以为您完成所有工作,这可能是比重新发明轮子更好的方法。
我会推荐TRichView,因为它具有世界级的支持和深厚的功能集。虽然它不是真正的“HTML”编辑器,但它确实支持导出到 HTML 的能力,甚至在必要时生成适当的 CSS 样式。我用它来处理我们主要产品的电子邮件部分,效果很好。在内部,存储要么是 RTF(扩展以更好地支持图像),要么是专有格式。有很多简单的编辑器示例可以轻松满足您的需求。
在 C++ Builder 中,它会是这样的:
(wb 是一个 TCppWebBrowser)
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "mshtml.h"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SHDocVw_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnNavigateAndEditClick(TObject *Sender)
{
wb->Navigate((WideString)"www.google.com");
while (wb->Busy)
Application->ProcessMessages();
if (wb->Document)
{
IHTMLDocument2 *html;
wb->Document->QueryInterface<IHTMLDocument2>(&html);
html->put_designMode(L"On");
html->Release();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnInsertImageClick(TObject *Sender)
{
if (wb->Document)
{
IHTMLDocument2 *html;
wb->Document->QueryInterface<IHTMLDocument2>(&html);
VARIANT var;
VARIANT_BOOL receive;
html->execCommand(L"InsertImage",true,var, &receive);
html->Release();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnGetHtmlClick(TObject *Sender)
{
if (wb->Document)
{
IHTMLDocument2 *html;
wb->Document->QueryInterface<IHTMLDocument2>(&html);
IHTMLElement *pElement;
html->get_body(&pElement);
pElement->get_parentElement(&pElement);
wchar_t *tmp;
pElement->get_outerHTML(&tmp);
Memo1->Lines->Text = tmp;
pElement->Release();
html->Release();
}
}
//---------------------------------------------------------------------------
提供一组免费的EmbeddedWebBrowser
组件,其中包含一个编辑设计器组件,您可以将其链接到EmbeddedBrowser
窗口以控制设计模式,并将编辑控件保存到文件、插入链接、图像等...
似乎运作良好!