Click here to Skip to main content
15,886,815 members
Articles / Desktop Programming / WTL

Web Win32/WTL Hybrid

Rate me:
Please Sign up or sign in to vote.
4.83/5 (15 votes)
22 Dec 20054 min read 136.2K   2.1K   50  
How to implement a two-way communication path from IExplorer and WTL code
// WebWin32SampleView.h : interface of the CWebWin32SampleView class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

#include "iExplorer.h"

#define BEGIN_HTML_DDX() void _HtmlDDXElements( CWTLIExplorer & pBrowser, BOOL VarToControl ) {
#define   BEGIN_FORM( X ) { _bstr_t formName( X );
#define     DDX_HTML_STRING(x,z) pBrowser.QueryHtmlElementValueTextBox( x,formName,z, VarToControl );
#define     DDX_HTML_CHECKBOX(x,z) pBrowser.QueryHtmlElementValueCheckBox( x,formName,z, VarToControl );
#define     DDX_HTML_LISTBOX(x,z) pBrowser.QueryHtmlElementValueListBox( x,formName,z, VarToControl );

#define   END_FORM() }
#define END_HTML_DDX() };
#define DDX_HTML_EXCHANGE( T , Z ) _HtmlDDXElements(T,Z);

#define ID_HTML_CLICK 1

#define SET_HTML_MESSAGE_MAP() _DefineHtmlMsgMap();
#define BEGIN_HTML_MSG_MAP(X,Y) 	void _DefineHtmlMsgMap()\
{\
CWTLIExplorer & __pBrowser = Y;\
DWORD pointer_part_1;\
DWORD pointer_part_2 = (DWORD) this;\
LRESULT (X::*pVar) ( BOOL & );

#define COMMAND_HTML_HANDLER(id,elementName,handler)		pVar = handler;\
memcpy( &pointer_part_1, ( char * )  &pVar, 4 );\
__pBrowser.SetObjectHandler( id, elementName, pointer_part_1, pointer_part_2 );

#define END_HTML_MSG_MAP() }


class CWebWin32SampleView : public CWindowImpl<CWebWin32SampleView,CAxWindow>
{
public:
	DECLARE_WND_SUPERCLASS(NULL, CAxWindow::GetWndClassName())

	// Our internet explorer
	CWTLIExplorer m_pBrowser;

	// Our vars to Exchange information with HTML page...
	CString		  m_FirstName;
	CString		  m_LastName;
	CString		  m_Address;
	CString		  m_Country;
	BOOL		  m_ILikeThisSample;

	 // html-hosted-page-form-based DDX
 	BEGIN_HTML_DDX()
	   BEGIN_FORM( "testForm" ) // Our Form Name ( at HTML page )
		  // Our exchange definitions...
		  DDX_HTML_STRING( m_FirstName ,"firstName" ) 
	      DDX_HTML_STRING( m_LastName  ,"lastName" )
	      DDX_HTML_STRING( m_Address,"address" )
	      DDX_HTML_LISTBOX( m_Country, "country" );
	      DDX_HTML_CHECKBOX( m_ILikeThisSample, "ILikeIt" )
	   END_FORM()
 	END_HTML_DDX()


	// HTML Handlers
	// Here we can define HTML handlers hooks related to element the 
	// page loaded
	BEGIN_HTML_MSG_MAP(CWebWin32SampleView,m_pBrowser)
		// Set an event handler on buttonSumbmit.onclick 
		COMMAND_HTML_HANDLER(ID_HTML_CLICK, "buttonSubmit", OnSubmit )
	END_HTML_MSG_MAP()

	LRESULT OnSubmit( BOOL & bHandled )
	{
		// Do Data Exchange with HTML page...
		DDX_HTML_EXCHANGE( m_pBrowser, false )

		// MessageBox text Composite 
		CString totalMessage = m_FirstName + CString(" - ") + m_LastName + CString(" - " ) + m_Address;

		if ( m_ILikeThisSample )
			 totalMessage += CString("\n\nYou have checked it." );
		else
			 totalMessage += CString("\n\nYou have not checked it." );

		totalMessage += CString("\n\nCountry Selected: ") + m_Country;

		// Show a message box
		MessageBox( totalMessage,"From CWebWin32SampleView::OnSubmit event handler.", MB_ICONINFORMATION );

		// set handled
		bHandled = true;
 
		return 0;
	}


	BOOL PreTranslateMessage(MSG* pMsg);

	BEGIN_MSG_MAP(CWebWin32SampleView)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_SIZE, OnSize)

		// We receive this message when the IExplorer is on a ready state
		// and we can Set handlers to the page...
		MESSAGE_HANDLER(WM_HTML_SETHANDLERS /* = WM_USER + 1 */ , OnHTMLSetHandlers )
	END_MSG_MAP()

	LRESULT OnHTMLSetHandlers(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ )
	{
		// Activate event handlers map
		SET_HTML_MESSAGE_MAP();
		return 0;
	}

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
CEO wave-vs.net
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions