\t\t几个WebBrowser相关的函数

2024-02-14 07:18

本文主要是介绍\t\t几个WebBrowser相关的函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


贴几个 TWebBrowser 编程相关的函数。


{ 去掉 TWebBrowser 的边框 }
procedure WB_Set3DBorderStyle(Sender: TWebBrowser; bValue: Boolean);

{ WebBrowser 从内存中读取 HTML 文件}
procedure WebBrowserLoadFromStream(WebBrowser: TWebBrowser; Stream: TStream);
procedure NavigateLoadFromStream(WebBrowser: TWebBrowser;   Stream:   TStream);

{ WebBrowser 从字符中读取 HTML 文件}
procedure WebBrowserLoadFromText(WebBrowser: TWebBrowser; HtmlText: String);


{ WebBrowser 保存成 Html 文件 }
function WebBrowserSaveHTMLCode(WebBrowser: TWebBrowser; const FileName: TFileName):   Boolean;

{ HTML 保存成流 }
procedure SaveDocumentSourceToStream(Document: IDispatch; Stream: TStream);

uses Axctrls, ActiveX, MSHTML, OleCtrls, SHDocVw


{ WB_Set3DBorderStyle }

procedure WB_Set3DBorderStyle(Sender: TWebBrowser; bValue: Boolean);
var
   Document: IHTMLDocument2;
   Element: IHTMLElement;
   StrBorderStyle: string;
begin
   //去掉边框
   try
Document := TWebBrowser(Sender).Document as IHTMLDocument2;
if Assigned(Document) then
begin
   Element := Document.Body;
   if Element <> nil then
   begin
       case BValue of
      False: StrBorderStyle := 'none';
      True: StrBorderStyle := '';
       end;
       Element.Style.BorderStyle := StrBorderStyle;
   end;
end;
   except
//..
   end;
end;


{ WebBrowserLoadFromText }

procedure WebBrowserLoadFromText(WebBrowser: TWebBrowser; HtmlText: String);
var
   v: Variant;
   IDoc: IHTMLDocument2;
begin
   WebBrowser.Navigate('about:blank');
   repeat
Application.ProcessMessages;
Sleep(0);
   until WebBrowser.ReadyState = READYSTATE_COMPLETE;

   IDoc := WebBrowser.Document as IHTMLDocument2;
   try
IDoc.designMode:='on';
while IDoc.readyState<>'complete' do
   Application.ProcessMessages;
v:=VarArrayCreate([0,0],VarVariant);
v[0]:= HtmlText;
IDoc.write(PSafeArray(System.TVarData(v).VArray));
IDoc.designMode:='off';
while IDoc.readyState<>'complete' do
   Application.ProcessMessages;
   finally
IDoc := nil;
   end;
end;

{ NavigateLoadFromStream }

procedure NavigateLoadFromStream(WebBrowser: TWebBrowser;   Stream:   TStream);
begin
   Stream.Seek(0, 0);
   if Assigned(WebBrowser.Document) then
(WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(Stream));
end;

{ WebBrowserLoadFromStream }

procedure WebBrowserLoadFromStream(WebBrowser: TWebBrowser;   Stream:   TStream);
begin
   WebBrowser.Navigate('about:blank');

   repeat
Application.ProcessMessages;
Sleep(0);
   until WebBrowser.ReadyState = READYSTATE_COMPLETE;

   NavigateLoadFromStream(WebBrowser, Stream);
end;

{ WebBrowserSaveHTMLCode }

function WebBrowserSaveHTMLCode(WebBrowser: TWebBrowser; const FileName: TFileName):   Boolean;
var
   ps: IPersistStreamInit;
   fs: TFileStream;
   sa: IStream;
begin
   ps := WebBrowser.Document as IPersistStreamInit;
   fs := TFileStream.Create(FileName, fmCreate);
   try
sa := TStreamAdapter.Create(fs, soReference) as IStream;
Result := Succeeded(ps.Save(sa, True));
   finally
