📄 chat_frm.cs
字号:
/// <summary>
/// 通过窗体右上角关闭窗体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chat_frm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
}
if (e.CloseReason == CloseReason.FormOwnerClosing)
{
return;
}
close_btn_Click(sender, e);
}
/// <summary>
/// 发送消息,将接受方用户名和消息正文分开发送,便于服务器端处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void send_btn_Click(object sender, EventArgs e)
{
string localTxt = null;
string sendTxt = null;
string msg = msg_tb.Text.Trim();
if (msg == string.Empty)
{
MessageBox.Show("不能发送空消息",
"提示",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
//如果是聊天室模式则向服务器发送广播请求
if (broadcast_rb.Checked)
{
localTxt = string.Format("[广播]您在 {0} 对所有人说:\r\n{1}\r\n\r\n", DateTime.Now, msg);
sendTxt = string.Format("[广播]{0} 在 {1} 对所有人说:\r\n{2}\r\n\r\n", _username, DateTime.Now, msg);
//发送广播请求
_nws.Write(new byte[] { 0, 5 }, 0, 2);
}
else
{
string _receiver = online_cb.Text;
if (_receiver == string.Empty)
{
MessageBox.Show("请选择一个接收者!\n如果没有接受者可选,表明当前只有您一个人在线\t",
"发送消息",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
localTxt = string.Format("[私聊]您在 {0} 对 {1} 说:\r\n{2}\r\n\r\n", DateTime.Now, _receiver, msg);
sendTxt = string.Format("[私聊]{0} 在 {1} 对您说:\r\n{2}\r\n\r\n", _username, DateTime.Now, msg);
//发送接受者用户名
_nws.Write(Encoding.Unicode.GetBytes(_receiver), 0, Encoding.Unicode.GetBytes(_receiver).Length);
}
_nws.Write(Encoding.Unicode.GetBytes(sendTxt), 0, Encoding.Unicode.GetBytes(sendTxt).Length);
chatrcd_rtb.AppendText(localTxt);
msg_tb.Clear();
}
/// <summary>
/// 有新消息来时闪烁任务栏并且保持聊天记录内容滚动到最底端,QQ就是这么玩滴~
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
[DllImport("user32.dll")]
public static extern bool FlashWindow(IntPtr hWnd, bool bInvert);
private void chatrcd_rtb_TextChanged(object sender, EventArgs e)
{
chatrcd_rtb.ScrollToCaret();
if (this.WindowState == FormWindowState.Minimized)
{
FlashWindow(this.Handle, true);
}
}
/// <summary>
/// 当窗口恢复后取消任务栏的闪烁效果
/// 当窗口最小化时判断是否要隐藏到系统托盘
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chat_frm_SizeChanged(object sender, EventArgs e)
{
switch (this.WindowState)
{
case FormWindowState.Normal:
FlashWindow(this.Handle, false);
break;
case FormWindowState.Minimized:
if (_hideFlag)
{
notifyIcon1.Visible = true;
this.Visible = false;
}
break;
default:
break;
}
}
/// <summary>
/// 在线列表下拉框显示之前向服务器发送请求在线列表的命令
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void online_cb_DropDown(object sender, EventArgs e)
{
_nws.Write(new byte[] { 0, 2 }, 0, 2);
}
/// <summary>
/// 聊天模式改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void broadcast_rb_CheckedChanged(object sender, EventArgs e)
{
if (private_rb.Checked)
{
online_cb.Enabled = true;
}
else
{
online_cb.Enabled = false;
}
}
/// <summary>
/// 设置最小化到系统托盘的标记值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void hide_cb_CheckedChanged(object sender, EventArgs e)
{
_hideFlag = hide_cb.Checked;
}
/// <summary>
/// 产生闪屏振动效果
/// </summary>
private void Nudge()
{
if (notifyIcon1.Visible == true)
{
return;
}
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
int i = 0;
Point _old = this.Location;
Point _new1 = new Point(_old.X + 2, _old.Y + 2);
Point _new2 = new Point(_old.X - 2, _old.Y - 2);
_sp2.Play();
while (i < 4)
{
this.Location = _new1;
Thread.Sleep(60);
this.Location = _new2;
Thread.Sleep(60);
i++;
}
this.Location = _old;
}
/// <summary>
/// 发送闪屏振动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void nudge_pb_Click(object sender, EventArgs e)
{
string displayTxt = null;
if (private_rb.Checked && online_cb.Text == string.Empty)
{
MessageBox.Show("非聊天室模式下必须先选择一个接收者!",
"发送闪屏振动",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
if (private_rb.Checked)
{
_nws.Write(new byte[] { 0, 4 }, 0, 2);
string _receiver = online_cb.Text;
_nws.Write(Encoding.Unicode.GetBytes(_receiver), 0, Encoding.Unicode.GetBytes(_receiver).Length);
displayTxt = string.Format("[系统提示]您向 {0} 发送了一个闪屏振动。\r\n\r\n", _receiver);
}
else
{
_nws.Write(new byte[] { 0, 3 }, 0, 2);
displayTxt = "[系统提示]您向所有人发送了一个闪屏振动。\r\n\r\n";
}
chatrcd_rtb.AppendText(displayTxt);
Nudge();
}
/// <summary>
/// 以下是系统托盘菜单的处理函数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void close_tsmi_Click(object sender, EventArgs e)
{
close_btn_Click(sender, e);
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
comeback_tsmi_Click(sender, e);
}
private void comeback_tsmi_Click(object sender, EventArgs e)
{
notifyIcon1.Visible = false;
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.BringToFront();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -