c#창 버클 채팅--로그인에서 채팅(1)

LonginU에 로그인합니다.cs 클래스 파일
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myqq
{
    class LonginU
    {
        public int qq { get; set; }
        public string LoginPwd { get; set; }
        public string NickName { get; set; }
        public int faceid { get; set; }
        public string sex { get; set; }
        public int age { get; set; } 
        public string name { get; set; }
    }
}

로그인 구현
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace myqq
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            string sql = String.Format("select * from Users where Id={0} and loginpwd='{1}'",textBox1.Text,textBox2.Text);
            DBHelper.conn.Open();
            SqlCommand cmd = new SqlCommand(sql, DBHelper.conn);
            SqlDataReader rd= cmd.ExecuteReader();
            
            Boolean flag = false;
            while (rd.Read())
            {                
                DBHelper.loginU.qq = Convert.ToInt32(rd["Id"]);
                DBHelper.loginU.NickName = rd["NickName"].ToString();
                DBHelper.loginU.faceid = Convert.ToInt32(rd["FaceId"]);
                flag = true;

            }
            DBHelper.conn.Close();
            if (flag)
            {
                
                this.Visible = false;
                Main m = new Main();
                m.Show();
            }
            else
            {
                MessageBox.Show("        !");
            }
        }
    }
}

친구 목록
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Aptech.UI;

namespace myqq
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            SelectPhoto selectPhoto = new SelectPhoto();
            selectPhoto.Show();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            DBHelper.conn.Open();
            string sql = string.Format("update Users set Online=1 where Users.Id={0}", DBHelper.loginU.qq);
            SqlCommand com = new SqlCommand(sql, DBHelper.conn);
            com.ExecuteNonQuery();
            DBHelper.conn.Close();
            this.pictureBox1.Image = imageList1.Images[DBHelper.loginU.faceid];
            this.label1.Text = DBHelper.loginU.NickName;
            sideBar1.AddGroup("    ");
            sideBar1.AddGroup("   ");
            ShowMyFriends();
            timer1.Start();
            timer2.Start();
        }

        private void ShowMyFriends()
        {
            String Sql = String.Format("select * from Users,Friends where HostId={0} and Users.Id=FriendId and online=1",DBHelper.loginU.qq);
            DBHelper.conn.Open();
            SqlCommand cmd = new SqlCommand(Sql, DBHelper.conn);
            SqlDataReader rd = cmd.ExecuteReader();
            while(rd.Read())
            {
                int i = 0;
                string friendname = rd["NickName"].ToString();
                int faceid = Convert.ToInt32(rd["FaceId"]);
                SbItem sb = new SbItem(friendname, faceid);
                sb.Tag = Convert.ToInt32(rd["Id"]);
                sideBar1.Groups[i].Items.Add(sb);
                i++;
            }
            DBHelper.conn.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //      
            sideBar1.Groups[0].Items.Clear();
            ShowMyFriends();
        }
        public int massageId;
        public int sendfaceID;
        public string sendnickname;
        private void timer2_Tick(object sender, EventArgs e)
        {
            //          
            DBHelper.conn.Close();
            string sql = string.Format("select * from Messages,Users  where ToUserId={0} and MessageTypeId=1 and Messages.FromUserId=Users.Id",DBHelper.loginU.qq);
            DBHelper.conn.Open();
            SqlCommand cm = new SqlCommand(sql,DBHelper.conn);
            SqlDataReader rd= cm.ExecuteReader();
            while (rd.Read())
            {
                massageId = Convert.ToInt32(rd["FromUserId"]);
                sendfaceID = Convert.ToInt32(rd["faceId"]);
                sendnickname = Convert.ToString(rd["NickName"]);
                if (Convert.ToInt32(rd["MessageState"]) == 0)
                {
                    SoundPlayer sp = new SoundPlayer("F:\\     \\myQQ\\qq  \\qq  \\msg.wav");
                    sp.Play();
                    shantu();
                }
                   
            }
            DBHelper.conn.Close();            
        }
       //        
        private void shantu()
        {
            for(int i = 0; i < sideBar1.Groups[0].Items.Count; i++)
            {
                if(Convert.ToInt32(sideBar1.Groups[0].Items[i].Tag)== massageId)
                {
                    if (sideBar1.Groups[0].Items[i].ImageIndex < 100)
                    {
                        sideBar1.Groups[0].Items[i].ImageIndex = 100;
                    }
                    else
                    {
                        sideBar1.Groups[0].Items[i].ImageIndex = sendfaceID;
                    }
                    sideBar1.Invalidate();
                }
            }
        }
        

        
        private void sideBar1_ItemDoubleClick(SbItemEventArgs e)
        {
            chat c = new chat();
            c.friendqq = massageId;
            c.nickname = sendnickname;
            c.faceId = sendfaceID;
            c.Show();
        }
        //      
        private void Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            DBHelper.conn.Open();
            string sql = string.Format("update Users set Online=0 where Users.Id={0}", DBHelper.loginU.qq);
            SqlCommand com = new SqlCommand(sql, DBHelper.conn);
            com.ExecuteNonQuery();
            DBHelper.conn.Close();
        }
    }
}

채팅
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace myqq
{
    public partial class chat : Form
    {
        public chat()
        {
            InitializeComponent();
        }
        public int friendqq;
        public int faceId;
        public string nickname;

        private void chat_Load(object sender, EventArgs e)
        {
            label1.Text = nickname;
            pictureBox1.Image = imageList1.Images[faceId];
            getmessage();
            timer1.Start();
        }

        private void getmessage()
        {
            DBHelper.conn.Open();
            string sql = string.Format("select * from Messages where FromUserId = {0} and ToUserId = {1} AND MessageTypeId = {2} AND MessageState = {3} ",friendqq,DBHelper.loginU.qq,1,0);
            SqlCommand cm = new SqlCommand(sql, DBHelper.conn);
            SqlDataReader rd=cm.ExecuteReader();
            while(rd.Read())
            {
                richTextBox1.Text += string.Format("{0} :{1}\r
", nickname, rd["message"].ToString()); } DBHelper.conn.Close(); DBHelper.conn.Open(); string Updatasql = string.Format("update Messages set MessageState=1 where FromUserId={0} and ToUserId={1} and MessageTypeId={2}",friendqq,DBHelper.loginU.qq,1); cm = new SqlCommand(Updatasql, DBHelper.conn); cm.ExecuteNonQuery(); DBHelper.conn.Close(); } private void button3_Click(object sender, EventArgs e) { if (textBox1.Text == "") { return; } DateTime d = DateTime.Now; DBHelper.conn.Open(); string sql = string.Format("insert into Messages values({0},{1},'{2}',{3},{4},'{5}')",DBHelper.loginU.qq,friendqq,textBox1.Text,1,0,d.ToString("yyyy-MM-dd hh:mm:ss")); SqlCommand com = new SqlCommand(sql,DBHelper.conn); com.ExecuteNonQuery(); DBHelper.conn.Close(); richTextBox1.Text += string.Format("{0} :{1}\r
", DBHelper.loginU.NickName, textBox1.Text); textBox1.Text = ""; } private void timer1_Tick(object sender, EventArgs e) { getmessage(); } } }

이상은 로그인 채팅 기능만 실현했고 추후에 이미지 수정, 개인 자료와 친구를 검색하여 친구로 추가하는 기능을 따라갈 것입니다.
 
 
 
예쁘게 생겨서 고마워요.
공부도 이렇게 잘하고.
그리고 제 블로그를 좋아해요.
 
 
모르는 게 있으면 댓글로 질문해 주세요!!

좋은 웹페이지 즐겨찾기