程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 用c#實現類似QQ的簡單通訊程序

用c#實現類似QQ的簡單通訊程序

編輯:關於C語言

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved