/// <summary> /// 置顶 /// </summary> /// <param name="o"></param> private void GroupShowForm_ToUpEvent(object o) { GroupShowForm.GroupShowForm groupShowForm = o as GroupShowForm.GroupShowForm; this.LeftPanel.Controls.Remove(groupShowForm); this.LeftPanel.Controls.Add(groupShowForm); groupShowForm.Dock = DockStyle.Top; }
///// <summary> ///// 添加信息 ///// </summary> ///// <param name="baseInformation"></param> ///// <param name="title"></param> //public void AddControlsObjectToPanel(List<GroupShowForm.GroupShowForm> baseInformation, string title = "") //{ // if (baseInformation == null) // { // //没有信息 // return; // } // if(baseInformation.Count<1) // { // return; // } // for (int i = 0; i < baseInformation.Count; i++) // { // this.Controls.Add(baseInformation[i]); // } // //添加模块标记 // if (title != "") // { // SpacerForm.SpacerForm titleSpacerForm = new SpacerForm.SpacerForm(title); // titleSpacerForm.Dock = DockStyle.Top; // this.Controls.Add(titleSpacerForm); // } //} /// <summary> /// 添加信息 /// </summary> /// <param name="baseInformation"></param> /// <param name="title"></param> public void AddControlsToPanel(List <BaseInformation> baseInformation, string title = "") { if (baseInformation == null || baseInformation.Count < 1) { //没有信息 return; } for (int i = 0; i < baseInformation.Count; i++) { BaseInformation info = baseInformation[i]; GroupShowForm.GroupShowForm groupShowForm = new GroupShowForm.GroupShowForm(); groupShowForm.SetInformation(info.Kind, info.UserName, info.Account, info.UserImage, info.IsOnline); groupShowForm.Visible = true; groupShowForm.Dock = DockStyle.Top; groupShowForm.DoubleClickEvent += GroupShowForm_DoubleClick; groupShowForm.MouseRightClick.Items.RemoveAt(3); groupShowForm.MouseRightClick.Items.RemoveAt(2); groupShowForm.MouseRightClick.Items.RemoveAt(1); this.Controls.Add(groupShowForm); } //添加模块标记 if (title != "") { SpacerForm.SpacerForm titleSpacerForm = new SpacerForm.SpacerForm(title); titleSpacerForm.Dock = DockStyle.Top; this.Controls.Add(titleSpacerForm); } }
/// <summary> /// 添加聊天好友/群 /// </summary> /// <param name="InsertForm"></param> public void AddControlsToLeft(int kind, string account, string userName, Bitmap userImage) { GroupShowForm.GroupShowForm groupShowForm = null; //尝试找到已有控件置顶 for (int i = 0; i < LeftPanel.Controls.Count; i++) { groupShowForm = LeftPanel.Controls[i] as GroupShowForm.GroupShowForm; if (groupShowForm.Account == account) { this.LeftPanel.Controls.Remove(groupShowForm); break; } groupShowForm = null; } //没有找到控件,生成新控件 if (groupShowForm == null) { groupShowForm = new GroupShowForm.GroupShowForm(); groupShowForm.ToUpEvent += GroupShowForm_ToUpEvent; groupShowForm.DeleteEvent += GroupShowForm_DeleteEvent; groupShowForm.ClickEvent += GroupShowForm_Click; groupShowForm.Dock = DockStyle.Top; } //设置信息 groupShowForm.SetInformation(kind, userName, account, userImage); groupShowForm.OnClick(); //触发 //控件放入panel this.LeftPanel.Controls.Add(groupShowForm); }
/// <summary> /// 控件点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void GroupShowForm_Click(object sender, EventArgs e) { GroupShowForm.GroupShowForm groupShowForm = sender as GroupShowForm.GroupShowForm; this.Kind = groupShowForm.Kind - 3;//为了区分群组,原类型值之前有加3处理 this.AccountOpposite = groupShowForm.Account; this.UserNameOpposite = groupShowForm.UserName; this.ImageOpposite = groupShowForm.UserImage; this.RightTopForm.SetInformation(this.accountOpposite, this.userNameOpposite, groupShowForm.UserImage); }
/// <summary> /// 点击聊天框中的关闭按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BottomCloseButton_Click(object sender, EventArgs e) { for (int i = 0; i < this.LeftPanel.Controls.Count; i++) { GroupShowForm.GroupShowForm groupShowForm = this.LeftPanel.Controls[i] as GroupShowForm.GroupShowForm; if (groupShowForm.Account == AccountOpposite) { this.GroupShowForm_DeleteEvent(groupShowForm); return; } } }
/// <summary> /// 搜索文本 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SearchTextBox_TextChanged(object sender, EventArgs e) { if (this.LeftPanel.Controls.Count < 1) { return; } for (int i = 0; i < this.LeftPanel.Controls.Count; i++) { GroupShowForm.GroupShowForm groupShowForm = this.LeftPanel.Controls[i] as GroupShowForm.GroupShowForm; if (groupShowForm.information.HasKeywords(this.SearchTextBox.Text)) { GroupShowForm_ToUpEvent(groupShowForm); } } }
/// <summary> /// 触发接收好友消息事件 /// </summary> /// <param name="userName">发送方昵称</param> /// <param name="userAccount">发送方账户</param> /// <param name="message">消息</param> /// <param name="time">发送时间</param> /// <param name="userImage">发送方图像</param> /// <param name="ID">群聊/账户</param> /// <param name="isOneline">是否在线</param> public static void EmitAddNewFriendMessageEvent(string userAccount, string message, string time) { string userName; Bitmap userImage; bool isOnline; GroupShowForm.GroupShowForm groupShowForm = UserInformation.GetFriendObject(userAccount) as GroupShowForm.GroupShowForm; if (groupShowForm != null) { userName = groupShowForm.UserName; userImage = groupShowForm.UserImage; isOnline = groupShowForm.IsOnline; } else { userName = "******"; userImage = new Bitmap(System.Environment.CurrentDirectory + "\\initImage.png"); isOnline = true; } AddNewMessageEvent(userName, userAccount, message, time, userImage, userAccount, isOnline); }
/// <summary> /// 打开聊天事件 /// </summary> /// <param name="o"></param> private void DownForm_RequestChat(object o) { GroupShowForm.GroupShowForm groupShowForm = o as GroupShowForm.GroupShowForm; if (chatForm == null) { MessageBox.Show("错误:内部聊天对象被意外释放"); chatForm = new ChatForm(); chatForm.Show(); chatForm.FormClosed += ChatForm_FormClosed; } this.chatForm.Visible = true; //显示界面 if (chatForm.WindowState == FormWindowState.Minimized) { chatForm.WindowState = FormWindowState.Normal; } //为了使得聊天界面左部信息控件可以区分类别,又使其不响应双击打开聊天事件,kind+3 chatForm.AddControlsToLeft(groupShowForm.Kind + 3, groupShowForm.Account, groupShowForm.UserName, groupShowForm.UserImage); }
/// <summary> /// 删除 /// </summary> /// <param name="o"></param> private void GroupShowForm_DeleteEvent(object o) { GroupShowForm.GroupShowForm groupShowForm = o as GroupShowForm.GroupShowForm; string account = groupShowForm.Account; this.LeftPanel.Controls.Remove(groupShowForm); if (account != AccountOpposite) //删除的并不是正在聊天的对象 { return; } //当前对象被删除,开启一个新的对象 if (this.LeftPanel.Controls.Count < 1) { this.CloseButton.PerformClick(); } else { GroupShowForm.GroupShowForm groupShowFormNew = this.LeftPanel.Controls[0] as GroupShowForm.GroupShowForm; groupShowFormNew.OnClick(); } }
/// <summary> /// 回放历史消息 /// </summary> /// <param name="type">好友还是群聊</param> /// <param name="accountOpposite">账户</param> /// <param name="count">回放条数</param> private void ShowHistorNews(int type, string ID, DateTime minDateTime) { //清空消息界面 this.Clearnews(); //打开数据库,不存在则创建 string databasePath = System.Environment.CurrentDirectory + "\\files\\records.db"; if (!File.Exists(databasePath)) { try { SQLiteConnection.CreateFile(databasePath); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } } SQLiteConnection databaseReader = null; try { databaseReader = new SQLiteConnection("Data Source=" + databasePath + "; Version=3;UseUTF16Encoding=True;"); databaseReader.Open(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } //打开表,不存在则创建 SQLiteCommand cmd = new SQLiteCommand(); cmd.Connection = databaseReader; cmd.CommandText = "select * from sqlite_master where type=\"table\" and name=\"TABLENAME\";".Replace("TABLENAME", ID); SQLiteDataReader resultTable = null; try { resultTable = cmd.ExecuteReader(); } catch (Exception ex) { MessageBox.Show(ex.Message); databaseReader.Close(); return; } bool tableIsExist = false; //存储聊天数据的表是否已经存在 if (resultTable.Read()) //一行数据有效则返回true { tableIsExist = true; } resultTable.Close(); try { if (!tableIsExist) //表不存在 { //以账户为表名创建新表 cmd.CommandText = @"CREATE TABLE 'TABLENAME' ( 'DateTime' TEXT NOT NULL, 'Account' TEXT NOT NULL, 'News' TEXT NOT NULL, 'UserName' TEXT NOT NULL );".Replace("TABLENAME", ID); //创建表的SQL语句 cmd.ExecuteNonQuery(); resultTable.Close(); return; } } catch (Exception ex) { MessageBox.Show(ex.Message); } try { cmd.CommandText = "select * from (select * from \"TABLENAME\" where DateTime >= \"OLDDATE\" order by DateTime DESC) order by DateTime ASC;".Replace("TABLENAME", ID).Replace("OLDDATE", minDateTime.ToString("yyyy-MM-dd hh:mm:ss")); SQLiteDataReader newsReader = cmd.ExecuteReader(); if (type == 1) //好友 { while (newsReader.Read()) { Bitmap image = null; //图像 string dateTime = ""; //日期 string news = ""; //消息 int flag = 0; //1表示自己,2表示对方 dateTime = (newsReader[0] as string).Replace("/", "-"); if ((newsReader[1] as string) == "@@me") { image = MessageBirdCommon.UserInformation.Image; flag = 1; } else { image = this.ImageOpposite; flag = 2; } news = newsReader[2] as string; //添加控件 AddNewsToPanel(flag, news, image, dateTime); } } else if (type == 2) //群聊 { while (newsReader.Read()) { string dateTime = ""; string news = ""; string account = ""; int flag = 0; //1表示自己,2表示对方 dateTime = (newsReader[0] as string).Replace("/", "-"); account = newsReader[1] as string; news = newsReader[2] as string; //查询图像和昵称 Bitmap image = null; string userName = ""; if (account == "@@me") { image = MessageBirdCommon.UserInformation.Image; flag = 1; userName = "******"; } else { flag = 2; GroupShowForm.GroupShowForm groupShowForm = MessageBirdCommon.UserInformation.GetFriendObject(account) as GroupShowForm.GroupShowForm; if (groupShowForm != null) { image = groupShowForm.UserImage; } userName = newsReader[3] as string; } if (image == null) { image = new Bitmap(System.Environment.CurrentDirectory + "\\initImage.png"); } //添加控件 AddNewsToPanel(flag, news, image, dateTime, userName); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { databaseReader.Close(); } }