How can I get information about the scrollbars of an instance of a Webbrowser or IE Webrowser control?
I need to get information about scrollbars (position, size, visibility) of an external application's Webbrowser control , I tried to use GetScrollBarInfo from my previous question , but the function always returns false, I have tested this function with other applications and it worked fine, but not with IE or with the Webbrowser control.So how I can get information about the scrollbars of an Webbrowser control instance or the IE Webbrowser?
source to share
This is how you can tell if scrollbars are visible or not. Some error checking has been omitted for brevity.
LPDISPATCH lpDispatch;
lpDispatch = m_Browser.GetDocument();
IHTMLDocument2 *doc2 = NULL;
disp->QueryInterface(IID_IHTMLDocument2,(void**)&doc2);
IHTMLElement *lpBodyElement;
IHTMLBodyElement *lpBody;
doc2->get_body(&lpBodyElement);
if ( lpBodyElement )
{
lpBodyElement->QueryInterface(IID_IHTMLBodyElement,(void**)&lpBody);
if ( lpBody )
{
BSTR bstrText;
pBody->get_scroll(&bstrText);
lpBody->Release();
}
lpBodyElement->Release();
}
doc2->Release();
Possible values for bstrText are yes, no, auto (scrollbars are displayed when the page content exceeds the client area)
And this is how you can find out the current scroll position:
IHTMLElement2 *pElement = NULL;
hr = pBody->QueryInterface(IID_IHTMLElement2,(void**)&pElement);
ASSERT(SUCCEEDED(hr));
ASSERT( pElement );
long scroll_pos;
pElement->get_scrollTop( &scroll_pos);
source to share
You can send a message WM_HTML_GETOBJECT
to the "Internet Explorer_Server"
external application class window to receive IHtmlDocument2
, and then using IServiceProvider
you can get the interface IWebBrowser2
.
Here's some sample code in Delphi:
uses
ActiveX, MSHTML;
type
TObjectFromLResult = function(LRESULT: lResult; const IID: TIID;
wParam: wParam; out pObject): HRESULT; stdcall;
function GetIEFromHWND(WHandle: HWND; var IE: IWebbrowser2): HRESULT;
var
hInst: HWND;
lRes: Cardinal;
Msg: Integer;
pDoc: IHTMLDocument2;
ObjectFromLresult: TObjectFromLresult;
begin
Result := S_FALSE;
hInst := LoadLibrary('Oleacc.dll');
@ObjectFromLresult := GetProcAddress(hInst, 'ObjectFromLresult');
if @ObjectFromLresult <> nil then
try
Msg := RegisterWindowMessage('WM_HTML_GETOBJECT');
SendMessageTimeOut(WHandle, Msg, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes);
Result := ObjectFromLresult(lRes, IHTMLDocument2, 0, pDoc);
if Result = S_OK then
(pDoc.parentWindow as IServiceprovider).QueryService(
IWebbrowserApp, IWebbrowser2, IE);
finally
FreeLibrary(hInst);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Wnd, WndChild: HWND;
IE: IWebBrowser2;
Document: IHtmlDocument2;
ScrollTop, ScrollLeft: Integer;
begin
Wnd := FindWindow('IEFrame', nil); // top level IE
if Wnd = 0 then Exit;
WndChild := FindWindowEX(Wnd, 0, 'Shell DocObject View', nil);
if WndChild = 0 then Exit;
WndChild := FindWindowEX(WndChild, 0, 'Internet Explorer_Server', nil);
if WndChild = 0 then Exit;
GetIEFromHWnd(WndChild, IE);
if IE <> nil then
begin
ShowMessage(IE.LocationURL);
Document := IE.Document as IHtmlDocument2;
ScrollTop := ((Document as IHTMLDocument3).documentElement as IHTMLElement2).scrollTop;
ScrollLeft := ((Document as IHTMLDocument3).documentElement as IHTMLElement2).scrollLeft;
ShowMessage(Format('%d;%d', [ScrollTop, ScrollLeft]));
// visible|hidden|scroll|auto|no-display|no-content
ShowMessage(OleVariant(Document).documentElement.currentStyle.overflowX);
ShowMessage(OleVariant(Document).documentElement.currentStyle.overflowY);
end;
end;
Edit: when the page uses the directive <!DOCTYPE>
to switch IE6 to strict standard use mode document.documentElement
. ( IHTMLDocument3
) in pre-standard mode, the body is a scrollable area, so you can get the scroll position with document.body.scrollTop
. In standard mode, the HTML element is scrolled, so you should use document.documentElement.scrollTop
.
If you document.documentElement.clientWidth <> 0
use element documentElement
for else properties, use element body
.
Useful properties relating to information on scrolling, as clientHeight
, scrollWidth
, scrollHeight
.
source to share