win32htmlbrowser.cpp

来自「这是VCF框架的代码」· C++ 代码 · 共 897 行 · 第 1/2 页

CPP
897
字号
//Win32HTMLBrowser.cpp/*Copyright 2000-2004 The VCF Project.Please see License.txt in the top level directorywhere you installed the VCF.*///Win32HTMLBrowser.h#include "vcf/HTMLKit/HTMLKit.h"#include "vcf/HTMLKit/HTMLBrowserControl.h"#include "vcf/ApplicationKit/Win32Object.h"#include "vcf/ApplicationKit/AbstractWin32Component.h"#include "vcf/HTMLKit/Win32HTMLBrowser.h"#include "vcf/ApplicationKit/Win32Toolkit.h"using namespace VCF;STDMETHODIMP HTMLEventHandler::Invoke( DISPID dispIdMember, REFIID riid, LCID lcid, 										WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, 										EXCEPINFO* pExcepInfo, UINT* puArgErr ) {	if ( 0 == dispIdMember && NULL != handler ) {		HTMLElementEvent e(eventSource,HTMLBrowserControl::heElementClicked);		e.elementID = elementID;				handler->invoke( &e );				return S_OK;	}		return E_NOTIMPL;}Win32HTMLBrowser::Win32HTMLBrowser():	browserHwnd_(NULL)	{	uiStyle_ = DOCHOSTUIFLAG_NO3DBORDER;}Win32HTMLBrowser::~Win32HTMLBrowser(){	clearHTMLHandlers();}void Win32HTMLBrowser::create( VCF::Control* owningControl ){	{		String className = "VCF::Win32HTMLBrowser";		if ( true != isRegistered() ){			registerWin32Class( className, wndProc_  );		}		CreateParams params = createParams();		Win32ToolKit* toolkit = (Win32ToolKit*)UIToolkit::internal_getDefaultUIToolkit();		HWND parent = toolkit->getDummyParent();		if ( System::isUnicodeEnabled() ) {			hwnd_ = ::CreateWindowExW( params.second, className.c_str(),										L"",	params.first,										0, 0,										1,										1,										parent,										NULL, ::GetModuleHandleW(NULL), NULL );		}		else {			hwnd_ = ::CreateWindowExA( params.second, className.ansi_c_str(),										"",	params.first,										0, 0,										1,										1,										parent,										NULL, ::GetModuleHandleA(NULL), NULL );		}		if ( NULL != hwnd_ ){			Win32Object::registerWin32Object( this );			//embed the browser 			embed( hwnd_ );		}		else {			throw RuntimeException( MAKE_ERROR_MSG_2("Unable to create hwnd") );		}	}	setCreated( true );}void Win32HTMLBrowser::setFocused(){	AbstractWin32Component::setFocused();	//::SetFocus( browserHwnd_ );}void Win32HTMLBrowser::setVisible( const bool& val ){	AbstractWin32Component::setVisible( val );	if ( val ) {		//::ShowWindow( browserHwnd_, SW_SHOW );	}	else {		//::ShowWindow( browserHwnd_, SW_HIDE );	}}bool Win32HTMLBrowser::handleEventMessages( UINT message, WPARAM wParam, LPARAM lParam, LRESULT& wndResult, WNDPROC defaultWndProc ){	bool result = false;	switch ( message ) {		case WM_DESTROY : {			unembed();			result = AbstractWin32Component::handleEventMessages( message, wParam, lParam, wndResult );					}		break;		case WM_PAINT : {			if ( System::isUnicodeEnabled() ) {				wndResult = DefWindowProcW(hwnd_, message, wParam, lParam);				result = true;			}			else {				wndResult = DefWindowProcA(hwnd_, message, wParam, lParam);				result = true;			}		}		break;		case WM_ERASEBKGND : {			wndResult = 0;			result = true;		}		break;		case WM_SIZE : {			result = AbstractWin32Component::handleEventMessages( message, wParam, lParam, wndResult );						resize( LOWORD(lParam), HIWORD(lParam) );		}		break;		default : {			result = AbstractWin32Component::handleEventMessages( message, wParam,lParam, wndResult, defaultWndProc );		}		break;	}	return  result;}Win32Object::CreateParams Win32HTMLBrowser::createParams(){	Win32Object::CreateParams result;	result.second = 0;	//WS_CLIPSIBLINGS and friends cause TONS of flicker	//during repaints, so we get rid of them.	result.first = WS_CHILD;//  | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;	return result;}String Win32HTMLBrowser::getCurrentURL(){	return WebBrowserCtrl::getCurrentURL();}void Win32HTMLBrowser::setCurrentURL( const String& url ){	loadingURL_ = url;	openURL( url );}void Win32HTMLBrowser::goBack(){	back();}void Win32HTMLBrowser::goForward(){	forward();}void Win32HTMLBrowser::goHome(){	home();}void Win32HTMLBrowser::refresh(){	WebBrowserCtrl::refresh();}bool Win32HTMLBrowser::isLoadingURL(){	return busy();}void Win32HTMLBrowser::stopLoadingURL(){	WebBrowserCtrl::stop();}void Win32HTMLBrowser::setFromHTML( const String& html ){	if ( busy() ) {		//barf!		throw RuntimeException( "The browser is still loading it's document and you can't set the document's HTML while it's loading." );	}	HTMLDocument doc = getDocument();	if ( !doc.null() ) {		doc.write( html );	}}String Win32HTMLBrowser::getTitle(){	String result;	HTMLDocument doc = getDocument();	if ( !doc.null() ) {		result = doc.getTitle();	}	return result;}void Win32HTMLBrowser::edit( const bool& val ){	HTMLDocument doc = getDocument();	if ( !doc.null() ) {		doc.setDesignMode( val );	}	else {		throw RuntimeException( "Can't edit a null document." );	}}void Win32HTMLBrowser::copy(){	HTMLDocument doc = getDocument();	if ( !doc.null() ) {		doc.copy();	}	else {		throw RuntimeException( "Can't copy from a null document." );	}}void Win32HTMLBrowser::selectAll(){	HTMLDocument doc = getDocument();	if ( !doc.null() ) {		doc.selectAll();	}	else {		throw RuntimeException( "Can't make a selection from a null document." );	}}void Win32HTMLBrowser::setAllowsPopupWindows( bool val ){//no-op}void Win32HTMLBrowser::setAllowsScrollbars( bool val ){	uiStyle_ = val ? (uiStyle_ ^ DOCHOSTUIFLAG_SCROLL_NO)                                      : (uiStyle_ | DOCHOSTUIFLAG_SCROLL_NO);	}void Win32HTMLBrowser::setAllowsTextSelection( bool val ){	uiStyle_ = val ? (uiStyle_ ^ DOCHOSTUIFLAG_DIALOG)                                      : (uiStyle_ | DOCHOSTUIFLAG_DIALOG);}String Win32HTMLBrowser::getElementHTMLText( const String& elementName ){	return getElementText( true, elementName );}void Win32HTMLBrowser::setElementHTMLText( const String& elementName, const String& html ){	setElementText( true, elementName, html );}String Win32HTMLBrowser::getElementText( const String& elementName ){	return getElementText( false, elementName );}void Win32HTMLBrowser::setElementText( const String& elementName, const String& text ){	setElementText( false, elementName, text );}String Win32HTMLBrowser::getActiveElementID(){	String result;	com_ptr<IDispatch> disp;	IHTMLDocument2Ptr doc;	browser_->get_Document( disp.out() );	doc = com_cast( disp );	if ( doc ) {		IHTMLElementPtr item;		doc->get_activeElement( item.out() );		bstr_t tmp;		item->get_id( tmp.out() );		result = tmp.c_str();		item->get_tagName( tmp.out() );	}	return result;}String Win32HTMLBrowser::getElementIDFromPoint( Point* pt ){	String result;	com_ptr<IDispatch> disp;	IHTMLDocument2Ptr doc;	browser_->get_Document( disp.out() );	doc = com_cast( disp );	if ( doc ) {		IHTMLElementPtr item;		doc->elementFromPoint( (long)pt->x_, (long)pt->y_, item.out() );		bstr_t tmp;		item->get_id( tmp.out() );		result = tmp.c_str();	}	return result;}String Win32HTMLBrowser::getElementText( bool textIsHTML, const String& elementName ){	String result;	VCF::HTMLElementPeer item;	VCF::HTMLElementCollectionPeer collection;		com_ptr<IDispatch> disp;	IHTMLDocument2Ptr doc;	browser_->get_Document( disp.out() );	doc = com_cast( disp );	if ( doc ) {		doc->get_all( collection.out() );		if ( collection ) {			VCF::HTMLElementCollection coll2;			coll2.setPeer( &collection );			long len = coll2.getLength();						for (int i=0;i<len;i++ ) {				VCF::HTMLElement* f = coll2[i];				if ( NULL != f ) {					if ( elementName == f->getID() ) {													item = *f->getPeer();						break;					}				}			}					}	}		if ( !item.is_null() ) {		bstr_t val;		if ( textIsHTML ) {			item->get_innerHTML( val.out() );		}		else {			item->get_innerText( val.out() );		}		result = val.c_str();	}	return result;}void Win32HTMLBrowser::setElementText( bool textIsHTML, const String& elementName, const String& text ){	VCF::HTMLElementPeer item;		HTMLElementCollectionPeer collection;	com_ptr<IDispatch> disp;	IHTMLDocument2Ptr doc;	browser_->get_Document( disp.out() );	doc = com_cast( disp );	if ( doc ) {		doc->get_all( collection.out() );		if ( collection ) {			VCF::HTMLElementCollection coll2;			coll2.setPeer( &collection );			long len = coll2.getLength();						for (int i=0;i<len;i++ ) {				VCF::HTMLElement* f = coll2[i];				if ( NULL != f ) {					if ( elementName == f->getID() ) {													item = *f->getPeer();						break;					}				}			}					}	}		if ( !item.is_null() ) {		bstr_t val = text.c_str();								if ( textIsHTML ) {			item->put_innerHTML( val.in() );		}		else {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?