qq源代码.docx
《qq源代码.docx》由会员分享,可在线阅读,更多相关《qq源代码.docx(37页珍藏版)》请在冰豆网上搜索。
qq源代码
UserHelper..cs类代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceMyQQ
{
//记录登录的用户Id
classUserHelper
{
publicstaticintloginId;//登录的用户Id
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespaceMyQQ
{
///
///聊天窗体
///
publicpartialclassChatForm:
Form
{
publicintfriendId;//当前聊天的好友号码
publicstringnickName;//当前聊天的好友昵称
publicintfaceId;//当前聊天的好友头像Id
publicChatForm()
{
InitializeComponent();
}
//窗体加载时的动作
privatevoidChatForm_Load(objectsender,EventArgse)
{
//设置窗体标题
this.Text=string.Format("与{0}聊天中",nickName);
//设置窗体顶部显示的好友信息
picFace.Image=ilFaces.Images[faceId];
lblFriend.Text=string.Format("{0}({1})",nickName,friendId);
//读取所有的未读消息,显示在窗体中
ShowMessage();
}
//关闭窗体
privatevoidbtnClose_Click(objectsender,EventArgse)
{
this.Close();
}
//发送消息
privatevoidbtnSend_Click(objectsender,EventArgse)
{
if(txtChat.Text.Trim()=="")//不能发送空消息
{
MessageBox.Show("不能发送空消息!
","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
elseif(txtChat.Text.Trim().Length>50)
{
MessageBox.Show("消息内容过长,请分为几条发送!
","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
else//发送消息,写入数据库
{
//MessageTypeId:
1-表示聊天消息,为简化操作没有读取数据表,到S2可以用常量或者枚举实现
//MessageState:
0-表示消息状态是未读
intresult=-1;//表示操作数据库的结果
stringsql=string.Format(
"INSERTINTOMessages(FromUserId,ToUserId,Message,MessageTypeId,MessageState)VALUES({0},{1},'{2}',{3},{4})"
UserHelper.loginId,friendId,txtChat.Text.Trim(),1,0);
try
{
//执行命令
SqlCommandcommand=newSqlCommand(sql,DBHelper.connection);
DBHelper.connection.Open();
result=command.ExecuteNonQuery();
}
catch(Exceptionex)
{
Console.WriteLine(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
if(result!
=1)
{
MessageBox.Show("服务器出现意外错误!
","抱歉",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
txtChat.Text="";//输入消息清空
this.Close();
}
}
///
///读取所有的未读消息,显示在窗体中
///
privatevoidShowMessage()
{
stringmessageIdsString="";//消息的Id组成的字符串
stringmessage;//消息内容
stringmessageTime;//消息发出的时间
//读取消息的SQL语句
stringsql=string.Format(
"SELECTId,Message,MessageTimeFromMessagesWHEREFromUserId={0}ANDToUserId={1}ANDMessageTypeId=1ANDMessageState=0",
friendId,UserHelper.loginId);
try
{
SqlCommandcommand=newSqlCommand(sql,DBHelper.connection);
DBHelper.connection.Open();
SqlDataReaderreader=command.ExecuteReader();
//循环将消息添加到窗体上
while(reader.Read())
{
messageIdsString+=Convert.ToString(reader["Id"])+"_";
message=Convert.ToString(reader["Message"]);
messageTime=Convert.ToDateTime(reader["MessageTime"]).ToString();//转换为日期类型,告诉学员
lblMessages.Text+=string.Format("\n{0}{1}\n{2}",nickName,messageTime,message);
}
reader.Close();
}
catch(Exceptionex)
{
Console.WriteLine(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
//把显示出的消息置为已读
if(messageIdsString.Length>1)
{
messageIdsString.Remove(messageIdsString.Length-1);
SetMessageRead(messageIdsString,'_');
}
}
///
///把显示出的消息置为已读
///
privatevoidSetMessageRead(stringmessageIdsString,charseparator)
{
string[]messageIds=messageIdsString.Split(separator);//分割出每个消息Id
stringsql="UpdateMessagesSETMessageState=1WHEREId=";//更新状态的SQL语句的固定部分
stringupdateSql;//执行的SQL语句
try
{
SqlCommandcommand=newSqlCommand();//创建Command对象
command.Connection=DBHelper.connection;//指定数据库连接
DBHelper.connection.Open();//打开数据库连接
foreach(stringidinmessageIds)
{
if(id!
="")
{
updateSql=sql+id;//补充完整的SQL语句
command.CommandText=updateSql;//指定要执行的SQL语句
intresult=command.ExecuteNonQuery();//执行命令
}
}
}
catch(Exceptionex)
{
Console.WriteLine(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
}
ChatForm.cs代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespaceMyQQ
{
///
///聊天窗体
///
publicpartialclassChatForm:
Form
{
publicintfriendId;//当前聊天的好友号码
publicstringnickName;//当前聊天的好友昵称
publicintfaceId;//当前聊天的好友头像Id
publicChatForm()
{
InitializeComponent();
}
//窗体加载时的动作
privatevoidChatForm_Load(objectsender,EventArgse)
{
//设置窗体标题
this.Text=string.Format("与{0}聊天中",nickName);
//设置窗体顶部显示的好友信息
picFace.Image=ilFaces.Images[faceId];
lblFriend.Text=string.Format("{0}({1})",nickName,friendId);
//读取所有的未读消息,显示在窗体中
ShowMessage();
}
//关闭窗体
privatevoidbtnClose_Click(objectsender,EventArgse)
{
this.Close();
}
//发送消息
privatevoidbtnSend_Click(objectsender,EventArgse)
{
if(txtChat.Text.Trim()=="")//不能发送空消息
{
MessageBox.Show("不能发送空消息!
","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
elseif(txtChat.Text.Trim().Length>50)
{
MessageBox.Show("消息内容过长,请分为几条发送!
","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
else//发送消息,写入数据库
{
//MessageTypeId:
1-表示聊天消息,为简化操作没有读取数据表,到S2可以用常量或者枚举实现
//MessageState:
0-表示消息状态是未读
intresult=-1;//表示操作数据库的结果
stringsql=string.Format(
"INSERTINTOMessages(FromUserId,ToUserId,Message,MessageTypeId,MessageState)VALUES({0},{1},'{2}',{3},{4})",
UserHelper.loginId,friendId,txtChat.Text.Trim(),1,0);
try
{
//执行命令
SqlCommandcommand=newSqlCommand(sql,DBHelper.connection);
DBHelper.connection.Open();
result=command.ExecuteNonQuery();
}
catch(Exceptionex)
{
Console.WriteLine(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
if(result!
=1)
{
MessageBox.Show("服务器出现意外错误!
","抱歉",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
txtChat.Text="";//输入消息清空
this.Close();
}
}
///
///读取所有的未读消息,显示在窗体中
///
privatevoidShowMessage()
{
stringmessageIdsString="";//消息的Id组成的字符串
stringmessage;//消息内容
stringmessageTime;//消息发出的时间
//读取消息的SQL语句
stringsql=string.Format(
"SELECTId,Message,MessageTimeFromMessagesWHEREFromUserId={0}ANDToUserId={1}ANDMessageTypeId=1ANDMessageState=0",
friendId,UserHelper.loginId);
try
{
SqlCommandcommand=newSqlCommand(sql,DBHelper.connection);
DBHelper.connection.Open();
SqlDataReaderreader=command.ExecuteReader();
//循环将消息添加到窗体上
while(reader.Read())
{
messageIdsString+=Convert.ToString(reader["Id"])+"_";
message=Convert.ToString(reader["Message"]);
messageTime=Convert.ToDateTime(reader["MessageTime"]).ToString();//转换为日期类型,告诉学员
lblMessages.Text+=string.Format("\n{0}{1}\n{2}",nickName,messageTime,message);
}
reader.Close();
}
catch(Exceptionex)
{
Console.WriteLine(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
//把显示出的消息置为已读
if(messageIdsString.Length>1)
{
messageIdsString.Remove(messageIdsString.Length-1);
SetMessageRead(messageIdsString,'_');
}
}
///
///把显示出的消息置为已读
///
privatevoidSetMessageRead(stringmessageIdsString,charseparator)
{
string[]messageIds=messageIdsString.Split(separator);//分割出每个消息Id
stringsql="UpdateMessagesSETMessageState=1WHEREId=";//更新状态的SQL语句的固定部分
stringupdateSql;//执行的SQL语句
try
{
SqlCommandcommand=newSqlCommand();//创建Command对象
command.Connection=DBHelper.connection;//指定数据库连接
DBHelper.connection.Open();//打开数据库连接
foreach(stringidinmessageIds)
{
if(id!
="")
{
updateSql=sql+id;//补充完整的SQL语句
command.CommandText=updateSql;//指定要执行的SQL语句
intresult=command.ExecuteNonQuery();//执行命令
}
}
}
catch(Exceptionex)
{
Console.WriteLine(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
}
privatevoidlblMessages_Click(objectsender,EventArgse)
{
}
}
}
DBHelper.cs类代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Data.SqlClient;
namespaceMyQQ
{
//数据库帮助类,维护数据库连接字符串和数据库连接对象
classDBHelper
{
privatestaticstringconnString="DataSource=.;database=MyQQ;integratedsecurity=sspi";
publicstaticSqlConnectionconnection=newSqlConnection(connString);
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceMyQQ
{
///
///头像选择窗体
///
publicpartialclassFacesForm:
Form
{
publicPersonalInfoFormpersonalInfoForm;//个人信息窗体
publicFacesForm()
{
InitializeComponent();
}
1.FacesForm.cs代码
privatevoidFacesForm_Load(objectsender,EventArgse)
{
for(inti=0;i{
lvFaces.Items.Add(i.ToString());
lvFaces.Items[i].ImageIndex=i;
}
}
//确定选择头像
privatevoidbtnOK_Click(objectsender,EventArgse)
{
if(lvFaces.SelectedItems.Count==0)
{
MessageBox.Show("请选择一个头像!
","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
intfaceId=lvFaces.SelectedItems[0].ImageIndex;//获得当前选中的头像的索引
personalInfoForm.ShowFace(faceId);//设置个人信息窗体中显示的头像
this.Close();
}
}
//双击时选择头像
privatevoidlvIcons_MouseDoubleClick(objectsender,MouseEventArgse)
{
intfaceId=lvFaces.SelectedItems[0].ImageIndex;//获得当前选中的头像的索引
personalInfoForm.ShowFace(faceId);//设置个人信息窗体中显示的头像
this.Close();
}
//关闭窗体
privatevoidbtnCancel_Click(objectsender,EventArgse)
{
this.Close();
}
}
}