2017-5-5 QQ面板 (用户控件timer控件,轮询实现聊天功能)

Posted Zoe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017-5-5 QQ面板 (用户控件timer控件,轮询实现聊天功能)相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 用户控件练习___QQ面板.App_code;

namespace 用户控件练习___QQ面板
{
    public partial class Form1 : Form
    {
        public Form1(Form2 f2,qquser u)
        {
            InitializeComponent();
            //绑定当前登录用户的信息
            pictureBox1.BackgroundImage = Image.FromFile(u.Qqpic);
            label1.Text = u.Qqname;
            label2.Text = u.Qqsign;
            //绑定好友信息
          qqfriend fri= new qqfrienddata().selectfriend(u.Qqnumber);
            if(fri!=null)
            {
              string[] strs = fri.Qqfriends.Split(\',\');
                foreach(string s in strs)
                {
                    qquser u1 = new qquserdata().selectuser(s);
                    if(u1!=null)
                    {
                        friend f = new friend(u,u1);
                        f.pic1.BackgroundImage = Image.FromFile(u1.Qqpic);
                        f.la1.Text = u1.Qqname;
                        f.la2.Text = u1.Qqsign;
                        flowLayoutPanel1.Controls.Add(f);
                    }
                }
            }
            label3.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
           
        }

        //private void button1_Click(object sender, EventArgs e)
        //{
        //    friend f = new friend();
        //    f.pic1.BackgroundImage = Image.FromFile("img/1.jpg.jpg");
        //    f.la1.Text = "姚慧旭";
        //    f.la2.Text = "套路得人心";
        //    flowLayoutPanel1.Controls.Add(f);
        //}

        private void timer1_Tick(object sender, EventArgs e)
        {
            label3.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 用户控件练习___QQ面板.App_code;

namespace 用户控件练习___QQ面板
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            qquser u=new qquserdata().selectuser(textBox1.Text.Trim(),textBox2.Text);
            if(u==null)
            {
                label1.Text = "账号或密码错误!";
                return;
            }
            if (u.Qqstate)
            {
                Form1 f1 = new Form1(this,u);
                f1.Show();
                this.Hide();

            }
            else 
            {
                label1.Text = "账号未激活!";
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 用户控件练习___QQ面板.App_code;

namespace 用户控件练习___QQ面板
{
    public partial class Form3 : Form
    {
        qquser ME = null;
        qquser TO = null;
        friend F = null;
        public Form3(qquser me,qquser to,friend f)
        {
            InitializeComponent();
            this.Text = me.Qqname + "" + to.Qqname + "的对话";
            ME = me;
            TO = to;
            F = f;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            qqchat cht = new qqchat();
            cht.From = ME.Qqnumber;
            cht.To = TO.Qqnumber;
            cht.Content = richTextBox2.Text;
            cht.Time = DateTime.Now;
            cht.State = false;

            new qqchatdata().insertchat(cht);
            richTextBox1.Text+=ME.Qqname+""+TO.Qqname+"说:("+cht.Time.ToString("HH:mm:ss")+")\\r"+richTextBox2.Text+"\\r";
            richTextBox2.Text = "";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

          qqchat cht= new qqchatdata().select(TO.Qqnumber, ME.Qqnumber);
            if(cht!=null)
            {
                richTextBox1.Text += cht.From + "" + cht.To + "说:(" + cht.Time.ToString("HH:mm:ss") + ")\\r"+cht.Content+"\\r";

                new qqchatdata().updatestate(cht.Ids);
            }

        }

        private void Form3_FormClosing(object sender, FormClosingEventArgs e)
        {
            F.ff3 = null;
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 用户控件练习___QQ面板.App_code;

namespace 用户控件练习___QQ面板
{
    public partial class friend : UserControl
    {
        qquser ME = null;
        qquser TO = null;
        public friend(qquser me,qquser to)
        {
            InitializeComponent();
            ME = me;
            TO = to;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void friend_MouseEnter(object sender, EventArgs e)
        {
            this.BackColor = Color.Red;

        }

        private void friend_MouseLeave(object sender, EventArgs e)
        {
            this.BackColor = Color.Transparent;
        }

      public Form3 ff3 = null;
        private void friend_DoubleClick(object sender, EventArgs e)
        {
            if (ff3 == null)
            {
                Form3 f3 = new Form3(ME, TO,this);
                f3.Show();
                ff3 = f3;
            }
            else 
            {
                ff3.WindowState = FormWindowState.Normal;
                ff3.Focus();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (new qqchatdata().select(TO.Qqnumber, ME.Qqnumber) != null)
            {
                timer2.Enabled = true;
            }
            else 
            {
                timer2.Enabled = false;
            }
        }
        bool top = false;
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (top)
            {
                this.pic1.Location = new Point(9, 13);
                top = false;
            }
            else 
            {
                this.pic1.Location = new Point(15, 19);
                top = true;
            }
        }
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 用户控件练习___QQ面板.App_code
{
    public class qqchat
    {
        private int _ids;

        public int Ids
        {
            get { return _ids; }
            set { _ids = value; }
        }
        private string _from;

        public string From
        {
            get { return _from; }
            set { _from = value; }
        }
        private string _to;

        public string To
        {
            get { return _to; }
            set { _to = value; }
        }
        private string _content;

        public string Content
        {
            get { return _content; }
            set { _content = value; }
        }
        private DateTime _time;

        public DateTime Time
        {
            get { return _time; }
            set { _time = value; }
        }
        private bool _state;

        public bool State
        {
            get { return _state; }
            set { _state = value; }
        }

    }
}
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 用户控件练习___QQ面板.App_code
{
     public class qqchatdata
    {
         SqlConnection conn=null;
         SqlCommand cmd=null;
         public qqchatdata() 
         {
             conn = new SqlConnection("server=.;database=master;user=sa;pwd=123");
             cmd = conn.CreateCommand();
         }
         public void insertchat(qqchat ct)
         {
             cmd.CommandText = "insert into qqchat values (@a,@b,@c,@d,@e)";
             cmd.Parameters.Clear();
             cmd.Parameters.AddWithValue("@a",ct.From);
             cmd.Parameters.AddWithValue("@b",ct.To);
             cmd.Parameters.AddWithValue("@c",ct.Content);
             cmd.Parameters.AddWithValue("@d",ct.Time);
             cmd.Parameters.AddWithValue("@e",ct.State);

             conn.Open();
             cmd.ExecuteNonQuery();
             conn.Close();
         }

         public qqchat select(string from,string to)
         {
             qqchat ct = null;
             cmd.CommandText = "select * from qqchat where [from]=@a and [to]=@b and [state]=0";
             cmd.Parameters.Clear();
             cmd.Parameters.AddWithValue("@a",from);
             cmd.Parameters.AddWithValue("@b",to);
             conn.Open();
             SqlDataReader dr= cmd.ExecuteReader();
             if(dr.HasRows)
             {
                 ct = new qqchat();
                 dr.Read();
                 ct.Ids = Convert.ToInt32(dr[0]);
                 ct.From = dr[1].ToString();
                 ct.To = dr[2].ToString();
                 ct.Content=dr[3].ToString();
                 ct.Time = Convert.ToDateTime(dr[4]);
                 ct.State = Convert.ToBoolean(dr[5]);
             }
             conn.Close();
             return ct;
         }


         public void updatestate(int ids)
         {
             cmd.CommandText = "update qqchat set state=1 where ids=@a";
             cmd.Parameters.Clear();
             cmd.Parameters.AddWithValue("@a",ids);
             conn.Open();
             cmd.ExecuteNonQuery();
             conn.Close();
         }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 用户控件练习___QQ面板.App_code
{
   public class qqfriend
    {
        private string _qqnumber;

        public string Qqnumber
        {
            get { return _qqnumber; }
            set { _qqnumber = value; }
        }
        private string _qqfriends;

        public string Qqfriends
        {
            get { return _qqfriends; }
            set { _qqfriends = value; }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 用户控件练习___QQ面板.App_code
{
     public class qqfrienddata
    {
         SqlConnection conn=null;
         SqlCommand cmd=null;

         public qqfrienddata() 
         {
             conn = new SqlConnection("server=.;database=master;user=sa;pwd=123");
             cmd = conn.CreateCommand();
         }
         public qqfriend selectfriend(string number)
         {
             qqfriend f = null;
             cmd.CommandText = "select * from qqfriends where qqnumber=@a ";
             cmd.Parameters.Clear();
             cmd.Parameters.AddWithValue("@a",number);
             conn.Open();
             SqlDataReader dr = cmd.ExecuteReader();
             if(dr.HasRows)
             {
                 f = new qqfriend();
                 dr.Read();
                 f.Qqnumber = dr[1].ToString();
                 f.Qqfriends = dr[2].ToString();
             }

             conn.Close();
             return f;

         }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 用户控件练习___QQ面板.App_code
{
    public class qquser
    {
        private string _qqnumber;

        public string Qqnumber
        {
            get { return _qqnumber; }
            set { _qqnumber = value; }
        }
        private string _qqpwd;

        public string Qqpwd
        {
            get { return _qqpwd; }
            set { _qqpwd = value; }
        }
        private string _qqname;

        public string Qqname
        {
            get { return _qqname; }
            set { _qqname = value; }
        }
        private string _qqsign;

        public string Qqsign
        {
            get { return _qqsign; }
            set { _qqsign = value; }
        }
        private string _qqpic;

        public string Qqpic
        {
            get { return _qqpic; }
            set { _qqpic = value; }
        }
        private bool _qqstate;

        public bool Qqstate
        {
            get { return _qqstate; }
            set { _qqstate = value; }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 用户控件练习___QQ面板.App_code
{
    public class qquserdata
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;

        public qquserdata() 
        {
            conn = new SqlConnection("server=.;database=master;user=sa;pwd=123");
            cmd = conn.CreateCommand();
        }
        public qquser selectuser(string number,string pwd)
        {
            qquser u = null;
            cmd.CommandText = "select * from qquser where qqnumber=@a and qqpwd=@b";
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@a",number);
            cmd.Parameters.AddWithValue("@b",pwd);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if(dr.HasRows)
            {
                u = new qquser();
                dr.Read();
                u.Qqnumber = dr[1].ToString();
                u.Qqpwd = dr[2].ToString();
                u.Qqname = dr[3].ToString();
                u.Qqsign = dr[4].ToString();
                u.Qqpic = dr[5].ToString();
                u.Qqstate = Convert.ToBoolean(dr[6]);
            }
            conn.Close();



            return u;
        }


        public qquser selectuser(string number)
        {
            qquser u = null;
            cmd.CommandText = "select * from qquser where qqnumber=@a ";
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@a", number);
            
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                u = new qquser();
                dr.Read();
                u.Qqnumber = dr[1].ToString();
                u.Qqpwd = dr[2].ToString();
                u.Qqname = dr[3].ToString();
                u.Qqsign = dr[4].ToString();
                u.Qqpic = dr[5].ToString();
                u.Qqstate = Convert.ToBoolean(dr[6]);
            }
            conn.Close();



            return u;
        }
    }
}

 

以上是关于2017-5-5 QQ面板 (用户控件timer控件,轮询实现聊天功能)的主要内容,如果未能解决你的问题,请参考以下文章

需要鼠标滚动滚动用户控件

在jquery中禁用当前日期之前的日期并且输入在用户控件中并且用户控件在更新面板中

winforms - 表单加载期间带有dockstyle填充的用户控件大小不正确

更新面板内动态添加的用户控件内部的Javascript函数未定义

ASP.NET 在嵌套在更新面板中的用户控件中注入 javascript

C ++如何将用户控件添加到表单中以便我可以在面板之间进行转换