Click here to Skip to main content
15,886,137 members
Articles / Web Development / ASP.NET

Chat Application using Web services in C#

Rate me:
Please Sign up or sign in to vote.
4.85/5 (38 votes)
15 May 2008CPOL5 min read 383.6K   41.8K   139  
This is a cool chat application created in DotNet using web services having all the functionalities.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ChatClient.ChatServices;
using System.Runtime.InteropServices;

namespace ChatClient
{
    public partial class PrivateMessage : Form
    {
        [DllImport("user32.dll")]
        static extern Int32 FlashWindowEx(ref FLASHWINFO pwfi);

        [StructLayout(LayoutKind.Sequential)]
        public struct FLASHWINFO
        {
            public UInt32 cbSize;
            public IntPtr hwnd;
            public UInt32 dwFlags;
            public UInt32 uCount;
            public UInt32 dwTimeout;
        }

        //Stop flashing. The system restores the window to its original state. 
        public const UInt32 FLASHW_STOP = 0;
        //Flash the window caption. 
        public const UInt32 FLASHW_CAPTION = 1;
        //Flash the taskbar button. 
        public const UInt32 FLASHW_TRAY = 2;
        //Flash both the window caption and taskbar button.
        //This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags. 
        public const UInt32 FLASHW_ALL = 3;
        //Flash continuously, until the FLASHW_STOP flag is set. 
        public const UInt32 FLASHW_TIMER = 4;
        //Flash continuously until the window comes to the foreground. 
        public const UInt32 FLASHW_TIMERNOFG = 12; 

        ChatService chat = new ChatService();
        private string strUser;
        private Font font;
        public PrivateMessage(string strUserName)
        {
            InitializeComponent();
            strUser = strUserName;
        }

        public PrivateMessage(string strUserName, string strMg)
        {
            InitializeComponent();
            strUser = strUserName;
            string[] strU = Form1.strMess.Split(':');
            string strM = string.Empty;
            
            for (int i = 1; i < strU.Length; i++)
            {
                strM = strM + strU[i];
            }
            rtbMessage.SelectedText = strU[0] + ": ";
            rtbMessage.SelectedRtf = strM;
            Form1.strMess = "";
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (rtbWriteMsg.Text.Trim().Length > 0)
            {
                chat.SendMessage(Form1.strMe, strUser, rtbWriteMsg.Rtf);
                rtbMessage.SelectedText = Form1.strMe + ": ";
                rtbMessage.SelectedRtf = rtbWriteMsg.Rtf;
                rtbMessage.ScrollToCaret();
                rtbWriteMsg.Clear();
                rtbWriteMsg.Focus();
            }
        }

        private void PrivateMessage_Load(object sender, EventArgs e)
        {            
            timer1.Enabled = true;
            Text = strUser;
        }

        private void PrivateMessage_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form1.arrOUsers.Remove(strUser);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Form1.strMess.Trim().Length > 0)
            {
                string[] strU = Form1.strMess.Split(':');
                string strM = string.Empty;
                if (strU[0] == strUser)
                {
                    for (int i = 1; i < strU.Length; i++)
                    {
                        strM = strM + strU[i];
                    }
                    rtbMessage.SelectedText = strU[0] + ": ";
                    rtbMessage.SelectedRtf = strM;
                    Form1.strMess = "";                    
                    rtbMessage.ScrollToCaret();
                    FlashWindowEx(this);
                }
            }
        }    

        public static bool FlashWindowEx(Form frm)
        {
            IntPtr hWnd = frm.Handle;
            FLASHWINFO fInfo = new FLASHWINFO();

            fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
            fInfo.hwnd = hWnd;
            fInfo.dwFlags = FLASHW_ALL | FLASHW_TIMERNOFG;
            fInfo.uCount = UInt32.MaxValue;
            fInfo.dwTimeout = 0;

            return (FlashWindowEx(ref fInfo) == 0);
        }

        private void rtbWriteMsg_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                if (rtbWriteMsg.Text.Trim().Length > 0)
                {
                    chat.SendMessage(Form1.strMe, strUser, rtbWriteMsg.Rtf);
                    rtbMessage.SelectedText = Form1.strMe + ": ";
                    rtbMessage.SelectedRtf = rtbWriteMsg.Rtf;
                    rtbMessage.ScrollToCaret();
                    rtbWriteMsg.Clear();
                    rtbWriteMsg.Focus();
                }
            }
        }

        private void btnFont_Click(object sender, EventArgs e)
        {
            fontDialog1.ShowDialog();
            font = new Font(fontDialog1.Font.Name, fontDialog1.Font.Size, fontDialog1.Font.Style);
            rtbWriteMsg.Font = font;
            rtbWriteMsg.Focus();
            font.Dispose();
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            rtbWriteMsg.ForeColor = colorDialog1.Color;
            rtbWriteMsg.Focus();
        }       
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United States United States
He has a total experience of more than 6 years in programming on different technologies. Currently working as a Systems Analyst.

Comments and Discussions