gpt4 book ai didi

delphi - 如何在 TWebBrowser 中始终加载新页面?

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

我正在尝试使用 Browser.Navigate(URL) 将网页加载到 TWebBrowser 中。但是,浏览器不会关心页面是否在线更新,因此只要我不重新启动程序,它就不会显示新页面。

更具体的例子:如果我导航到具有访问者计数器(如图所示)的网页,计数器将会增加。如果我离开该页面然后返回(不使用后退),计数器将不会增加。在 Firefox 中,它确实会增加。

这是我尝试过的方法,但行不通:

const
DLCTL_PRAGMA_NO_CACHE = $00004000;

procedure TBrowserFrm.LoadURL(URL: string);
var
Flag: OleVariant;
begin
Flag:=DLCTL_PRAGMA_NO_CACHE;
Browser.Navigate(URL, Flag);
end;


procedure TBrowserFrm.LoadURL(URL: string);
var
Flags: OleVariant;
begin
Flags := 'navNoHistory, navNoReadFromCache, navNoWriteToCache';
Browser.navigate2(URL, Flags);
end;

关于如何让 TWebBrowser 加载真实页面有什么想法吗?

最佳答案

在 VCL 中,TWebBrowser 是 Internet Explorer 的包装器,特别是 IWebBrowser2界面。

DLCTL_PRAGMA_NO_CACHE 不是可以传递给 Navigate2() 的标志。阅读文档:

SHDocVw.TWebBrowser

TWebBrowser wraps the IWebBrowser2 interface from Microsoft's Shell Doc Object and Control Library (SHDOCVW.DLL) to allow you to create a customized Web browsing application or to add Internet, file and network browsing, document viewing, and data downloading capabilities to your applications.

IWebBrowser2::Navigate2 Method

Flags [in] A pointer to a VARIANT of type VT_I4 or VT_I2 that specifies a combination of the values defined by the BrowserNavConstants enumeration.

BrowserNavConstants Enumerated Type

typedef enum BrowserNavConstants {
navOpenInNewWindow = 0x1,
navNoHistory = 0x2,
navNoReadFromCache = 0x4,
navNoWriteToCache = 0x8,
navAllowAutosearch = 0x10,
navBrowserBar = 0x20,
navHyperlink = 0x40,
navEnforceRestricted = 0x80,
navNewWindowsManaged = 0x0100,
navUntrustedForDownload = 0x0200,
navTrustedForActiveX = 0x0400,
navOpenInNewTab = 0x0800,
navOpenInBackgroundTab = 0x1000,
navKeepWordWheelText = 0x2000,
navVirtualTab = 0x4000,
navBlockRedirectsXDomain = 0x8000,
navOpenNewForegroundTab = 0x10000
} BrowserNavConstants;

如您所见,DLCTL_PRAGMA_NO_CACHE 不在该列表中。它实际上是您在为浏览器的 DISPID_AMBIENT_DLCONTROL 属性实现处理程序时指定为输出值的标志。阅读 MSDN 文档:

WebBrowser Customization | Controlling Download and Execution

The WebBrowser Control gives you control over what it downloads, displays, and executes. To gain this control, you need to implement your host's IDispatch so it handles DISPID_AMBIENT_DLCONTROL. When the WebBrowser Control is instantiated, it will call your IDispatch::Invoke with this ID. Set pvarResult to a combination of following flags, using the bitwise OR operator, to indicate your preferences.
...
•DLCTL_RESYNCHRONIZE and DLCTL_PRAGMA_NO_CACHE: These flags cause cache refreshes. With DLCTL_RESYNCHRONIZE, the server will be asked for update status. Cached files will be used if the server indicates that the cached information is up-to-date. With DLCTL_PRAGMA_NO_CACHE, files will be re-downloaded from the server regardless of the update status of the files.
...

因此,您必须实现自定义 IDispatch 对象并将其挂接到 IWebBrowser2 中,才能正确使用 DLCTL_PRAGMA_NO_CACHE

或者,您可以考虑切换到 TEmbeddedWB ,它为您处理浏览器自定义,并具有接受 DLCTL... 标志的 DownloadOptions 属性,包括 DLCTL_PRAGMA_NO_CACHE

关于delphi - 如何在 TWebBrowser 中始终加载新页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28440480/

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