日期:2011-07-06  浏览次数:20407 次

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Net.Sockets;
using System.Threading;
namespace p2pChat
{
  ///
  /// MainForm 的摘要说明。
  ///
  public class MainForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox txtIp;
    private System.Windows.Forms.TextBox txtRecord;
    private System.Windows.Forms.TextBox txtName;
    private System.Windows.Forms.Button btnSend;
    private System.Windows.Forms.TextBox txtContent;

    private TcpListener tcpLister = new TcpListener(5566);
    System.Threading.ThreadStart listenPort;
    System.Threading.Thread lister;
    private System.Windows.Forms.Label label3;
    System.Windows.Forms.NotifyIcon NotifyIcon1;
    //托盘里显示的图标,我用的是QQ里的宠物狗的图标
    private Icon img = new Icon(@"C:\OpenPet.ico");
    System.Windows.Forms.ContextMenu nMenu;
    ///
    /// 必需的设计器变量。
    ///
    private System.ComponentModel.Container components = null;
    public MainForm()
    {
        //
        // Windows 窗体设计器支持所必需的
        //
        InitializeComponent();

        //不显示最大化按钮
        this.MaximizeBox = false;
        //最小化时不显示在任务栏
        this.ShowInTaskbar = false;
 listenPort += new ThreadStart(this.Listen);
        lister = new Thread(listenPort);
        this.Closing += new System.ComponentModel.CancelEventHandler(abortLister);
        Initializenotifyicon();
        //
        // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
        //
    }
    #region 初始化托盘组件
    private void Initializenotifyicon()
    {
        NotifyIcon1 = new NotifyIcon();
        NotifyIcon1.Icon = img;
        NotifyIcon1.Text = "局域网聊天程序";
        NotifyIcon1.Visible = true;
        NotifyIcon1.DoubleClick += new EventHandler(this.showMainForm);

        MenuItem [] menuArray = new MenuItem[3];
        menuArray[0] = new MenuItem();
        menuArray[0].Text = "显示主窗口";
        menuArray[0].Click += new EventHandler(this.showMainForm);
        menuArray[0].DefaultItem = true;

        menuArray[1] = new MenuItem("-");

        menuArray[2] = new MenuItem();
        menuArray[2].Text = "退出";
        menuArray[2].Click += new EventHandler(this.exitSystem);

        nMenu = new ContextMenu(menuArray);
        NotifyIcon1.ContextMenu = nMenu;
    }
    //显示主窗口
    private void showMainForm(object sender,System.EventArgs e)
    {
        this.WindowState = System.Windows.Forms.FormWindowState.Normal;
    }
    //退出程序
    private void exitSystem(object sender,System.EventArgs e)
    {
        NotifyIcon1.Visible = false;
        this.Close();
    }
    #endregion
    ///
    /// 清理所有正在使用的资源。
    ///
    protected override void Dispose( bool disposing )
    {
        if( disposing )
        {
         if (components != null)
         {
            components.Dispose();
         }
        }
       base.Dispose( disposing );
    }
    #region Windows 窗体设计器生成的代码
    ///
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    ///
    private void InitializeComponent()
{

        this.label1 = new System.Windows.Forms.Label();
        this.txtIp = new System.Windows.Forms.TextBox();
        this.txtRecord = new System.Windows.Forms.TextBox();