Click here to Skip to main content
15,891,757 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.5K   12.4K   119  
This article shows how to create a Chat for Local Area Networks which uses the WebBrowser control to display the messages.
// WebBrowserEx.h

#pragma once

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Runtime::InteropServices;

// WebBrowser
using namespace mshtml;
using namespace AxSHDocVw;

#ifdef _DEBUG
	using namespace System::Diagnostics;
#endif

namespace MaChat
{
	namespace WebBrowser
	{
		public __value enum DOCHOSTUITYPE
		{
			DOCHOSTUITYPE_BROWSE    = 0,
			DOCHOSTUITYPE_AUTHOR    = 1
		};

		public __value enum ContextMenuConstants
		{
			Default			= 0,
			Image			= 1,
			Control			= 2,
			Table			= 3,
			Selection		= 4,
			Anchor			= 5,
			Unknown			= 6,
			VScroll			= 10,
			HScroll			= 11
		};

		public __value enum DOCHOSTUIDBLCLK
		{
			DOCHOSTUIDBLCLK_DEFAULT         = 0,
			DOCHOSTUIDBLCLK_SHOWPROPERTIES	= 1,
			DOCHOSTUIDBLCLK_SHOWCODE        = 2
		};

		public __value enum DOCHOSTUIFLAG
		{
			DOCHOSTUIFLAG_DIALOG                    = 0x00000001,
			DOCHOSTUIFLAG_DISABLE_HELP_MENU         = 0x00000002,
			DOCHOSTUIFLAG_NO3DBORDER                = 0x00000004,
			DOCHOSTUIFLAG_SCROLL_NO                 = 0x00000008,
			DOCHOSTUIFLAG_DISABLE_SCRIPT_INACTIVE   = 0x00000010,
			DOCHOSTUIFLAG_OPENNEWWIN                = 0x00000020,
			DOCHOSTUIFLAG_DISABLE_OFFSCREEN         = 0x00000040,
			DOCHOSTUIFLAG_FLAT_SCROLLBAR            = 0x00000080,
			DOCHOSTUIFLAG_DIV_BLOCKDEFAULT          = 0x00000100,
			DOCHOSTUIFLAG_ACTIVATE_CLIENTHIT_ONLY   = 0x00000200,
			DOCHOSTUIFLAG_OVERRIDEBEHAVIORFACTORY   = 0x00000400,
			DOCHOSTUIFLAG_CODEPAGELINKEDFONTS       = 0x00000800,
			DOCHOSTUIFLAG_URL_ENCODING_DISABLE_UTF8 = 0x00001000,
			DOCHOSTUIFLAG_URL_ENCODING_ENABLE_UTF8	= 0x00002000,
			DOCHOSTUIFLAG_ENABLE_FORMS_AUTOCOMPLETE = 0x00004000,
			DOCHOSTUIFLAG_ENABLE_INPLACE_NAVIGATION = 0x00010000,
			DOCHOSTUIFLAG_IME_ENABLE_RECONVERSION   = 0x00020000,
			DOCHOSTUIFLAG_THEME                     = 0x00040000,
			DOCHOSTUIFLAG_NOTHEME                   = 0x00080000,
			DOCHOSTUIFLAG_NOPICS                    = 0x00100000,
			DOCHOSTUIFLAG_NO3DOUTERBORDER           = 0x00200000,
			DOCHOSTUIFLAG_DELEGATESIDOFDISPATCH     = 0x00400000
		};

		public __value enum CommandStateChangeConstants
		{
			CSC_UPDATECOMMANDS = 0xFFFFFFFF,
			CSC_NAVIGATEFORWARD = 0x00000001,
			CSC_NAVIGATEBACK = 0x00000002
		};


		public __value enum RefreshConstants
		{
			REFRESH_NORMAL		 = 0,
			REFRESH_IFEXPIRED	 = 1,
			REFRESH_CONTINUE	 = 2,
			REFRESH_COMPLETELY	 = 3
		};

		/*public __value enum BrowserNavConstants
		{
			navOpenInNewWindow = 0x1,
			navNoHistory = 0x2,
			navNoReadFromCache = 0x4,
			navNoWriteToCache = 0x8
			navAllowAutosearch = 0x10,
			navBrowserBar = 0x20,
			navHyperlink = 0x40
		};*/

		[ StructLayout( LayoutKind::Sequential )]
		public __gc struct DOCHOSTUIINFO
		{
			unsigned int cbSize;
			unsigned int dwFlags;
			unsigned int dwDoubleClick;
			[MarshalAs(UnmanagedType::BStr)] String* pchHostCss;
			[MarshalAs(UnmanagedType::BStr)] String* pchHostNS;
		};

		[StructLayout( LayoutKind::Sequential )]
		public __gc struct tagPOINT
		{
			long x;
			long y;
		};

		[StructLayout( LayoutKind::Sequential )]
		public __gc struct tagRECT
		{
			long nLeft;
			long nTop;
			long nRight;
			long nBottom;
		};

		[StructLayout( LayoutKind::Sequential )]
		public __gc struct tagMSG 
		{
			IntPtr hwnd;
			int lParam;
			unsigned int message;
			WebBrowser::tagPOINT* pt;
			unsigned int time;
			unsigned int wParam;
		};

