Mqq主界面

来源:互联网 发布:js文本框内容改变事件 编辑:程序博客网 时间:2024/04/28 07:37

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aptech.UI;
using System.Data.SqlClient;
using System.Media;

namespace MyQQ
{
    public partial class MainForm : Form
    {
       
        int fromUserId;   // 消息的发起者
        int friendFaceId;  // 发消息的好友的头像Id 
        int messageindex = 0;  //消息图片索引
        public PictureBox ture = null;
        public string id;//第一次登录窗体的id
        public MainForm()
        {
            InitializeComponent();
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            ////窗体渐变
            //double i;
            //for ( i = 0.5; i <=1.0; i+=0.05)
            //{
            //    this.Opacity = i;
            //    System.Threading.Thread.Sleep(300);
            //}
            //this.Opacity = i;
            //在窗体中显示的登录号码
            this.Text = UserHeper.loginId.ToString();
            //获取登陆用户的昵称
            this.lblNickName.Text = UserHeper.nickName + "(" + UserHeper.loginId.ToString() + ")";
            //将SIdebar分为两组
            sbFridends.AddGroup("我的好友");
            sbFridends.AddGroup("陌生人");
            //显示登陆用户的头项
            picUserFace.Image = ilface.Images[UserHeper.faceid];
            //好友列表
            friends();
        }
        //好友列表方法
        private void friends()
        {
            string sql = "select id,faceid,nickname from users where id in(select friendid from friends where hostid='" + UserHeper.loginId + "')";
            SqlCommand com = new SqlCommand(sql, DBHelper.connection);
            try
            {
                DBHelper.connection.Open();
                SqlDataReader data = com.ExecuteReader();
                if (data.HasRows)
                {
                    while (data.Read())
                    {
                        string friendid = data["id"].ToString();
                        int faceid = Convert.ToInt32(data["faceid"].ToString());
                        string nickName = data["nickname"].ToString();
                        //创建Sidebar项
                        SbItem item = new SbItem(nickName, faceid);
                        //把所有查找的好友id存到TAg中
                        item.Tag = friendid;
                        //把查找出来的好友添加到Sidebar中
                        this.sbFridends.Groups[0].Items.Add(item);
                    }
                    data.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }
        }

        //个人信息
        private void tsttext_Click(object sender, EventArgs e)
        {
            PersonalInfoForm ni = new PersonalInfoForm();
            ni.picture = this.picUserFace;
            ni.Show();
        }
        //查找好友
        private void tstfriend_Click(object sender, EventArgs e)
        {
            SearchFriendForm no = new SearchFriendForm();
            no.Show();
        }
        //退出
        private void tstexit_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result == DialogResult.OK)
            {
                Application.Exit();
            }
        }
        //小头像
        private void littleface_Click(object sender, EventArgs e)
        {
            sbFridends.View = SbView.SmallIcon;
        }
        //加为好友
        private void AddFriend_Click(object sender, EventArgs e)
        {
            int result = 0;  // 操作结果
            string sql = string.Format(
                "INSERT INTO Friends (HostId, FriendId) VALUES({0},{1})",
                UserHeper.loginId, Convert.ToInt32(sbFridends.SeletedItem.Tag));
            try
            {
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                result = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }
            if (result == 1)
            {
                MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                sbFridends.SeletedItem.Parent.Items.Remove(sbFridends.SeletedItem);
                sbFridends.Groups[0].Items.Clear();
                friends(); // 更新好友列表            
            }
            else
            {
                MessageBox.Show("添加失败,请稍候再试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //删除
        private void delete_Click(object sender, EventArgs e)
        {
            DialogResult resut = MessageBox.Show("确定要删除吗?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
            if (resut == DialogResult.Yes)
            {
                sbFridends.SeletedItem.Parent.Items.Remove(sbFridends.SeletedItem);
                int friendsid = Convert.ToInt32(sbFridends.SeletedItem.Tag);
                string sql = string.Format("delete from friends where hostid ='{0}' and friendid ='{1}'", UserHeper.loginId, friendsid);
                SqlCommand com = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                int result = Convert.ToInt32(com.ExecuteNonQuery());
                DBHelper.connection.Close();
            }
        }
        //刷新好友列表
        private void tstupdate_Click(object sender, EventArgs e)
        {
            sbFridends.Groups[0].Items.Clear();
            friends();
        }
        //大头像
        private void bigFace_Click(object sender, EventArgs e)
        {
            sbFridends.View = SbView.LargeIcon;
        }
        //控制喇叭闪烁
        private void timer2_Tick(object sender, EventArgs e)
        {
            messageindex = messageindex == 0 ? 1 : 0;
            tstxiaoxi.Image = lstMessage.Images[messageindex];
        }
        //系统消息
        private void tstxiaoxi_Click(object sender, EventArgs e)
        {
            this.timer2.Stop();
            messageindex = 0;
            tstxiaoxi.Image = lstMessage.Images[messageindex];
            RequestForm of = new RequestForm();
            of.Show();
        }
        //跳转图片框
        private void picUserFace_Click(object sender, EventArgs e)
        {
            FacesForm vo = new FacesForm();
            vo.pic = this.picUserFace;
            vo.Show();
        }
 
        //头像
        private void timer3_Tick(object sender, EventArgs e)
        {
            // 循环好友列表两个组中的每个item,找到发消息的好友,让他的头像闪烁
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < sbFridends.Groups[i].Items.Count; j++)
                {
                    if (Convert.ToInt32(sbFridends.Groups[i].Items[j].Tag) == this.fromUserId)
                    {
                        if (sbFridends.Groups[i].Items[j].ImageIndex < 101)
                        {
                            sbFridends.Groups[i].Items[j].ImageIndex = 101;// 索引为101的图片是一个空白图片
                        }
                        else
                        {
                            sbFridends.Groups[i].Items[j].ImageIndex = this.friendFaceId;
                        }
                        sbFridends.Invalidate();  // 重新绘制
                    }
                }
            }
        }
        //扫描计时器
        private void timer1_Tick(object sender, EventArgs e)
        {
            sbFridends.Groups[0].Items.Clear();
            friends();
            int messageTypeId = 1;  // 消息类型
            int messageState = 1;   // 消息状态
            // 找出未读消息对应的好友Id
            string sql = string.Format(
                "SELECT Top  1  FromUserId, MessageTypeId, MessageState FROM Messages WHERE ToUserId={0} AND MessageState=0", UserHeper.loginId);
           SqlCommand command;
            // 消息有两种类型:聊天消息、添加好友消息
            try
            {
                command = new SqlCommand(sql, DBHelper.con);
                DBHelper.con.Open();
                SqlDataReader dataReader = command.ExecuteReader();

                // 循环读出一个未读消息
                while (dataReader.Read())
                {
                    this.fromUserId = (int)dataReader["FromUserId"];
                    messageTypeId = (int)dataReader["MessageTypeId"];
                    messageState = (int)dataReader["MessageState"];
                }
                dataReader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelper.con.Close();
            }

            //判断消息类型,如果是添加好友消息,就启动喇叭timer,让小喇叭闪烁
            if (messageTypeId == 2 && messageState == 0)
            {
                SoundPlayer player = new SoundPlayer("system.wav");
                player.Play();
                timer2.Start();
            }
            // 如果是聊天消息,就启动聊天timer,让好友头像闪烁
            if (messageTypeId == 1 && messageState == 0)
            {
                // 获得发消息的人的头像Id
                sql = "SELECT FaceId FROM Users WHERE Id=" + this.fromUserId;
                try
                {
                    command = new SqlCommand(sql, DBHelper.con);
                    DBHelper.con.Open();
                    this.friendFaceId = Convert.ToInt32(command.ExecuteScalar());   // 设置发消息的好友的头像索引
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    DBHelper.con.Close();
                }

                // 如果发消息的人没有在列表中就添加到陌生人列表中
                if (!HasShowUser(fromUserId))
                {
                    UpdateStranger(fromUserId);
                }
                SoundPlayer player = new SoundPlayer("msg.wav");
                player.Play();
                timer3.Start(); // 启动闪烁头像定时器
            }
        }
         
      
        // 判断发消息的人是否在列表中
       
        private bool HasShowUser(int loginId)
        {
            bool find = false;  // 表示是否在当前显示出的用户列表中找到了该用户

            // 循环 SideBar 中的2个组,寻找发消息的人是否在列表中
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < sbFridends.Groups[i].Items.Count; j++)
                {
                    if (Convert.ToInt32(sbFridends.Groups[i].Items[j].Tag) == loginId)
                    {
                        find = true;                       
                    }
                }
            }
            return find;
        }
      
      
        // 更新陌生人列表
     
        private void UpdateStranger(int loginId)
        {
            // 选出这个人的基本信息
            string sql = "SELECT NickName, FaceId FROM Users WHERE Id=" + loginId;
         
            try
            {
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                SqlDataReader dataReader = command.ExecuteReader(); // 查询
                if (dataReader.Read())
                {
                    SbItem item = new SbItem((string)dataReader["NickName"], (int)dataReader["FaceId"]);
                    item.Tag = this.fromUserId;           // 将Id记录在Tag属性中
                    sbFridends.Groups[1].Items.Add(item);  // 向陌生人组中添加项
                }
                sbFridends.VisibleGroup = sbFridends.Groups[1];  // 设定陌生人组为可见组
            }
            catch (Exception ex)
            {
                //error = true;
                Console.WriteLine(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }
        }
        // 双击一项,弹出聊天窗体 
        private void sbFridends_ItemDoubleClick(SbItemEventArgs e)
        {
         
            if (timer3.Enabled == true)
            {
                timer3.Stop();
                e.Item.ImageIndex = this.friendFaceId;
            }
            // 显示聊天窗体
            ChatForm chatForm = new ChatForm();
            chatForm.friendId = Convert.ToInt32(e.Item.Tag); // 号码
            chatForm.name = e.Item.Text;  // 昵称
            chatForm.faceId = e.Item.ImageIndex;  // 头像

            chatForm.Show();
        }
        //查看好友信息
        private void friendsXinxi_Click(object sender, EventArgs e)
        {
            friendMessage fri= new friendMessage();
            fri.Show();
        }
        //皮肤
        private void 蓝色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = "DiamondBlue.ssk";
        }

        private void 绿色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = "DiamondGreen.ssk";
        }

        private void aToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = "MacOS.ssk";
        }

        private void qToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = "Eighteen.ssk";
        }

        private void 往往ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = "RealOne.ssk";
        }

        private void cToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = "Calmness.ssk";
        }
        //退出所有应用程序
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            frmMails f = new frmMails();
            f.Show();
        }

        //新建一个timer,让他控制窗体的影藏
        private void trzhuchantti_Tick(object sender, EventArgs e)
        {
            if (this.Bounds.Contains(Cursor.Position))
            {
                //窗体上有鼠标
                switch (lstate)
                {
                    case "top":
                        this.Location = new Point(this.Location.X, 0);
                        break;
                    case "left":
                        this.Location = new Point(0, this.Location.Y);
                        break;
                    case "right":
                        this.Location = new Point(Screen.AllScreens[0].Bounds.Width - this.Width, this.Location.Y);
                        break;
                    case "bottom":
                        this.Location = new Point(this.Location.X, Screen.AllScreens[0].Bounds.Height - this.Height);
                        break;
                }
            }
            else
            {
                //窗体上没鼠标,根据当前位置隐藏窗体。
                switch (lstate)
                {
                    case "top":
                        this.Location = new Point(this.Location.X, -(this.Height - 3));
                        break;
                    case "left":
                        this.Location = new Point(-(this.Width - 3), this.Location.Y);
                        break;
                    case "right":
                        this.Location = new Point(Screen.AllScreens[0].Bounds.Width - 3, this.Location.Y);
                        break;
                    case "bottom":
                        this.Location = new Point(this.Location.X, Screen.AllScreens[0].Bounds.Height - 3);
                        break;

                }
            }

        }
        private string lstate;

        //窗体改变位置事件。给他设一个锚机
        private void MainForm_LocationChanged(object sender, EventArgs e)
        {
            if (this.Top <= 0)
            {
                lstate = "top";
            }
            else if (this.Bottom >= Screen.AllScreens[0].Bounds.Height)
            {
                lstate = "bottom";
            }
            else if (this.Left <= 0)
            {
                lstate = "left";
            }
            else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width)
            {
                lstate = "right";
            }
            else
            {
                lstate = "null";
            }
        }

     
    }
}