C++Builder에서 웹브라우저를 어떻게 프로그램적으로 스크롤 시키는가?
[소개]
난 이런 작업을 어떻게하는 몰라 때때로 몸부림쳐야 했다 ㅡ,.ㅡ; (몸부림씩이나...)
어떻게 프로그램적으로 브라우저를 스크롤 시킬까?
도대체 윈도 스크롤 메시지도 안받아들이고, MFC GetScrollInfo API 도 소용없으니... 하지만 길이 있었다.
IHTMLElement2 COM 인터페이스의 get_scrollTop/put_scrollTop를 사용해 스크롤시키면 된다는 것이었다.
(ㅡㅡ;)
[코드 구성]
폼에 CppWebBrowser 와 버턴 올려 놓고 아래와 같이 테스트할 수 있습니다.
CppWebBrowser는 WebBr로 컴포넌트 객체 이름을 주었습니다.
//---------------------------------------------------------------------------
#include <mshtml.h>
#include <assert.h>
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
WebBr->Navigate(WideString("naver.com"));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
#define ASSERT assert
//
// All this code does is what
// "m_browser.Document.Body.ScrollTop = 100;"
// does in VB. Gotta love COM in C++.
//
// let's say m_browser is the WebBrowser's member variable.
HRESULT hr;
// get the document dispatch from browser
IDispatch *pDisp = WebBr->Document;
ASSERT( pDisp ); //if NULL, we failed
// get document interface
IHTMLDocument2 *pDocument = NULL;
hr = pDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pDocument );
ASSERT( SUCCEEDED( hr ) );
ASSERT( pDocument );
//
// this is the trick!
// take the body element from document...
//
IHTMLElement *pBody = NULL;
hr = pDocument->get_body( &pBody );
ASSERT( SUCCEEDED( hr ) );
ASSERT( pBody );
// from body we can get element2 interface,
// which allows us to do scrolling
IHTMLElement2 *pElement = NULL;
hr = pBody->QueryInterface(IID_IHTMLElement2,(void**)&pElement);
ASSERT(SUCCEEDED(hr));
ASSERT( pElement );
// now we are ready to scroll 이제 준비되었으니 원하는 픽셀만큼 스크롤 시킬 수 있다.
// scroll down to 100th pixel from top
pElement->put_scrollTop( 100 );
// try to get the whole page size - but the returned number
// is not allways correct. especially with pages that use dynamic html
// tricks...
long scroll_height;
pElement->get_scrollHeight( &scroll_height );
// we can use this workaround! 현 웹페이지의 전체 크기를 얻으려면.
long real_scroll_height;
pElement->put_scrollTop( 20000000 ); // ask to scroll really far down...
pElement->get_scrollTop( &real_scroll_height );
real_scroll_height += WebBr->Height; // will return the scroll height
// for the first visible pixel, to get whole html page size must
// add the window's height... (to obtain window_height is
// left as an exercise for the reader)
// print to debug output
Caption = String().sprintf( "real scroll height: %ld, get_scrollHeight: %ld",
real_scroll_height, scroll_height );
}
//---------------------------------------------------------------------------
그럼 알아서 쓰세요.
ㅡ..ㅡ;
원문:
http://www.codeproject.com/miscctrl/scrollbrowser.asp
Programmatically scrolling WebBrowser control from Visual C/C++
assert 는 참이 아닌 경우는 소스의 어디에 어떤 평가값이 에러다라고 보여주기 위한 디버깅 용도입니다.
위 내용은
if (!pDisp)
{
에러 표시;
프로그램 종료;
}
와 같습니다.
IDispatch *pDisp;
는 COM 인터페이스를 얻어 오기 위해 기본 인터페이스 포인트입니다.
이건 COM에 대해 공부를 따로 하셔야 합니다.
시간이 없다면 그냥 위처럼 관행적으로 사용하시면 됩니다.