		[ComImport, InterfaceType(ComInterfaceType::InterfaceIsIUnknown), 
			Guid("bd3f23c0-d43e-11cf-893b-00aa00bdce1a")]
		public __gc __interface IDocHostUIHandler 
		{
			[PreserveSig]
			void ShowContextMenu ( unsigned int dwID, tagPOINT* ppt,
				[MarshalAs(UnmanagedType::IUnknown)]Object* pcmdtReserved,
				[MarshalAs(UnmanagedType::IDispatch)]Object* pdispReserved );

			[PreserveSig]
			void GetHostInfo(DOCHOSTUIINFO* pInfo);

			[PreserveSig]
			void ShowUI( unsigned int dwID, 
				[MarshalAs(UnmanagedType::Interface)] IntPtr pActiveObject, 
				[MarshalAs(UnmanagedType::Interface)] IntPtr pCommandTarget, 
				[MarshalAs(UnmanagedType::Interface)] IntPtr pFrame,
				[MarshalAs(UnmanagedType::Interface)] IntPtr pDoc);

			[PreserveSig]
			void HideUI();

			[PreserveSig]
			void UpdateUI();

			[PreserveSig]
			void EnableModeless(int fEnable);

			[PreserveSig]
			void OnDocWindowActivate(int fActivate);

			[PreserveSig]
			void OnFrameWindowActivate(int fActivate);

			[PreserveSig]
			void ResizeBorder( tagRECT* prcBorder, 
				[MarshalAs(UnmanagedType::Interface)] IntPtr pUIWindow,
				int fRameWindow);

			[PreserveSig]
			void TranslateAccelerator( tagMSG* lpmsg, Guid* pguidCmdGroup, unsigned int nCmdID);
			
			[PreserveSig]
			void GetOptionKeyPath( String* pchKey, unsigned int dw);
			
			[PreserveSig]
			void GetDropTarget([MarshalAs(UnmanagedType::Interface)] IntPtr pDropTarget,
				[MarshalAs(UnmanagedType::Interface)] IntPtr ppDropTarget);

			[PreserveSig]
			void GetExternal( Object* ppDispatch);

			[PreserveSig]
			void TranslateUrl( unsigned int dwTranslate, unsigned short pchURLIn,
				IntPtr ppchURLOut);

			[PreserveSig]
			void FilterDataObject( System::Windows::Forms::IDataObject* pDO, System::Windows::Forms::IDataObject* ppDORet);
		};

		[ComImport, InterfaceType(ComInterfaceType::InterfaceIsIUnknown), 
			Guid("3050f3f0-98b5-11cf-bb82-00aa00bdce0b")]
		public __gc __interface ICustomDoc
		{
			[PreserveSig]
			void SetUIHandler(IDocHostUIHandler* pUIHandler);
		};

		public __gc class ContextMenuEventArgs : public EventArgs
		{
		public:
			// Constructor
			ContextMenuEventArgs::ContextMenuEventArgs( ContextMenuConstants eType, 
				System::Drawing::Point point, Object* obj1, Object* obj2 )
			{
				Type = eType;
				Point = point;
				Object1 = obj1;
				Object2 = obj2;
			}

		public:
			// Properties
			__property ContextMenuConstants get_Type () { return m_eType; }
			__property void set_Type ( ContextMenuConstants eType ) { m_eType = eType; }
			__property System::Drawing::Point get_Point () { return m_pointShow; }
			__property void set_Point ( System::Drawing::Point point ) { m_pointShow = point; }

			__property Object* get_Object1 () { return m_obj1; }
			__property void set_Object1 ( Object* obj1 ) { m_obj1 = obj1; }
			__property Object* get_Object2 () { return m_obj2; }
			__property void set_Object2 ( Object* obj2 ) { m_obj2 = obj2; }

		private:
			ContextMenuConstants m_eType;
			System::Drawing::Point m_pointShow;
			Object* m_obj1;
			Object* m_obj2;
		};

		public __delegate void ContextMenuEventHandler ( Object* sender, ContextMenuEventArgs* e  );

		public __gc class WebBrowserEx : public AxWebBrowser, 
			public IDocHostUIHandler
		{
		public:
			// Constructor
			WebBrowserEx::WebBrowserEx( Control* controlParent );

			void OnWebStateChange( Object* sender, DWebBrowserEvents2_CommandStateChangeEvent* e );
			// Navigate functions
			////////////////////////////////////////////////////
			void Navigate ( String* strUrl );
			void NavigateNW ( String* strUrl );

		private:
			// IDocHostUIHandler
			////////////////////////////////////////////////////
			void ShowContextMenu( unsigned int dwID, tagPOINT* ppt, 
				[MarshalAs(UnmanagedType::IUnknown)] Object* pcmdtReserved, 
				[MarshalAs(UnmanagedType::IDispatch)]Object* pdispReserved );
			void GetHostInfo( DOCHOSTUIINFO* pInfo );
			void 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 HideUI();
			void UpdateUI();
			void EnableModeless(int fEnable);
			void OnDocWindowActivate(int fActivate);
			void OnFrameWindowActivate(int fActivate);
			void ResizeBorder( tagRECT* prcBorder,
				[MarshalAs(UnmanagedType::Interface)] IntPtr pUIWindow, int fRameWindow);
			void TranslateAccelerator( tagMSG* lpmsg, Guid* pguidCmdGroup, unsigned int nCmdID);
			void GetOptionKeyPath( String* pchKey, unsigned int dw);
			void GetDropTarget( [MarshalAs(UnmanagedType::Interface)] IntPtr pDropTarget,
				[MarshalAs(UnmanagedType::Interface)] IntPtr ppDropTarget);
			void GetExternal( Object* ppDispatch );
			void TranslateUrl(unsigned int dwTranslate, unsigned short pchURLIn, IntPtr ppchURLOut);
			void FilterDataObject( System::Windows::Forms::IDataObject* pDO, System::Windows::Forms::IDataObject* ppDORet);

		public:
			// Event
			__event ContextMenuEventHandler* ContextMenu;

			// Properties
			__property bool get_CanGoBack () { return m_bCanGoBack; }
			__property bool get_CanGoForward () { return m_bCanGoForward; }

		private:
			bool m_bCanGoBack;
			bool m_bCanGoForward;
		};
	}
}

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