Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 将样式插入TWebBrowser_Html_Delphi_Twebbrowser_Innertext - Fatal编程技术网

Html 将样式插入TWebBrowser

Html 将样式插入TWebBrowser,html,delphi,twebbrowser,innertext,Html,Delphi,Twebbrowser,Innertext,我使用TWebBrowser作为用户的编辑器GUI。我希望能够在文档中插入web控件。一个简单的例子是复选框。(如果需要,我可以详细说明原因)。当我第一次组装HTML文档(及其样式和脚本部分)并将其以块形式传递给TWebBrowser时,所有这些都起到了作用。但现在我希望能够将我的元素插入到现有文档中 我有下面的代码,但它会导致和OLE错误(请参见代码中的注释): 任何人都可以帮助您了解如何在TWebBrowser中现有HTML文档的部分设置样式吗?如果您有有效的IHTMLDocument2,则

我使用TWebBrowser作为用户的编辑器GUI。我希望能够在文档中插入web控件。一个简单的例子是复选框。(如果需要,我可以详细说明原因)。当我第一次组装HTML文档(及其样式和脚本部分)并将其以块形式传递给TWebBrowser时,所有这些都起到了作用。但现在我希望能够将我的元素插入到现有文档中

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


任何人都可以帮助您了解如何在TWebBrowser中现有HTML文档的
部分设置样式吗?

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


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

根据@Zamrony p.Juhara的指导,我想出了以下代码。我张贴的情况下,它可以帮助任何人在未来

procedure THtmlObj.AddStylesToExistingStyleSheet(StyleSheet: IHTMLStyleSheet; SelectorSL, CSSLineSL : TStringList);
//NOTE: There must be a 1:1 correlation between SelectorSL and CSSLineSL
//  The first SL will contain the selector text
//  the second SL will contain all the CSS in one line (divided by ";"'s)
var
  SLIdx, RuleIdx, p: integer;
  SelectorText, CSSText, OneCSSEntry : string;
begin
  if not assigned(StyleSheet) then begin
    raise Exception.Create('Invalid StyleSheet');
  end;
  for SLIdx := 0 to SelectorSL.Count - 1 do begin
    SelectorText := SelectorSL.Strings[SLIdx];
    if SlIdx > (CSSLineSL.Count - 1) then break;
    CSSText := CSSLineSL.Strings[SLIdx];
    while CSSText <> '' do begin
      p := Pos(';', CSSText);
      if p > 0 then begin
        OneCSSEntry := MidStr(CSSText, 1, p);
        CSSText := MidStr(CSSText, p+1, Length(CSSText));
      end else begin
        OneCSSEntry := CSSText;
        CSSText := '';
      end;
      RuleIdx := StyleSheet.Rules.length;
      StyleSheet.addRule(SelectorText, OneCSSEntry, RuleIdx);
    end;
  end;
end;


function THtmlObj.AddStyles(SelectorSL, CSSLineSL : TStringList) :     IHTMLStyleSheet;
//NOTE: There must be a 1:1 correlation between SelectorSL and CSSLineSL
//  The first SL will contain the selector text
//  the second SL will contain all the CSS in one line (divided by ";"'s)
var
  Document:      IHTMLDocument2;              // IHTMLDocument2 interface of Doc
  StyleSheets:   IHTMLStyleSheetsCollection;  // document's style sheets
  StyleSheet:    IHTMLStyleSheet;             // reference to a style sheet
  OVStyleSheet:  OleVariant;                  // variant ref to style sheet
  Idx:           integer;
begin
  Result := nil;
  if not Supports(Doc, IHTMLDocument2, Document) then begin
    raise Exception.Create('Invalid HTML document');
  end;
  StyleSheets := Document.styleSheets;
  Idx := Document.StyleSheets.length;
  OVStyleSheet := Document.createStyleSheet('',Idx);
  if not VarSupports(OVStyleSheet, IHTMLStyleSheet, StyleSheet) then begin
    raise Exception.Create('Unable to create valid style sheet');
  end;
  Result := StyleSheet;
  AddStylesToExistingStyleSheet(StyleSheet, SelectorSL, CSSLineSL);
end; //AddStyles
procedure THtmlObj.AddStylesToExistingStyleSheet(样式表:IHTMLStyleSheet;SelectorSL,CSSLineSL:TStringList);
//注意:选择器SL和CSSLineSL之间必须有1:1的相关性
//第一个SL将包含选择器文本
//第二个SL将在一行中包含所有CSS(除以“;”)
变量
slidex,RuleIdx,p:整数;
选择OrText、CSSText、OneCSSEntry:string;
开始
如果未指定(样式表),则开始
引发异常。创建('Invalid StyleSheet');
结束;
对于SLIDEX:=0选择SLID.Count-1开始
SelectorText:=SelectorSL.Strings[slidex];
如果slidex>(CSSLineSL.Count-1),则中断;
CSSText:=CSSLineSL.Strings[slidex];
而CSSText“”确实开始
p:=Pos(“;”,CSSText);
如果p>0,则开始
OneCSSEntry:=MidStr(CSSText,1,p);
CSSText:=MidStr(CSSText,p+1,长度(CSSText));
结束,否则开始
OneCSSEntry:=CSSText;
CSSText:='';
结束;
RuleIdx:=StyleSheet.Rules.length;
addRule(SelectorText、OneCSSEntry、RuleIdx);
结束;
结束;
结束;
函数THtmlObj.AddStyles(SelectorSL,CSSLineSL:TStringList):IHTMLStyleSheet;
//注意:选择器SL和CSSLineSL之间必须有1:1的相关性
//第一个SL将包含选择器文本
//第二个SL将在一行中包含所有CSS(除以“;”)
变量
文件:IHTMLDocument2;//文档的IHTMLDocument2接口
样式表:IHTMLStyleSheetsCollection;//文档的样式表
样式表:IHTMLStyleSheet;//对样式表的引用
OVStyleSheet:OleVariant;//样式表的变体引用
Idx:整数;
开始
结果:=无;
如果不支持(文档、IHTMLDocument2、文档),则开始
引发异常。创建('无效HTML文档');
结束;
样式表:=Document.StyleSheets;
Idx:=Document.StyleSheets.length;
OVStyleSheet:=Document.createStyleSheet(“”,Idx);
如果不支持(OVStyleSheet、IHTMLStyleSheet、StyleSheet),则开始
引发异常。创建('无法创建有效的样式表');
结束;
结果:=样式表;
将样式添加到现有样式表(样式表、选择器、CSSLineSL);
结束//AddStyles

