现有一个很棘手的问题,,从远程接受到一个消息在本地初始化一个窗体,怎么实现,,各位帮帮忙,,,
如QQ那样,发起视频邀请 ,,它是怎么动态生成窗体的!!谢谢各位!!!
如QQ那样,发起视频邀请 ,,它是怎么动态生成窗体的!!谢谢各位!!!
解决方案 »
- C#事务Transactions
- textBox 为空的或者字数大于30个字 提示内容为空和字数大于30个字
- Arraylist+gridview 一直不能获取有效值???
- winform 窗体。。急急急。。。
- 高手帮个忙。。Repeater 中襄套一个GridView,怎样编辑GridView中行的信息。。
- 高手帮我看下我这C#怎么连接不上SQL
- 打字软件中文件文字变色效果的实现
- 构造函数中有异常还能实例化正确而且还能执行代码?这神一样的代码是怎么写的?
- Web页面上有一个<asp:panel>,是隐藏的,按下一个button后panel会显示出来,现在要在它显示出来后自动将光标定位到panel上面的<asp:textb
- 一个程序运行不了的问题,急急!(在线等待)
- Cookie 中不能保存换行符?
- 怎样访问本机某个文件的属性
你的问题,我碰到过!
首先在本机器上定义好窗体,当你接受对方的消息时,就利用对方的姓名等信息来初始化那个定义好的窗体
为了以后能够更好的管理这些new出来的窗体,比如象QQ一样,出现N个聊天窗体,所以你得把这些窗体的引用或者句柄保存下来
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);
}