用c#实现类似QQ的简单通讯程序

发布时间:2009年06月04日      浏览次数:459 次
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();
        this.label2 = new System.Windows.Forms.Label();
        this.txtName = new System.Windows.Forms.TextBox();
        this.btnSend = new System.Windows.Forms.Button();
        this.txtContent = new System.Windows.Forms.TextBox();
        this.label3 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        //
        // label1
        //
        this.label1.Location = new System.Drawing.Point(16, 232);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(72, 23);
        this.label1.TabIndex = 0;
        this.label1.Text = "目标地址:";
        this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        //
        // txtIp
        //
        this.txtIp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
        this.txtIp.Location = new System.Drawing.Point(80, 232);
        this.txtIp.Name = "txtIp";
        this.txtIp.Size = new System.Drawing.Size(200, 21);
        this.txtIp.TabIndex = 1;
        this.txtIp.Text = "";
        //
        // txtRecord
        //
        this.txtRecord.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
        this.txtRecord.Location = new System.Drawing.Point(16, 32);
        this.txtRecord.Multiline = true;
        this.txtRecord.Name = "txtRecord";
        this.txtRecord.ReadOnly = true;
        this.txtRecord.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
        this.txtRecord.Size = new System.Drawing.Size(264, 176);
        this.txtRecord.TabIndex = 4;
        this.txtRecord.Text = "";
        //
        // label2
        //
        this.label2.Location = new System.Drawing.Point(24, 256);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(48, 23);
        this.label2.TabIndex = 5;
        this.label2.Text = "呢 称:";
        this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        //
        // txtName
        //
        this.txtName.Location = new System.Drawing.Point(80, 256);
        this.txtName.Name = "txtName";
        this.txtName.Size = new System.Drawing.Size(88, 21);
        this.txtName.TabIndex = 6;
        this.txtName.Text = "";
        //
        // btnSend
        //
        this.btnSend.Location = new System.Drawing.Point(200, 256);
        this.btnSend.Name = "btnSend";
        this.btnSend.Size = new System.Drawing.Size(64, 23);
        this.btnSend.TabIndex = 0;
        this.btnSend.Text = "发 送";
        this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
        //
        // txtContent
        //
        this.txtContent.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
         | System.Windows.Forms.AnchorStyles.Right)));
        this.txtContent.Location = new System.Drawing.Point
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!