我请了一位德尔福专家来帮助我使用chromium embedded。他告诉我,如果浏览器在主窗体上,效果会很好。但是,当它在中学的形式,它不会工作的权利。为什么不用Javascript来做呢。。。我有一个Delphi应用程序,我想在编辑区(TWebBrowser)插入一个模板。目前有一个模板引擎,它弹出一个框,显示模板,完成后折叠为插入文档的文本。我正试图将该模板直接嵌入编辑区域。HTML文档不知道在插入过程中需要什么,我不认为Javascript可以做到。那么你就不理解Javascript。花点时间在它上面。您可以将任何对象插入DOM,并通过JavaScript管理这些对象、它们的可见性和位置。您可以在运行时请求javascript并加载html片段,然后将其插入页面。查看jQuery。如果您希望用户能够与生成的HTML交互,那么无论如何您都需要一些javascript知识。最后,我想你会遇到麻烦,除非你学会了如何在父文档中执行的javascript块中做到这一点。@WarrenP谢谢你的帖子。我知道javascript可以操纵DOM。但是我觉得我的设置和你想的不一样。此浏览器不与internet交互。它只呈现Delphi应用程序传递给它的内容。如果javascript要操作DOM,那么Delphi代码必须首先设置javascript代码并将其传入。事件链起源于Delphi代码。所以我可以直接设置样式,或者创建javascript来实现。这两个操作都是从Delphi代码开始的。我不建议它与internet交互。但它应该与您正在运行的本地服务器进行交互,我认为使用JavaScript+JQUERY之类的layer+load from page比使用IShellDocView/IHTMLStyleSheet接口(设计于1995年)并尝试以这种方式执行所有操作要好。这似乎正是我想要的。谢谢美好的有时我会发布类似的内容,甚至一年后,我会以这种方式找到自己的代码并再次使用。
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;
// *********************************************************************//
// 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;
procedure THtmlObj.AddStylesToExistingStyleSheet(StyleSheet: IHTMLStyleSheet; SelectorSL, CSSLineSL : TStringList);
//NOTE: There must be a 1:1 correlation between SelectorSL and CSSLineSL
//  The first SL will contain the selector text
//  the second SL will contain all the CSS in one line (divided by ";"'s)
var
  SLIdx, RuleIdx, p: integer;
  SelectorText, CSSText, OneCSSEntry : string;
begin
  if not assigned(StyleSheet) then begin
    raise Exception.Create('Invalid StyleSheet');
  end;
  for SLIdx := 0 to SelectorSL.Count - 1 do begin
    SelectorText := SelectorSL.Strings[SLIdx];
    if SlIdx > (CSSLineSL.Count - 1) then break;
    CSSText := CSSLineSL.Strings[SLIdx];
    while CSSText <> '' do begin
      p := Pos(';', CSSText);
      if p > 0 then begin
        OneCSSEntry := MidStr(CSSText, 1, p);
        CSSText := MidStr(CSSText, p+1, Length(CSSText));
      end else begin
        OneCSSEntry := CSSText;
        CSSText := '';
      end;
      RuleIdx := StyleSheet.Rules.length;
      StyleSheet.addRule(SelectorText, OneCSSEntry, RuleIdx);
    end;
  end;
end;


function THtmlObj.AddStyles(SelectorSL, CSSLineSL : TStringList) :     IHTMLStyleSheet;
//NOTE: There must be a 1:1 correlation between SelectorSL and CSSLineSL
//  The first SL will contain the selector text
//  the second SL will contain all the CSS in one line (divided by ";"'s)
var
  Document:      IHTMLDocument2;              // IHTMLDocument2 interface of Doc
  StyleSheets:   IHTMLStyleSheetsCollection;  // document's style sheets
  StyleSheet:    IHTMLStyleSheet;             // reference to a style sheet
  OVStyleSheet:  OleVariant;                  // variant ref to style sheet
  Idx:           integer;
begin
  Result := nil;
  if not Supports(Doc, IHTMLDocument2, Document) then begin
    raise Exception.Create('Invalid HTML document');
  end;
  StyleSheets := Document.styleSheets;
  Idx := Document.StyleSheets.length;
  OVStyleSheet := Document.createStyleSheet('',Idx);
  if not VarSupports(OVStyleSheet, IHTMLStyleSheet, StyleSheet) then begin
    raise Exception.Create('Unable to create valid style sheet');
  end;
  Result := StyleSheet;
  AddStylesToExistingStyleSheet(StyleSheet, SelectorSL, CSSLineSL);
end; //AddStyles