您可以将空框架加载到 WebBrowser1
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>SVG Frame</title>
</head>
<body>
<svg>
</svg>
</body>
</html>
并通过设置 svg 标签的 outerHTML 来添加 SVG 代码,如下所示:
var
Elem: IHTMLElement;
..
Elem := GetElementByTag(WebBrowser1.Document, 'svg');
Elem.outerHTML := slSVG.Text;
函数 GetElementByTag 应定义如下:
function GetElementByTag(const Doc: IDispatch; const TagName: string): IHTMLElement;
var
Document: IHTMLDocument2; // IHTMLDocument2 interface of Doc
Body: IHTMLElement2; // document body element
Tags: IHTMLElementCollection; // all tags in document body
Tag: IHTMLElement; // a tag in document body
I: Integer; // loops thru tags in document body
begin
Result := nil;
// Check for valid document: require IHTMLDocument2 interface to it
if not Supports(Doc, IHTMLDocument2, Document) then
raise Exception.Create('Invalid HTML document');
// Check for valid body element: require IHTMLElement2 interface to it
if not Supports(Document.body, IHTMLElement2, Body) then
raise Exception.Create('Can''t find <body> element');
// Get all tags in body element ('*' => any tag name)
if Tags.length > 0 then
Result := Tags.item(0, EmptyParam) as IHTMLElement
else
raise Exception.Create('no <svg> element found!');
end;
您需要将单元mshtml添加到uses语句。
一个示例项目(Delphi 10.3.3)可以在这里下载