Click here to Skip to main content
15,885,141 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.1K   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            - WebBrowserPage.cpp
// Autor           - Michael Mac
// Contact         - GreenSequoia@wp.pl
// Description     - Implementation of the WebBrowserPage class
////////////////////////////////////////////////////

#include "stdafx.h"
#include "WebBrowserPage.h"


namespace MaChat
{
	////////////////////////////////////////////////////
	// MaChat::WebBrowserPage
	////////////////////////////////////////////////////
	WebBrowserPage::WebBrowserPage()
	{
		this->Resize += new EventHandler( this, OnResize );

		m_bProgressBar = false;
		m_bStatusBar = true;

		// Hider Label
		this->m_labelHider = new Label();
		this->Controls->Add( m_labelHider );

		// Initialize the WebBrowser
		this->m_browserWeb = new WebBrowser::WebBrowserEx( this );
		// Set EventHandlers
		this->m_browserWeb->CommandStateChange += new DWebBrowserEvents2_CommandStateChangeEventHandler( this, OnWebStateChange );
		this->m_browserWeb->ProgressChange += new DWebBrowserEvents2_ProgressChangeEventHandler( this, OnWebProgressChange );
		this->m_browserWeb->TitleChange += new DWebBrowserEvents2_TitleChangeEventHandler( this, OnWebTitleChange );
		this->m_browserWeb->NavigateComplete2 += new DWebBrowserEvents2_NavigateComplete2EventHandler( this, OnWebNavigateComplete );
		this->m_browserWeb->StatusTextChange += new DWebBrowserEvents2_StatusTextChangeEventHandler( this, OnWebStatusChange );
		// NOTE: We don't need to add this control to the panel becasue it's done in the constructor

		// Add a ProgressBar
		this->m_labelProgressBar = new Label();
		this->m_labelProgressBar->BorderStyle = BorderStyle::None;
		this->m_labelProgressBar->Visible = false;
		this->Controls->Add( m_labelProgressBar );

		// Add a StatusBar
		this->m_labelStatusBar = new Label();
		this->m_labelStatusBar->BorderStyle = BorderStyle::None;
		this->m_labelStatusBar->Visible = false;
		this->Controls->Add( m_labelStatusBar );
	}

	void WebBrowserPage::OnResize ( Object* sender, EventArgs* e )
	{
		if ( m_browserWeb )
		{
			this->m_browserWeb->SetBounds( -1, -1, this->Width + 2 ,
				( m_bProgressBar || m_bStatusBar ) ? this->Height - 12 : this->Height + 2 );

			this->m_labelHider->SetBounds( -1, this->m_browserWeb->Bottom - 1,
				this->Width, 1 );

			if ( m_bProgressBar )
			{
				this->m_labelProgressBar->SetBounds( 0, m_browserWeb->Bottom + 1,
					m_bStatusBar ? 50 : this->Width, 12 );
				this->m_labelProgressBar->Visible = true;
			}

			if ( m_bStatusBar )
			{
				this->m_labelStatusBar->SetBounds( m_bProgressBar ? 50 : 0, 
					m_browserWeb->Bottom + 1, m_bProgressBar ? this->Width - m_labelProgressBar->Width : this->Width, 12 );
				this->m_labelStatusBar->Visible = true;
			}
		}
	}

	// WebBrowser Handlers
	////////////////////////////////////////////////////
	void WebBrowserPage::OnWebStateChange( Object* sender, DWebBrowserEvents2_CommandStateChangeEvent* e )
	{
		if ( e->command == WebBrowser::CommandStateChangeConstants::CSC_NAVIGATEBACK )
		{
			if ( UpdateUI )
				UpdateUI->Invoke( this, new UpdateUIEventArgs( UpdateUIElement::Back ) );
		}
		if ( e->command == WebBrowser::CommandStateChangeConstants::CSC_NAVIGATEFORWARD )
		{
			if ( UpdateUI )
				UpdateUI->Invoke( this, new UpdateUIEventArgs( UpdateUIElement::Forward ) );
		}
	}

	void WebBrowserPage::OnWebProgressChange( Object* sender, DWebBrowserEvents2_ProgressChangeEvent* e )
	{
		double dProgress = e->progress;
		double dMax = e->progressMax;
		double dWidth = ( dProgress / dMax ) * 50;
		m_labelProgressBar->Width = dWidth;
	}

	void WebBrowserPage::OnWebTitleChange( Object* sender, DWebBrowserEvents2_TitleChangeEvent* e )
	{
		// UpdateUI
		if ( UpdateUI )
			UpdateUI->Invoke( this, new UpdateUIEventArgs( UpdateUIElement::Title ) );
	}

	/*void WebBrowserPage::OnWebBeforeNavigate ( Object* sender, DWebBrowserEvents2_BeforeNavigate2Event* e )
	{
	}*/

	void WebBrowserPage::OnWebStatusChange ( Object* sender, DWebBrowserEvents2_StatusTextChangeEvent* e )
	{
		m_labelStatusBar->Text = e->text;
	}

	void WebBrowserPage::OnWebNavigateComplete ( Object* sender, DWebBrowserEvents2_NavigateComplete2Event* e )
	{
		if ( UpdateUI )
			UpdateUI->Invoke( this, new UpdateUIEventArgs( UpdateUIElement::Address ) );
	}

	/*void WebBrowserPage::OnWebDocumentComplete ( Object* sender, DWebBrowserEvents2_DocumentCompleteEvent* e )
	{	
	}*/
}

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