现有一个很棘手的问题,,从远程接受到一个消息在本地初始化一个窗体,怎么实现,,各位帮帮忙,,,
如QQ那样,发起视频邀请 ,,它是怎么动态生成窗体的!!谢谢各位!!!

解决方案 »

  1.   

    简单地,本地收到请求后,new一个窗体不就行了...
      

  2.   

    哈哈
    你的问题,我碰到过!
    首先在本机器上定义好窗体,当你接受对方的消息时,就利用对方的姓名等信息来初始化那个定义好的窗体
    为了以后能够更好的管理这些new出来的窗体,比如象QQ一样,出现N个聊天窗体,所以你得把这些窗体的引用或者句柄保存下来
      

  3.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.Net;
    using System.Net.Sockets;
    using System.ComponentModel;
    using System.Windows.Forms;
    using P2P;
    using ClientApp;namespace CreateFormApp
    {
    /// <summary>
    /// CreateForm 的摘要说明。
    /// </summary>
    public class CreateForm
    {
    public System.Windows.Forms.Form myForm;
    public System.Windows.Forms.Label labClient;
    public static System.Windows.Forms.RichTextBox richTextBox1;
    public System.Windows.Forms.Label labHost;
    public System.Windows.Forms.RichTextBox richTextBox2;
    public System.Windows.Forms.Button btnCancel;
    public System.Windows.Forms.Button btnOK; public static string otherName; //定义个委托
    public delegate void sendMessageDelegate(string otherName,string message);
    public sendMessageDelegate SendMessageDelegate = null;
    //
    // public string chatContents;
    /// <summary>
    /// 需要的传入的参数:窗体的标题,窗体的ID号
    ///                   你们俩的聊天记录,对方的基本信息
    ///                   client对象的各个基本信息
    /// </summary>
    public CreateForm(string formCaption,string formID,string clientName,string message)
    {
    //初始化窗体对象
    myForm = new Form();
    InitializeComponent();
    //设置控件的焦点在 richTextBox2 中
    // this.richTextBox2.Focus();
    otherName = clientName;
    SetFormCaption(formCaption,formID);
    SetClientInfo(clientName);
    //显示消息
    if(message != "")
    {
    richTextBox1.Text = clientName + ": ("+System.DateTime.Now.ToString()+")\n" + message + "\n";
    }

    //实例化委托
    SendMessageDelegate = new sendMessageDelegate(formClient.SendMessageFromCreateForm);
    } private void InitializeComponent()
    {
    this.labClient = new System.Windows.Forms.Label();
    richTextBox1 = new System.Windows.Forms.RichTextBox();
    this.labHost = new System.Windows.Forms.Label();
    this.richTextBox2 = new System.Windows.Forms.RichTextBox();
    this.btnCancel = new System.Windows.Forms.Button();
    this.btnOK = new System.Windows.Forms.Button();
    this.myForm.SuspendLayout();
    // 
    // labClient
    // 
    this.labClient.Location = new System.Drawing.Point(0, 0);
    this.labClient.Name = "labClient";
    this.labClient.Size = new System.Drawing.Size(136, 32);
    this.labClient.TabIndex = 0;
    this.labClient.Text = "XX说:";
    this.labClient.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
    // 
    // richTextBox1
    // 
    richTextBox1.Location = new System.Drawing.Point(0, 32);
    richTextBox1.Name = "rtbChatContents";
    richTextBox1.Size = new System.Drawing.Size(296, 144);
    richTextBox1.TabIndex = 1;
    richTextBox1.Enabled = true;
    richTextBox1.ReadOnly = true;
    richTextBox1.Text = "";
    richTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    // 
    // labHost
    // 
    this.labHost.Location = new System.Drawing.Point(0, 184);
    this.labHost.Name = "labHost";
    this.labHost.Size = new System.Drawing.Size(136, 24);
    this.labHost.TabIndex = 2;
    this.labHost.Text = "我说:";
    // 
    // richTextBox2
    // 
    this.richTextBox2.Location = new System.Drawing.Point(0, 208);
    this.richTextBox2.Name = "rtbMessage";
    this.richTextBox2.Size = new System.Drawing.Size(296, 96);
    this.richTextBox2.TabIndex = 3;
    this.richTextBox2.Text = "";
    this.richTextBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    // 
    // btnCancel
    // 
    this.btnCancel.Location = new System.Drawing.Point(24, 312);
    this.btnCancel.Name = "btnCancel";
    this.btnCancel.Size = new System.Drawing.Size(80, 24);
    this.btnCancel.TabIndex = 4;
    this.btnCancel.Text = "Cancel";
    this.btnCancel.Enabled = false;
    this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
    // 
    // btnOK
    // 
    this.btnOK.Location = new System.Drawing.Point(176, 312);
    this.btnOK.Name = "btnOK";
    this.btnOK.Size = new System.Drawing.Size(80, 24);
    this.btnOK.TabIndex = 5;
    this.btnOK.Text = "Send";
    this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
    // 
    // Form2
    // 
    this.myForm.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.myForm.ClientSize = new System.Drawing.Size(292, 341);
    this.myForm.Controls.Add(this.btnOK);
    this.myForm.Controls.Add(this.btnCancel);
    this.myForm.Controls.Add(this.richTextBox2);
    this.myForm.Controls.Add(this.labHost);
    this.myForm.Controls.Add(richTextBox1);
    this.myForm.Controls.Add(this.labClient);
    this.myForm.Name = "Form2";
    this.myForm.Text = "Form2";
    this.myForm.MaximizeBox = false;
    this.myForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
    this.myForm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.myForm.Closing += new System.ComponentModel.CancelEventHandler(myForm_Closing);
    this.myForm.ResumeLayout(false); } private void btnCancel_Click(object sender, System.EventArgs e)
    {
    this.myForm.Close();
    // this.talkObj.Stop();
    this.btnCancel.Enabled = false;
    } private void btnOK_Click(object sender, System.EventArgs e)
    {
    if(this.richTextBox2.Text == "")
    {
    MessageBox.Show("不能发送空消息!");
    return;
    }
    string info = ClientApp.formClient.localName + ": (" + System.DateTime.Now.ToString() + ")\n";
    string msg = this.richTextBox2.Text;
    info += msg;
    richTextBox1.Text += info;
    richTextBox1.Text += "\n";
    this.richTextBox2.Text = ""; //处理发送的message
    SendMessageDelegate(otherName,msg); this.btnCancel.Enabled =true;

    } public void SetFormCaption(string caption,string nameID)
    {
    this.myForm.Text = "与" + caption + "谈话中...";
    this.myForm.Name = nameID;
    } public void SetClientInfo(string clientName)
    {
    this.labClient.Text = clientName + "和您说:";
    } //如果以前的窗体已经存在,那将把现在的信息追加到 richTextBox1
    public void writeMsgToRichTxbx(string message)
    {
    string info = otherName + ": (" + System.DateTime.Now.ToString() + ")\n";
    info += message;
    richTextBox1.Text += info;
    richTextBox1.Text += "\n";
    richTextBox1.SelectionStart = richTextBox1.Text.Length;
    richTextBox1.Focus();
    this.richTextBox2.Focus();
    } private void myForm_Closing(object sender,System.ComponentModel.CancelEventArgs e)
    {
    //当关闭窗体时,应该把此窗体从HashTable 中删除
    ClientApp.formClient.htForms.Remove(this.myForm.Name);
    }
    }
    }
    /////////////////////////////////////////////////////////
    以上是我自己写的生成聊天窗体类
    下面是在主线程里处理new出来的聊天窗体
    if(!htForms.ContainsKey(chatID))
    {
    CreateForm chatForm = new CreateForm(chatName,chatID,chatName,"");
    htForms.Add(chatID,chatForm);
    chatForm.myForm.Show();
    chatForm.richTextBox2.Focus();
    }
    else
    {
    MessageBox.Show(this,"您已经打开了一个与" +  chatName + "聊天的窗体了,不能打开多个!","友情提示:",MessageBoxButtons.OK,MessageBoxIcon.Stop);

    }
      

  4.   

    请问你new 出一个聊天窗体是通过与界面交互 产生的还是 ,但接受到消息后,自动弹出窗体!!!!