gpt4 book ai didi

html - 将样式插入 TWebBrowser

转载 作者:行者123 更新时间:2023-12-03 03:21:08 30 4
gpt4 key购买 nike

我使用 TWebBrowser 作为用户的编辑器 GUI。我希望能够将 Web 控件插入到文档中。一个简单的例子是复选框。 (如果需要的话我可以详细说明原因)。当我第一次组装 HTML 文档(及其 STYLE 和 SCRIPTS 部分)然后将其整体传递给 TWebBrowser 时,所有这些工作都完成了。但现在我希望能够将我的元素插入到现有文档中。

我有下面的代码,但它导致了 OLE 错误(请参阅代码中的注释):

procedure THTMLTemplateDocument.EnsureStylesInWebDOM;
var StyleBlock : IHTMLElement;
StyleText: string;
begin
StyleBlock := FWebBrowser.GetDocStyle;
if not assigned(StyleBlock) then
raise Exception.Create('Unable to access <STYLE> block in web document');
StyleText := FCumulativeStyleCodes.Text;
StyleBlock.InnerText := StyleText; <--- generates "OLE ERROR 800A0258"
end;

上述代码调用的函数如下:

function THtmlObj.GetDocStyle: IHTMLElement;
//Return pointer to <STYLE> block, creating this if it was not already present.
var
Document: IHTMLDocument2; // IHTMLDocument2 interface of Doc
Elements: IHTMLElementCollection; // all tags in document body
AElement: IHTMLElement; // a tag in document body
Style, Head: IHTMLElement;
I: Integer; // loops thru Elements in document body
begin
Result := nil;
if not Supports(Doc, IHTMLDocument2, Document) then
raise Exception.Create('Invalid HTML document');
Elements := Document.all;
for I := 0 to Pred(Elements.length) do begin
AElement := Elements.item(I, EmptyParam) as IHTMLElement;
if UpperCase(AElement.tagName) <> 'STYLE' then continue;
result := AElement;
break;
end;
if not assigned(Result) then begin
Head := GetDocHead;
if assigned(Head) then begin
Style := Document.CreateElement('STYLE');
(Head as IHTMLDOMNode).AppendChild(Style as IHTMLDOMNode);
Result := Style;
end;
end;
end;

function THtmlObj.GetDocHead: IHTMLElement;
//Return pointer to <HEAD> block, creating this if it was not already present.
var
Document: IHTMLDocument2; // IHTMLDocument2 interface of Doc
Elements: IHTMLElementCollection; // all tags in document body
AElement: IHTMLElement; // a tag in document body
Body: IHTMLElement2; // document body element
Head: IHTMLElement;
I: Integer; // loops thru Elements in document body
begin
Result := nil;
if not Supports(Doc, IHTMLDocument2, Document) then
raise Exception.Create('Invalid HTML document');
if not Supports(Document.body, IHTMLElement2, Body) then
raise Exception.Create('Can''t find <body> element');
Elements := Document.all;
for I := 0 to Pred(Elements.length) do begin
AElement := Elements.item(I, EmptyParam) as IHTMLElement;
if UpperCase(AElement.tagName) <> 'HEAD' then continue;
Result := AElement;
break;
end;
if not assigned(Result) then begin
Head := Document.CreateElement('HEAD');
(Body as IHTMLDOMNode).insertBefore(Head as IHTMLDOMNode, Body as IHTMLDOMNode);
//now look for it again
Elements := Document.all;
for I := 0 to Pred(Elements.length) do begin
AElement := Elements.item(I, EmptyParam) as IHTMLElement;
if UpperCase(AElement.tagName) <> 'HEAD' then continue;
Result := AElement;
break;
end;
end;
end;

当我运行这个时,StyleText =

'.selected {'#$D#$A' 字体粗细 : 粗体;'#$D#$A'//背景颜色 : 黄色;'#$D#$A'}'#$D#$A' .unselected {'#$D#$A' 字体粗细 : 正常;'#$D#$A'//背景颜色 : 白色;'#$D#$A'}'#$D#$A#$D#$A

但是我尝试将 StyleText 设置为像“hello”这样的简单内容,但它仍然崩溃了。

Google 搜索“OLE ERROR 800A0258”显示其他几个人也遇到过类似问题,例如 herehere -- 后来的用户似乎表明他通过使用 .OuterHTML 解决了问题,但我尝试了此操作,但生成了类似的错误。 This线程似乎表明 .InnerText 是只读的。但在接口(interface)声明中(见下文),似乎有一个设置方法(即不是只读)。

// *********************************************************************//
// Interface: IHTMLElement
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {3050F1FF-98B5-11CF-BB82-00AA00BDCE0B}
// *********************************************************************//
IHTMLElement = interface(IDispatch)
['{3050F1FF-98B5-11CF-BB82-00AA00BDCE0B}']
...
procedure Set_innerHTML(const p: WideString); safecall;
function Get_innerHTML: WideString; safecall;
procedure Set_innerText(const p: WideString); safecall;
function Get_innerText: WideString; safecall;
procedure Set_outerHTML(const p: WideString); safecall;
function Get_outerHTML: WideString; safecall;
procedure Set_outerText(const p: WideString); safecall;
function Get_outerText: WideString; safecall;
...
property innerHTML: WideString read Get_innerHTML write Set_innerHTML;
property innerText: WideString read Get_innerText write Set_innerText;
property outerHTML: WideString read Get_outerHTML write Set_outerHTML;
property outerText: WideString read Get_outerText write Set_outerText;
...
end;

任何人都可以帮忙弄清楚如何在<STYLE>中设置样式吗? TWebBrowser 中现有 HTML 文档的部分?

最佳答案

如果您有有效的 IHTMLDocument2,那么您可以调用它的 createStyleSheet() 。它将返回 IHTMLStyleSheet 实例。您可以使用其 cssText 属性来设置样式。

确保考虑文档的字符编码。

关于html - 将样式插入 TWebBrowser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35904836/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com