如何以编程方式在 TWebBrowser 的导航结束时自动滚动到所需位置
how to programmatically at the end of Navigation of a TWebBrowser autoscroll to desired position
我想知道如何在 TWebBrowser
(Delphi XE7) 的导航结束时以编程方式强制此页面从左上角显示页面(某种自动滚动).出于未知原因,Web 浏览器在导航结束时向右滚动。
我尝试了网上的各种解决方案。 SendMessage
是其中之一:
SendMessage(WebBrowser1.Handle, WM_HSCROLL, 0 , 0);
但 none 有效。任何的想法?
简单而正确的方法是对WebBrowser 使用DOM 而不是SendMessage
。
例如:
var
window: IHTMLWindow2;
window := (WebBrowser1.Document as IHTMLDocument2).parentWindow;
window.scroll(0, 0);
为什么 SendMessage(WebBrowser1.Handle, ...)
不起作用?
TWebBrowser.Handle
不是您应该向其发送消息的 IE 句柄。它是一个包装器 window (Shell Embedding
) 持有 IE window,名称 class Internet Explorer_Server
。
根据 IE 版本和文档模式,结构可能是(使用 Spy++ 检查结构):
Shell Embedding
Shell DocObject View
Internet Explorer_Server <- send message to this window
您可以使用 EnumChildWindows
找到 Internet Explorer_Server
:
function EnumChilds(hwnd: HWND; lParam: LPARAM): BOOL; stdcall;
const
Server = 'Internet Explorer_Server';
var
ClassName: array[0..24] of Char;
begin
GetClassName(hwnd, ClassName, Length(ClassName));
Result := ClassName <> Server;
if not Result then
PLongWord(lParam)^ := hwnd;
end;
function GetIEHandle(AWebBrowser: TWebbrowser): HWND;
begin
Result := 0;
EnumChildWindows(AWebBrowser.Handle, @EnumChilds, LongWord(@Result));
end;
并发送消息:
IEHandle := GetIEHandle(WebBrowser1);
if IEHandle <> 0 then
begin
SendMessage(IEHandle, WM_HSCROLL, SB_LEFT ,0);
SendMessage(IEHandle, WM_VSCROLL, SB_TOP ,0);
end;
我想知道如何在 TWebBrowser
(Delphi XE7) 的导航结束时以编程方式强制此页面从左上角显示页面(某种自动滚动).出于未知原因,Web 浏览器在导航结束时向右滚动。
我尝试了网上的各种解决方案。 SendMessage
是其中之一:
SendMessage(WebBrowser1.Handle, WM_HSCROLL, 0 , 0);
但 none 有效。任何的想法?
简单而正确的方法是对WebBrowser 使用DOM 而不是SendMessage
。
例如:
var
window: IHTMLWindow2;
window := (WebBrowser1.Document as IHTMLDocument2).parentWindow;
window.scroll(0, 0);
为什么 SendMessage(WebBrowser1.Handle, ...)
不起作用?
TWebBrowser.Handle
不是您应该向其发送消息的 IE 句柄。它是一个包装器 window (Shell Embedding
) 持有 IE window,名称 class Internet Explorer_Server
。
根据 IE 版本和文档模式,结构可能是(使用 Spy++ 检查结构):
Shell Embedding
Shell DocObject View
Internet Explorer_Server <- send message to this window
您可以使用 EnumChildWindows
找到 Internet Explorer_Server
:
function EnumChilds(hwnd: HWND; lParam: LPARAM): BOOL; stdcall;
const
Server = 'Internet Explorer_Server';
var
ClassName: array[0..24] of Char;
begin
GetClassName(hwnd, ClassName, Length(ClassName));
Result := ClassName <> Server;
if not Result then
PLongWord(lParam)^ := hwnd;
end;
function GetIEHandle(AWebBrowser: TWebbrowser): HWND;
begin
Result := 0;
EnumChildWindows(AWebBrowser.Handle, @EnumChilds, LongWord(@Result));
end;
并发送消息:
IEHandle := GetIEHandle(WebBrowser1);
if IEHandle <> 0 then
begin
SendMessage(IEHandle, WM_HSCROLL, SB_LEFT ,0);
SendMessage(IEHandle, WM_VSCROLL, SB_TOP ,0);
end;