fs.Free;
   end;
end;


{ SaveDocumentSourceToStream }

procedure SaveDocumentSourceToStream(Document: IDispatch; Stream: TStream);
var
   PersistStreamInit: IPersistStreamInit;
   StreamAdapter: IStream;
begin
   Stream.Size   :=   0;
   Stream.Position   :=   0;
   if Document.QueryInterface(IPersistStreamInit, PersistStreamInit) = S_OK then
   begin
StreamAdapter := TStreamAdapter.Create(Stream, soReference);
PersistStreamInit.Save(StreamAdapter, False);
StreamAdapter := nil;
   end;
end;

这篇关于\t\t几个WebBrowser相关的函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/707856

相关文章

Kotlin 作用域函数apply、let、run、with、also使用指南

《Kotlin作用域函数apply、let、run、with、also使用指南》在Kotlin开发中,作用域函数(ScopeFunctions)是一组能让代码更简洁、更函数式的高阶函数,本文将... 目录一、引言:为什么需要作用域函数?二、作用域函China编程数详解1. apply:对象配置的 “流式构建器”最

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

JavaScript Array.from及其相关用法详解(示例演示)

《JavaScriptArray.from及其相关用法详解(示例演示)》Array.from方法是ES6引入的一个静态方法,用于从类数组对象或可迭代对象创建一个新的数组实例,本文将详细介绍Array... 目录一、Array.from 方法概述1. 方法介绍2. 示例演示二、结合实际场景的使用1. 初始化二

C++中::SHCreateDirectoryEx函数使用方法

《C++中::SHCreateDirectoryEx函数使用方法》::SHCreateDirectoryEx用于创建多级目录,类似于mkdir-p命令,本文主要介绍了C++中::SHCreateDir... 目录1. 函数原型与依赖项2. 基本使用示例示例 1:创建单层目录示例 2:创建多级目录3. 关键注

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

kotlin的函数forEach示例详解

《kotlin的函数forEach示例详解》在Kotlin中,forEach是一个高阶函数,用于遍历集合中的每个元素并对其执行指定的操作,它的核心特点是简洁、函数式,适用于需要遍历集合且无需返回值的场... 目录一、基本用法1️⃣ 遍历集合2️⃣ 遍历数组3️⃣ 遍历 Map二、与 for 循环的区别三、高

C语言字符函数和字符串函数示例详解

《C语言字符函数和字符串函数示例详解》本文详细介绍了C语言中字符分类函数、字符转换函数及字符串操作函数的使用方法,并通过示例代码展示了如何实现这些功能,通过这些内容,读者可以深入理解并掌握C语言中的字... 目录一、字符分类函数二、字符转换函数三、strlen的使用和模拟实现3.1strlen函数3.2st

MySQL中COALESCE函数示例详解

《MySQL中COALESCE函数示例详解》COALESCE是一个功能强大且常用的SQL函数,主要用来处理NULL值和实现灵活的值选择策略,能够使查询逻辑更清晰、简洁,:本文主要介绍MySQL中C... 目录语法示例1. 替换 NULL 值2. 用于字段默认值3. 多列优先级4. 结合聚合函数注意事项总结C

Java8需要知道的4个函数式接口简单教程

《Java8需要知道的4个函数式接口简单教程》:本文主要介绍Java8中引入的函数式接口,包括Consumer、Supplier、Predicate和Function,以及它们的用法和特点,文中... 目录什么是函数是接口?Consumer接口定义核心特点注意事项常见用法1.基本用法2.结合andThen链

MySQL 日期时间格式化函数 DATE_FORMAT() 的使用示例详解

《MySQL日期时间格式化函数DATE_FORMAT()的使用示例详解》`DATE_FORMAT()`是MySQL中用于格式化日期时间的函数,本文详细介绍了其语法、格式化字符串的含义以及常见日期... 目录一、DATE_FORMAT()语法二、格式化字符串详解三、常见日期时间格式组合四、业务场景五、总结一、