Click here to Skip to main content
15,892,746 members
Articles / Programming Languages / C++/CLI

MaChat - a chat with a browser for LANs

Rate me:
Please Sign up or sign in to vote.
4.97/5 (27 votes)
30 Jul 200211 min read 582.6K   12.4K   119  
This article shows how to create a Chat for Local Area Networks which uses the WebBrowser control to display the messages.
////////////////////////////////////////////////////
// File            - WebBrowserEx.cpp
// Autor           - Michael Mac
// Contact         - GreenSequoia@wp.pl
// Description     - Implementation of the WebBrowserEx class
////////////////////////////////////////////////////

#include "stdafx.h"
#include "WebBrowserEx.h"
#include "WinError.h"

using namespace Crownwood::Magic::Menus;

#pragma warning ( disable : 4100 ) // Disable C4100 warning
#pragma warning ( disable : 4564 ) // Disable C4564 warning

namespace MaChat
{
	namespace WebBrowser
	{
		////////////////////////////////////////////////////
		// MaChat::WebBrowser::WebBrowserEx
		////////////////////////////////////////////////////
		WebBrowserEx::WebBrowserEx( Control* controlParent )
		{
			// Initialize
			this->BeginInit();
			controlParent->Controls->Add( this );
			this->CommandStateChange += new DWebBrowserEvents2_CommandStateChangeEventHandler( this, OnWebStateChange );
			this->EndInit();

			// Set several options
			this->RegisterAsBrowser = true;
			this->RegisterAsDropTarget = true;
			this->Silent = false;

			// Navigate to a blank page
			Navigate( "about:blank" );

			// Set the UI handler for the browser to this application
			IHTMLDocument2* doc = dynamic_cast<IHTMLDocument2*>( this->Document );
			ICustomDoc* custom = dynamic_cast<ICustomDoc*>( doc );
			custom->SetUIHandler( static_cast<IDocHostUIHandler*>( this ) );
		}

		void WebBrowserEx::OnWebStateChange( Object* sender, DWebBrowserEvents2_CommandStateChangeEvent* e )
		{
			if ( e->command == CommandStateChangeConstants::CSC_NAVIGATEBACK )
				m_bCanGoBack = e->enable;
			if ( e->command == CommandStateChangeConstants::CSC_NAVIGATEFORWARD )
				m_bCanGoForward = e->enable;		
		}

		// IDocHostUIHandler implementation
		////////////////////////////////////////////////////
		void WebBrowserEx::ShowContextMenu( unsigned int dwID, tagPOINT* ppt, 
			[MarshalAs(UnmanagedType::IUnknown)] Object* pcmdtReserved, 
			[MarshalAs(UnmanagedType::IDispatch)]Object* pdispReserved)
		{
			ContextMenuConstants eMenu = (ContextMenuConstants)dwID;
			System::Drawing::Point point = System::Drawing::Point::Point( ppt->x, ppt->y );
			
			// Fire the event
			if ( ContextMenu )
			{
				ContextMenu->Invoke( this, new ContextMenuEventArgs( eMenu, point,
					pcmdtReserved, pdispReserved ) );
			}
		}

		void WebBrowserEx::GetHostInfo(DOCHOSTUIINFO* pInfo)        
		{
		}

		void WebBrowserEx::ShowUI(unsigned int dwID,
			[MarshalAs(UnmanagedType::Interface)] IntPtr pActiveObject,
			[MarshalAs(UnmanagedType::Interface)] IntPtr pCommandTarget,
			[MarshalAs(UnmanagedType::Interface)] IntPtr pFrame,
			[MarshalAs(UnmanagedType::Interface)] IntPtr pDoc)
		{
		}

		void WebBrowserEx::HideUI()        
		{
		}

		void WebBrowserEx::UpdateUI()        
		{
		}

		void WebBrowserEx::EnableModeless(int fEnable)        
		{
		}

		void WebBrowserEx::OnDocWindowActivate(int fActivate)        
		{
		}

		void WebBrowserEx::OnFrameWindowActivate(int fActivate)        
		{
		}

		void WebBrowserEx::ResizeBorder( tagRECT* prcBorder,
			[MarshalAs(UnmanagedType::Interface)] IntPtr pUIWindow, int fRameWindow)
		{
		}

		void WebBrowserEx::TranslateAccelerator( tagMSG* lpmsg, Guid* pguidCmdGroup, unsigned int nCmdID)
		{
			/*if ( lpmsg->message == 0x0100 && lpmsg->lParam & 0x11)
				Marshal::ThrowExceptionForHR( 0x80004005L );*/
		}

		void WebBrowserEx::GetOptionKeyPath( String* pchKey, unsigned int dw)        
		{
			
		}

		void WebBrowserEx::GetDropTarget( [MarshalAs(UnmanagedType::Interface)] IntPtr pDropTarget,
			[MarshalAs(UnmanagedType::Interface)] IntPtr ppDropTarget)
		{
		}

		void WebBrowserEx::GetExternal( Object* ppDispatch )
		{
		}

		void WebBrowserEx::TranslateUrl( unsigned int dwTranslate, unsigned short pchURLIn,
					IntPtr ppchURLOut)
		{
		}

		void WebBrowserEx::FilterDataObject(IDataObject* pDO, IDataObject* ppDORet)
		{
		}

		// Navigate functions
		////////////////////////////////////////////////////
		void WebBrowserEx::Navigate ( String* strUrl )
		{
			Object* objMissing = System::Reflection::Missing::Value;
			
			// Navigate
			AxWebBrowser::Navigate( strUrl, &objMissing, &objMissing,
				&objMissing, &objMissing );	
		}

		void WebBrowserEx::NavigateNW ( String* strUrl )
		{
			Object* objMissing = System::Reflection::Missing::Value;
			Object* objTarget = static_cast<Object*>( new String( "_BLANK" ) );

			// Navigate
			AxWebBrowser::Navigate( strUrl, &objMissing, &objTarget,
				&objMissing, &objMissing );	
		}
	}
}

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
Architect macmichal.pl
Poland Poland
Micheal is an independent consultant - www.macmichal.pl.
He's main areas of interest are: DDD\CqRS, TDD, SaaS, Design Patterns, Architecture. He specializes in .Net/C# for the early beginning of it and T-SQL. He's a writer, blogger (blog.macmichal.pl) and speaker.

In his spare time, he's climbing the mountains all over the Europe.

Comments and Discussions