首页 > 开发 > 综合 > 正文

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

2024-07-21 02:24:52
字体:
来源:转载
供稿:网友

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


 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表