实例介绍
【实例简介】winform写的一个QQ界面
【实例截图】

【核心代码】namespace MyQQ
{
/// <summary>
/// 聊天窗体
/// </summary>
public partial class ChatForm : Form
{
public int friendId; // 当前聊天的好友号码
public string nickName; // 当前聊天的好友昵称
public int faceId; // 当前聊天的好友头像Id
public ChatForm()
{
InitializeComponent();
}
// 窗体加载时的动作
private void ChatForm_Load(object sender, EventArgs e)
{
// 设置窗体标题
this.Text = string.Format("与{0}聊天中",nickName);
// 设置窗体顶部显示的好友信息
picFace.Image = ilFaces.Images[faceId];
lblFriend.Text = string.Format("{0}({1})",nickName,friendId);
// 读取所有的未读消息,显示在窗体中
ShowMessage();
}
// 关闭窗体
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
// 发送消息
private void btnSend_Click(object sender, EventArgs e)
{
if (txtChat.Text.Trim() == "") // 不能发送空消息
{
MessageBox.Show("不能发送空消息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else if (txtChat.Text.Trim().Length > 50)
{
MessageBox.Show("消息内容过长,请分为几条发送!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else // 发送消息,写入数据库
{
// MessageTypeId:1-表示聊天消息,为简化操作没有读取数据表,到S2可以用常量或者枚举实现
// MessageState:0-表示消息状态是未读
int result = -1; // 表示操作数据库的结果
string sql = string.Format(
"INSERT INTO Messages (FromUserId, ToUserId, Message, MessageTypeId, MessageState) VALUES ({0},{1},'{2}',{3},{4})",
UserHelper.loginId, friendId, txtChat.Text.Trim(), 1, 0);
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.Error);
}
txtChat.Text = ""; // 输入消息清空
this.Close();
}
}
/// <summary>
/// 读取所有的未读消息,显示在窗体中
/// </summary>
private void ShowMessage()
{
string messageIdsString = ""; // 消息的Id组成的字符串
string message; // 消息内容
string messageTime; // 消息发出的时间
// 读取消息的SQL语句
string sql = string.Format(
"SELECT Id, Message,MessageTime From Messages WHERE FromUserId={0} AND ToUserId={1} AND MessageTypeId=1 AND MessageState=0",
friendId,UserHelper.loginId);
try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
SqlDataReader reader = 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 (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
// 把显示出的消息置为已读
if (messageIdsString.Length > 1)
{
messageIdsString.Remove(messageIdsString.Length - 1);
SetMessageRead(messageIdsString, '_');
}
}
/// <summary>
/// 把显示出的消息置为已读
/// </summary>
private void SetMessageRead(string messageIdsString, char separator)
{
string[] messageIds = messageIdsString.Split(separator); // 分割出每个消息Id
string sql = "Update Messages SET MessageState=1 WHERE Id="; // 更新状态的SQL语句的固定部分
string updateSql; // 执行的SQL语句
try
{
SqlCommand command = new SqlCommand(); // 创建Command对象
command.Connection = DBHelper.connection; // 指定数据库连接
DBHelper.connection.Open(); // 打开数据库连接
foreach (string id in messageIds)
{
if (id != "")
{
updateSql = sql id; // 补充完整的SQL语句
command.CommandText = updateSql; // 指定要执行的SQL语句
int result = command.ExecuteNonQuery(); // 执行命令
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
}
}
【实例截图】

【核心代码】namespace MyQQ
{
/// <summary>
/// 聊天窗体
/// </summary>
public partial class ChatForm : Form
{
public int friendId; // 当前聊天的好友号码
public string nickName; // 当前聊天的好友昵称
public int faceId; // 当前聊天的好友头像Id
public ChatForm()
{
InitializeComponent();
}
// 窗体加载时的动作
private void ChatForm_Load(object sender, EventArgs e)
{
// 设置窗体标题
this.Text = string.Format("与{0}聊天中",nickName);
// 设置窗体顶部显示的好友信息
picFace.Image = ilFaces.Images[faceId];
lblFriend.Text = string.Format("{0}({1})",nickName,friendId);
// 读取所有的未读消息,显示在窗体中
ShowMessage();
}
// 关闭窗体
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
// 发送消息
private void btnSend_Click(object sender, EventArgs e)
{
if (txtChat.Text.Trim() == "") // 不能发送空消息
{
MessageBox.Show("不能发送空消息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else if (txtChat.Text.Trim().Length > 50)
{
MessageBox.Show("消息内容过长,请分为几条发送!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else // 发送消息,写入数据库
{
// MessageTypeId:1-表示聊天消息,为简化操作没有读取数据表,到S2可以用常量或者枚举实现
// MessageState:0-表示消息状态是未读
int result = -1; // 表示操作数据库的结果
string sql = string.Format(
"INSERT INTO Messages (FromUserId, ToUserId, Message, MessageTypeId, MessageState) VALUES ({0},{1},'{2}',{3},{4})",
UserHelper.loginId, friendId, txtChat.Text.Trim(), 1, 0);
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.Error);
}
txtChat.Text = ""; // 输入消息清空
this.Close();
}
}
/// <summary>
/// 读取所有的未读消息,显示在窗体中
/// </summary>
private void ShowMessage()
{
string messageIdsString = ""; // 消息的Id组成的字符串
string message; // 消息内容
string messageTime; // 消息发出的时间
// 读取消息的SQL语句
string sql = string.Format(
"SELECT Id, Message,MessageTime From Messages WHERE FromUserId={0} AND ToUserId={1} AND MessageTypeId=1 AND MessageState=0",
friendId,UserHelper.loginId);
try
{
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
SqlDataReader reader = 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 (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
// 把显示出的消息置为已读
if (messageIdsString.Length > 1)
{
messageIdsString.Remove(messageIdsString.Length - 1);
SetMessageRead(messageIdsString, '_');
}
}
/// <summary>
/// 把显示出的消息置为已读
/// </summary>
private void SetMessageRead(string messageIdsString, char separator)
{
string[] messageIds = messageIdsString.Split(separator); // 分割出每个消息Id
string sql = "Update Messages SET MessageState=1 WHERE Id="; // 更新状态的SQL语句的固定部分
string updateSql; // 执行的SQL语句
try
{
SqlCommand command = new SqlCommand(); // 创建Command对象
command.Connection = DBHelper.connection; // 指定数据库连接
DBHelper.connection.Open(); // 打开数据库连接
foreach (string id in messageIds)
{
if (id != "")
{
updateSql = sql id; // 补充完整的SQL语句
command.CommandText = updateSql; // 指定要执行的SQL语句
int result = command.ExecuteNonQuery(); // 执行命令
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
}
}
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论