QQ项目之九 主窗体

网友投稿 649 2022-05-29

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using Aptech.UI; //添加引用

using System.Data.SqlClient;

using System.Media; //需要添加多媒体命名空间!

namespace MYQQ

{

public partial class MainForm : Form

{

//*******************************************************成员变量定义开始*********

string myNickName = "";

int fromUserId;  // 消息的发起者

int friendFaceId; // 发消息的好友的头像Id

//********************************************************成员变量定义的结束!!!!!!!!!

public MainForm()

{

InitializeComponent();

}

///

/// 加载好友到主界面;链接数据库的代码,交给大家!

/// 写一个连接查询语句,连接两个表哦!!!!

///

///

///

private void MainForm_Load(object sender, EventArgs e)

{

Point p = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, this.Top);

this.PointToScreen(p); //制定工作区的坐标,转换成屏幕坐标;

this.Location = p;

// this.Location = new Point(Screen.GetWorkingArea(this).X - this.Width, this.Location.Y) ;

//1.加载好友头像列表;

sbFriends.AddGroup("我的好友"); //3个分组;

sbFriends.AddGroup("我的陌生人");

showFriend();

//2.加载自己头像列表方法;

showMyInfo();

InfoForm shan = new InfoForm();

shan.myName = myNickName;

shan.Show();

UserHelper.myNickName = myNickName;

FormDock fd = new FormDock(this);

}

#region 1.加载好友头像列表放到自定义的方法内;

public void showFriend() {

//+++++++++++++断点绝招;大招:try块;

sbFriends.Groups[0].Items.Clear(); //清除好友分组一内所有项

try

{

DBHelper.conn.Open();

string sql = string.Format("SELECT f.FriendId,u.NickName,u.FaceId FROM Users u,Friends f WHERE f.HostId={0} AND u.Id=f.FriendId",

UserHelper.loginId);

SqlCommand comm = new SqlCommand(sql, DBHelper.conn);

SqlDataReader dataReader = comm.ExecuteReader();

while (dataReader.Read())

{ //SbItem:是分组的每一项;,需要添加Aptech.UI;命名空间;

SbItem item = new SbItem((string)dataReader["NickName"], (int)dataReader["faceId"]);//昵称,头像编号

item.Tag = (int)dataReader["FriendId"]; //QQ号码;Tag:不显示,是一个隐藏标记,我们发消息的时候,可以直接引用;

sbFriends.Groups[0].Items.Add(item); //添加到分组;

}

dataReader.Close();

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

finally

{

DBHelper.conn.Close();

}

//************,不在需要了,添加好友***********

//SbItem item = new SbItem("火龙果籽",3);//昵称,头像编号

//item.Tag = 10007; //QQ号码

//sbFriends.Groups[2].Items.Add(item); //添加到分组;

//需要用到SqlDataReader类添加数据进行完善!

}

#endregion

#region 2.加载自己头像列表的方法;

public void showMyInfo() {

//如何加载自己的头像和昵称 /链接数据库

//借助于UserHelper,这里面有一个loginId,当前登录成功的QQ号码;

int faceId=0;

DBHelper.conn.Open();

string sql=string.Format("select faceId,nickname from users where id={0}",UserHelper.loginId);

SqlCommand comm = new SqlCommand(sql, DBHelper.conn);

SqlDataReader sdr = comm.ExecuteReader(); //执行,返回获取的数据

while (sdr.Read()) {

if (!sdr.IsDBNull(0)) { //头像编号不为空

faceId = sdr.GetInt32(0);

myNickName = sdr.GetString(1); //给自己的昵称赋值;

}

}

//下一步干嘛?

sdr.Close();

DBHelper.conn.Close();

//********************头像和昵称只是给了两个变量

picFace.Image = ilFaces.Images[faceId];

lblLoginId.Text = myNickName + "(" + UserHelper.loginId.ToString() + ")"; //标签的文本=等于字符串变量;

}

#endregion

#region 3.托盘图标控制

private void qToolStripMenuItem_Click(object sender, EventArgs e)

{

notifyIcon1.Icon = new Icon("ico\shipin.ico");

}

private void 离线ToolStripMenuItem_Click(object sender, EventArgs e)

{

notifyIcon1.Icon = new Icon("ico\yun.ico");

}

private void 隐身ToolStripMenuItem_Click(object sender, EventArgs e)

{

QQ项目之九 主窗体

notifyIcon1.Icon = new Icon("ico\mang.ico");

}

SearchFriendForm sff; //单例模式!!!!

private void tsbtnSearchFriend_Click(object sender, EventArgs e)

{

if (sff == null || sff.IsDisposed)

{

sff = new SearchFriendForm();

//sff.ShowDialog(); //最简单的方式

sff.Show();

}

}

private void 上线ToolStripMenuItem_Click(object sender, EventArgs e)

{

notifyIcon1.Icon = new Icon("ico\\q.ico"); //设置托盘图标的位置;

}

private void 我ToolStripMenuItem_Click(object sender, EventArgs e)

{

notifyIcon1.Icon = new Icon("ico\\yidong.ico");

}

#endregion

//sbFrident.SeletedItem.Parent.Items.Remove(sbFrident.SeletedItem);//删除

private void sbFriends_ItemDoubleClick(SbItemEventArgs e) //当前事件对象,谁上面进行双击,小强,10002

{

// 消息timer停止运行

if (tmrChatRequest.Enabled == true)

{

tmrChatRequest.Stop();

e.Item.ImageIndex = this.friendFaceId;

}

//实现的新功能!

foreach (Form f1 in OwnedForms)//Application.OpenForms

{

if (f1 is ChatForm) //如果是聊天窗体

{

if ((f1 as ChatForm).friendId == Convert.ToInt32(e.Item.Tag))

{

f1.Show();

return;

}

}

}

ChatForm f2 = new ChatForm();

f2.friendId = Convert.ToInt32(e.Item.Tag); // 号码,事件,项的Tag,好友的ID 10002

f2.nickName = e.Item.Text; // 昵称

f2.faceId = e.Item.ImageIndex; // 头像

f2.myName = myNickName; //需要在主窗体类再定义一个昵称变量;改行可以省略

f2.Show(this);

}

#region 4.编辑个人信息窗体,已经写过了

private void tsbtnPersonalInfo_Click(object sender, EventArgs e)

{

PersonalInfoForm personalInfoForm = new PersonalInfoForm();

personalInfoForm.mainForm = this; // 将当前窗体本身传给个人信息窗体

personalInfoForm.ShowDialog(); //模态对话框;

}

#endregion

//刷新好友

private void tsbtnUpdateFriendList_Click(object sender, EventArgs e)

{

showFriend();

}

#region 5.定义扫描消息

private void tmrMessage_Tick(object sender, EventArgs e)

{

showFriend(); // 刷新好友列表

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",

UserHelper.loginId);

SqlCommand command;

// 消息有两种类型:聊天消息、添加好友消息

try

{

command = new SqlCommand(sql, DBHelper.conn);

DBHelper.conn.Open();

SqlDataReader dataReader = command.ExecuteReader();

// 循环读出一个未读消息

if (dataReader.Read())

{

fromUserId = (int)dataReader["FromUserId"]; //获得发消息的ID

messageTypeId = (int)dataReader["MessageTypeId"]; //得到消息的类型

messageState = (int)dataReader["MessageState"]; //发的消息状态;

}

dataReader.Close();

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

finally

{

DBHelper.conn.Close();

}

// 判断消息类型,如果是添加好友消息,就启动喇叭timer,让小喇叭闪烁

if (messageTypeId == 2 && messageState == 0) //2:这是加为好友消息

{

SoundPlayer player = new SoundPlayer("system.wav"); //表示和当前QQ.exe是同一个级别

player.Play(); //播放声音

tmrAddFriend.Start(); //消息类型为2,并且消息未读,则启动添加好友消息!

}

// 如果是聊天消息,就启动聊天timer,让好友头像闪烁

else if (messageTypeId == 1 && messageState == 0)

{

// 获得发消息的人的头像Id

sql = "SELECT FaceId FROM Users WHERE Id=" + fromUserId;

try

{

command = new SqlCommand(sql, DBHelper.conn);

DBHelper.conn.Open();

this.friendFaceId = Convert.ToInt32(command.ExecuteScalar()); // 设置发消息的好友的头像索引;

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

finally

{

DBHelper.conn.Close();

}

//如果发消息的人没有在列表中就添加到陌生人列表中

if (!HasShowUser(fromUserId))

{

UpdateStranger(fromUserId);

}

SoundPlayer player = new SoundPlayer("msg.wav"); //聊天消息声音启动!

player.Play();

tmrChatRequest.Start(); // 启动闪烁头像定时器

}

}

#endregion

#region 更新到陌生人

private void UpdateStranger(int fromUserId)

{ //和添加到好友分组时一样的,唯一不同的是,将Group[0]-->Group[1]

string sql = "SELECT NickName, FaceId FROM Users WHERE Id=" +UserHelper.loginId;

bool error = false; // 用来标识是否出现错误

try

{

SqlCommand command = new SqlCommand(sql, DBHelper.conn);

DBHelper.conn.Open();

SqlDataReader dataReader = command.ExecuteReader(); // 查询

if (dataReader.Read())

{

SbItem item = new SbItem((string)dataReader["NickName"], (int)dataReader["FaceId"]);

item.Tag = this.fromUserId; // 将Id记录在Tag属性中

sbFriends.Groups[1].Items.Add(item); // 向陌生人组中添加项

}

sbFriends.VisibleGroup = sbFriends.Groups[1]; // 设定陌生人组为可见组

}

catch (Exception ex)

{

error = true;

Console.WriteLine(ex.Message);

}

finally

{

DBHelper.conn.Close();

}

if (error) // 出错了

{

MessageBox.Show("服务器出现意外错误!", "抱歉", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

#endregion

#region 判断发消息的人是否在列表中

private bool HasShowUser(int fromUserId)

{ //fromUserId:发消息过来的好友 号码,传参过来,如果相同,表示找到!就在好友闪烁;否则,陌生人

bool find = false; // 表示是否在当前显示出的用户列表中找到了该用户

// 循环 SideBar 中的2个组,寻找发消息的人是否在列表中

for (int i = 0; i < 2; i++)

{

for (int j = 0; j < sbFriends.Groups[i].Items.Count; j++)

{

if (Convert.ToInt32(sbFriends.Groups[i].Items[j].Tag) == fromUserId)

{

find = true;

}

}

}

return find;

}

#endregion

int messageImageIndex = 0; //0.工具栏中的消息图标的索引:xiaoxiIndex

//1.控制喇叭闪烁,控制喇叭闪烁是最简单的代码段!

private void tmrAddFriend_Tick(object sender, EventArgs e)

{

// 反复修改它的图像;xiaoxiIndex:

messageImageIndex = (messageImageIndex == 0) ? 1 : 0;

tsbtnMessageReading.Image = ilMessage.Images[messageImageIndex]; //Images[0]/Images[1];

//另外的方式

//if (tsbtnMessageReading.Image.ToString() == "")

// tsbtnMessageReading.Image = Image.FromFile("MessageReading.gif");

//else

// tsbtnMessageReading.Image = Image.FromFile("");

}

//2.单击小喇叭显示请求好友消息窗体

private void tsbtnMessageReading_Click(object sender, EventArgs e)

{//tsbtnMessageReading:就是小喇叭按钮

tmrAddFriend.Stop(); //添加好友.消息timer停止运行

// 图片恢复正常

messageImageIndex = 0; //tsbtnMessageReading.Image = Image.FromFile("MessageReading.gif");

tsbtnMessageReading.Image = ilMessage.Images[messageImageIndex];

// 显示系统消息窗体;请求加为好友的窗体;!!!!

RequestForm requestForm = new RequestForm();

requestForm.ShowDialog();

}

#region 让相应的好友头像闪烁

private void tmrChatRequest_Tick(object sender, EventArgs e)

{

// 循环好友列表两个组中的每个item,找到发消息的好友,让他的头像闪烁

for (int i = 0; i < 2; i++) //0或1

{

for (int j = 0; j < sbFriends.Groups[i].Items.Count; j++) //j:0,....,好友个数

{

if (Convert.ToInt32(sbFriends.Groups[i].Items[j].Tag) == this.fromUserId) //如果[0][j]==你的好友号码;

{

if (sbFriends.Groups[i].Items[j].ImageIndex < 100) //实现头像变化,什么时候引起呢?

{

sbFriends.Groups[i].Items[j].ImageIndex = 100;// 索引为100的图片是一个空白图片

}

else

{

sbFriends.Groups[i].Items[j].ImageIndex = this.friendFaceId;

}

sbFriends.Invalidate(); // 重新绘制,只要告诉学生需要这句话才能正常闪烁头像就行

}

}

}

}

#endregion

private void MainForm_FormClosed(object sender, FormClosedEventArgs e)

{

Application.Exit(); //退出整个程序,不会导致,你的程序存在于资源管理器

}

public FrmUserInformation frm; //小信息窗体;

#region 鼠标离开事件

private void picFace_MouseLeave(object sender, EventArgs e)

{

if (frm != null)

{

frm.Hide();

}

}

#endregion

#region 鼠标悬停事件

private void picFace_MouseHover(object sender, EventArgs e)

{

//窗体的TopLeft值

int UserTop = this.Location.Y+55;

int UserLeft = this.Location.X;

//窗体不为空传值

if (frm == null || frm.IsDisposed)

{

frm = new FrmUserInformation();

frm.point = new Point(UserLeft - frm.Width, UserTop);

frm.Show();

}

else {

frm.point = new Point(UserLeft - frm.Width, UserTop);

frm.Show();

}

}

#endregion

private void sbFriends_Load(object sender, EventArgs e)

{

}

private void picFace_Click(object sender, EventArgs e)

{

}

public frmWeather fw;

//天气

private void pictureBox1_MouseHover(object sender, EventArgs e)

{

//窗体的TopLeft值

int UserTop = this.Top;

int UserLeft = this.Right ;

//窗体不为空传值

if (fw == null || fw.IsDisposed)

{

fw = new frmWeather();

fw.point = new Point(UserLeft,UserTop);

fw.Show();

}

else

{

fw.point = new Point(UserLeft, UserTop);

fw.Show();

}

}

private void pictureBox1_MouseLeave(object sender, EventArgs e)

{

if(fw!=null)

fw.Hide();

}

private void contextMenuStrip2_Click(object sender, EventArgs e)

{

}

private void 小图标ToolStripMenuItem_Click(object sender, EventArgs e)

{

sbFriends.View = SbView.SmallIcon;

}

private void 大图标ToolStripMenuItem_Click(object sender, EventArgs e)

{

sbFriends.View = SbView.LargeIcon;

}

private void 列表ToolStripMenuItem_Click(object sender, EventArgs e)

{

sbFriends.View = SbView.List;

}

private void sbFriends_MouseClick(object sender, MouseEventArgs e)

{

Point p = sbFriends.PointToClient(MousePosition);

if (e.Button == MouseButtons.Right)

{

if (sbFriends.SeletedItem != null && sbFriends.SeletedItem.HitTest(p.X, p.Y) == true)

{

contextMenuStrip1.Show(MousePosition);

}

}

}

private void 修改组名ToolStripMenuItem_Click(object sender, EventArgs e)

{

sbFriends.SeletedItem.Parent.Text = "AAA";

}

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)

{

}

private void 刷新好友ToolStripMenuItem_Click(object sender, EventArgs e)

{

}

private void pictureBox1_Click(object sender, EventArgs e)

{

}

}

}

数据库

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:完美取代hashmap,SpringBoot整合Redis缓存DB数据
下一篇:IC先生网:什么是MEMS传感器?类型、应用特点介绍
相关文章