} catch {
MessageBox.Show(\发送失败\ }
finally {
myUdpClient.Close(); } }
else if (index == 0 || index == -1) {
IsValidIP(txtIPaddree.Text);
send(txtIPaddree.Text, txtPort.Text); } else {
send(cbxChoose.Text, txtPort.Text); } }
#endregion
#region IsValid_field public void Terminate() {
udpClient.DropMulticastGroup(m_GroupAddress); }
//检测输入对方IP有没有效
private void IsValidIP(string str) {
IPAddress ip;
if (!IPAddress.TryParse(str, out ip)) {
MessageBox.Show(\非法IP地址\ return; } }
//检测端口号有没有效
private void IsValidPort(string str) {
int isPort;
if (txtPort.Text == \ {
16
MessageBox.Show(\端口号不能为空\ return; }
else if (!int.TryParse(str, out isPort)) {
MessageBox.Show(\端口号无效\ return; }
else if ((isPort < 1024) || (isPort > 65335)) {
MessageBox.Show(\端口号应该大于或等于1024,小于或等于65535\ return; } }
#endregion
#region tray_field
private void chat_Load(object sender, EventArgs e) {
btnChat.Enabled = true; btnStop.Enabled = false; txbSend.Enabled = false;
cbxChoose.SelectedIndex = -1;
//在当前窗体的容器中创建托盘图标NotifyIcon的实例 myNotifyIcon = new NotifyIcon(this.components); //指定托盘图标
myNotifyIcon.Icon = new Icon(\ //鼠标悬停在托盘图标上方时显示的内容 myNotifyIcon.Text = \我的聊天器\ //设置关联的上下文菜单
myNotifyIcon.ContextMenuStrip = this.contextMenuStrip1; //显示托盘图标
myNotifyIcon.Visible = true;
//添加用户双击任务栏中的托盘图标时触发的事件 myNotifyIcon.DoubleClick += new EventHandler(myNotifyIcon_DoubleClick); }
void myNotifyIcon_DoubleClick(object sender, EventArgs e) {
if (isShow) {
this.Hide(); isShow = false; } else
17
{
this.Show(); isShow = true; } }
private void chat_FormClosing(object sender, FormClosingEventArgs e) {
if (isExit == false) {
//取消关闭窗体事件 e.Cancel = true; //隐藏窗体 this.Hide(); } else {
udpClient.Close(); } }
private void 显示窗口toolStripMenuItem1_Click(object sender, EventArgs e) {
this.Show(); }
private void 退出程序toolStripMenuItem2_Click(object sender, EventArgs e) {
isExit = true;
Application.Exit(); }
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (isShow) {
this.Hide(); isShow = false; } else {
this.Show(); isShow = true; } }
private Icon m_Icon1; private Icon m_Icon2;
18
private void 打开计时器ToolStripMenuItem_Click(object sender, EventArgs e)
{
tmIcon.Start(); }
private void 停止计时器ToolStripMenuItem_Click(object sender, EventArgs e)
{
tmIcon.Stop(); }
private void tmIcon_Tick(object sender, EventArgs e) {
if (m_Icon1 != null && m_Icon2 != null) //如果两个图标文件都被正确载入
{
//只要tmIcon被启动,则在两个图标之间不断进行选择变换,实现动画效果
if (m_bFlag == true) {
notifyIcon1.Icon = m_Icon2; m_bFlag = false; } else {
notifyIcon1.Icon = m_Icon1; m_bFlag = true; } } }
#endregion
#region control_delegate_method
private void AppendString(string text) {
if (rtbMessage.InvokeRequired) {
rtbMessage.Invoke(appendStringCallback, text); } else {
rtbMessage.AppendText(text + \ } }
private void SetComboBox(string str) {
19