Click here to Skip to main content
15,893,668 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.
// This script was created by Michael Mac and can be used as a freeware
var m_arMessages = new Array(0);
var m_nCurMsg = 0;
var m_strNoMoreMsg = "";
function AddMessage( strTitle, strText )
{
	// Create a new bigger array
	var array = new Array( m_arMessages.length + 2 );
	
	// Copy the old messages
	for( i=0; i<m_arMessages.length; i++ )
		array[i] = m_arMessages[i];
	
	// Add a new message at the end
	array[array.length-2] = strTitle;
	array[array.length-1] = strText;
			
	m_arMessages = array;
}
		
function Remove( nPos )
{
	// Create a new smaller array
	var array = new Array( m_arMessages.length - 2 );

	var nAddIndex=0;
	// Copy  messages
	for( i=0; i<m_arMessages.length; i++ )
	{
		if ( i == (nPos*2) || i == (nPos*2+1) )
			continue;
		
		array[nAddIndex] = m_arMessages[i];
		nAddIndex++;
	}
			
	m_arMessages = array;
						
	if ( m_nCurMsg >=1  )
	{
		m_nCurMsg--;
		ShowMsg( m_nCurMsg );
	}
	else
		ShowMsg( m_nCurMsg );
}
		
function ShowMsg( nMsg )
{
	if ( m_arMessages.length == 0 )
	{
		// Disable back, next and remove buttons
		document.getElementById( "back" ).style.visibility = "hidden";
		document.getElementById( "next" ).style.visibility = "hidden";
		document.getElementById( "remove" ).style.visibility = "hidden";

		// Show appropriate message
		text.innerHTML = m_strNoMoreMsg;
		title.innerHTML = m_strNoMoreMsg;
		info.innerHTML = "0/0";
		show();
		return;
	}
			
	m_nCurMsg = nMsg;
	title.innerHTML = m_arMessages[nMsg*2];
	text.innerHTML = m_arMessages[nMsg*2 + 1];
	show();
			
	// Enable or disable buttons
	if ( nMsg == 0 )
		document.getElementById( "back" ).style.visibility = "hidden";
	else
		document.getElementById( "back" ).style.visibility = "visible";
	
	if ( nMsg >= ( m_arMessages.length / 2 ) - 1 )
		document.getElementById( "next" ).style.visibility = "hidden";
	else
		document.getElementById( "next" ).style.visibility = "visible";
				
	// Set info
	info.innerHTML = ( m_nCurMsg + 1 ) + "/" + ( m_arMessages.length / 2 );
}
		
function Back()
{
	m_nCurMsg--;
	ShowMsg( m_nCurMsg );
}
		
function Next()
{
	m_nCurMsg++;
	ShowMsg( m_nCurMsg );
}

function SetNoMoreMsgString( strText )
{
	m_strNoMoreMsg = strText;
}

// This part of the script is a modification of the orginal one which you can download from www.dynamicdrive.com

var bouncelimit = 32;

function show()
{	
	// Start
	crossobj = document.getElementById( "msg" ).style;
	crossobj.left = 45;
	scroll_top = document.body.scrollTop;
	crossobj.top = scroll_top - 250;
	crossobj.visibility = "visible";
	dropstart = setInterval( "dropin()", 50 );
}

function dropin()
{
	scroll_top = document.body.scrollTop;
	if ( parseInt( crossobj.top ) < 100 + scroll_top )
		crossobj.top = parseInt( crossobj.top ) + 40;
	else
	{
		clearInterval( dropstart );
		bouncestart = setInterval( "bouncein()" , 50) ;
	}
}

function bouncein()
{
	crossobj.top = parseInt( crossobj.top ) - bouncelimit;
	if ( bouncelimit < 0 )
		bouncelimit += 8;
	bouncelimit = bouncelimit * -1;
	if ( bouncelimit == 0 )
		clearInterval( bouncestart );
}

function dismissbox()
{
	if ( window.bouncestart )
		clearInterval( bouncestart );
		
	bouncelimit = 32; //(must be divisible by 8)
	crossobj.visibility = "hidden";
	clearInterval( dropstart );
	clearInterval( bouncestart );
}

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