📄 formsendmsg.cs
字号:
}
#endregion
#region 发送文件请求事件及其函数
private void butSendFile_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.OpenFileDialog fd=new OpenFileDialog();
fd.Filter ="所有文件|*.*";
if (fd.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
sendFileRequest(fd.FileName );
}
}
public void sendFileRequest(string fileName)//发送文件请求
{
//添加Tab
DevComponents.DotNetBar.TabControlPanel pa =new DevComponents.DotNetBar.TabControlPanel();
pa.Dock=System.Windows.Forms.DockStyle.Fill;
tabCsendFile.Controls.Add(pa);
DevComponents.DotNetBar.TabItem tabItem=new DevComponents.DotNetBar.TabItem(this.components);
tabItem.AttachedControl=pa;
tabItem.Text=Convert.ToString(this.tabCsendFile.Tabs.Count + 1) ;
bool isF;
for(int i=1;i<=this.tabCsendFile.Tabs.Count;i++)
{
isF=false;
foreach(DevComponents.DotNetBar.TabItem tItem in this.tabCsendFile.Tabs)
{
if(tItem.Text==i.ToString())
isF=true;
}
if(!isF)
{
tabItem.Text=i.ToString();goto xx;
}
}
xx:
tabCsendFile.Tabs.Add(tabItem);
//添加filesSend控件
LanMsg.Controls.filesSend fSend=new LanMsg.Controls.filesSend();
pa.Controls.Add(fSend);
System.IO.FileInfo f=new System.IO.FileInfo(fileName);
fSend.SetParameter(true,f.Name,fileName,f.Length,System.Net.IPAddress.Parse("127.0.0.1"),0,f.Extension);
fSend.fileSendCancel +=new LanMsg.Controls.filesSend.fileSendCancelEventHandler(fs_fileSendCancel);
fSend.fileSendEnd +=new LanMsg.Controls.filesSend.fileSendEndEventHandler(fs_fileSendEnd);
string fileInfo=f.Name +"|"+ f.Extension +"|"+ f.Length.ToString();//初次请求发送文件时要先发送“控件参数”到对方,请求对方创建“文件发送控件”并建立连接
fSend.Dock=System.Windows.Forms.DockStyle.Fill;
LanMsg.Controls.ClassMsg msg=new LanMsg.Controls.ClassMsg(10,this.FormMain.formMain.selfInfo.ID,System.Text.Encoding.Unicode.GetBytes(fileInfo));
//发送消息给对方,让对方接收文件
currUserInfo=this.FormMain.formMain.findUser(this.Tag.ToString());
if(currUserInfo!=null)
fSend.SendData(currUserInfo.IP ,currUserInfo.Port, new ClassSerializers().SerializeBinary(msg).ToArray());
AppendSystemRtf("等待对方接收文件 "+ f.Name +"("+ fSend.GetSizeStr(f.Length) +") 。请等待回应或取消文件传输");
//显示tab控件
this.tabCsendFile.Visible=true;
this.tabCsendFile.SelectedTab=tabItem;
this.tabCsendFile.Refresh();
}
#endregion
#region 接收文件 函数
public void ReceiveFileRequest(string fileName,string Extension,long FileSize,System.Net.IPAddress Ip,int Port)//接收文件请求
{
//添加Tab
DevComponents.DotNetBar.TabControlPanel pa =new DevComponents.DotNetBar.TabControlPanel();
pa.Dock=System.Windows.Forms.DockStyle.Fill;
tabCsendFile.Controls.Add(pa);
DevComponents.DotNetBar.TabItem tabItem=new DevComponents.DotNetBar.TabItem(this.components);
tabItem.AttachedControl=pa;
tabItem.Text=Convert.ToString(this.tabCsendFile.Tabs.Count + 1) ;
bool isF;
for(int i=1;i<=this.tabCsendFile.Tabs.Count;i++)
{
isF=false;
foreach(DevComponents.DotNetBar.TabItem tItem in this.tabCsendFile.Tabs)
{
if(tItem.Text==i.ToString())
isF=true;
}
if(!isF)
{
tabItem.Text=i.ToString();goto xx;
}
}
xx:
tabCsendFile.Tabs.Add(tabItem);
//添加filesSend控件
LanMsg.Controls.filesSend fSend=new LanMsg.Controls.filesSend();
pa.Controls.Add(fSend);
fSend.SetParameter(false,fileName,fileName,FileSize,Ip,Port,Extension);
fSend.fileSendCancel +=new LanMsg.Controls.filesSend.fileSendCancelEventHandler(fs_fileSendCancel);
fSend.fileSendEnd +=new LanMsg.Controls.filesSend.fileSendEndEventHandler(fs_fileSendEnd);
fSend.fileSend +=new LanMsg.Controls.filesSend.fileSendEventHandler(fSend_fileSend);
fSend.Dock=System.Windows.Forms.DockStyle.Fill;
AppendSystemRtf("对方要传文件 "+ fileName +"("+ fSend.GetSizeStr(FileSize) +")" +" 给您,请接收或取消文件传输");
//显示tab控件
this.tabCsendFile.Visible=true;
this.tabCsendFile.SelectedTab=tabItem;
this.tabCsendFile.Refresh();
this.panelAVchat.Visible=false;//将视频对话面板隐藏,以便用户接收或取消文件传输
}
#endregion
#region 文件发送取消、文件发送结束 事件
private void fs_fileSendCancel(object sender, bool isSelf)
{
LanMsg.Controls.filesSend FileSend=(sender as LanMsg.Controls.filesSend);
if(isSelf)
AppendSystemRtf("您已经取消了文件 "+FileSend.labFileName.Text +" 的传输");
else
AppendSystemRtf("对方已经取消了文件 "+FileSend.labFileName.Text +" 的传输");
delTabItem(FileSend);//删除文件传输Tab控件
if(!this.ButAVchat.Enabled)this.panelAVchat.Visible=true;//显示视频对话面板
}
private void fSend_fileSend(object sender, bool isSelf)//文件发送事件
{
if(!this.ButAVchat.Enabled)this.panelAVchat.Visible=true;//显示视频对话面板
LanMsg.Controls.filesSend FileSend=(sender as LanMsg.Controls.filesSend);
if(isSelf)
AppendSystemRtf("您已经接收了文件 "+FileSend.labFileName.Text +" 的传输,文件正在传输中...");
else
AppendSystemRtf("对方已经接收了文件 "+FileSend.labFileName.Text +" 的传输,文件正在传输中...");
}
private void fs_fileSendEnd(object sender, bool isSelf)
{
LanMsg.Controls.filesSend FileSend=(sender as LanMsg.Controls.filesSend);
if(isSelf)
AppendSystemRtf("文件 "+ FileSend.labFileName.Text +" 已经传输完成");
else
AppendSystemRtf("文件 "+ FileSend.labFileName.Text +" 已经传输完成,保存路径<file:\\\\"+ FileSend.FileName +">" );
delTabItem(FileSend);//删除文件传输Tab控件
}
private void AppendSystemRtf(string str)
{
LanMsg.MyExtRichTextBox rich=new MyExtRichTextBox();
rich.AppendText("\n " );
rich.InsertImage(this.FormMain.formMain.imageList1.Images[18]);
rich.AppendText(" "+str);
rich.ForeColor=Color.Brown;
this.RTBRecord.AppendRtf (rich.Rtf);
rich.Clear();
rich.Dispose();
}
private void delTabItem(LanMsg.Controls.filesSend FileSend)//删除文件传输Tab控件
{
try
{
DevComponents.DotNetBar.TabControlPanel tPanel=(FileSend.Parent as DevComponents.DotNetBar.TabControlPanel);
tPanel.Text="0" ;
foreach(DevComponents.DotNetBar.TabItem tabItem in this.tabCsendFile.Tabs)
if((tabItem.AttachedControl as DevComponents.DotNetBar.TabControlPanel).Text=="0")
{
this.tabCsendFile.Tabs.Remove(tabItem);
FileSend.Dispose();
tPanel.Dispose();
tabItem.Dispose();
this.tabCsendFile.Refresh();
return;
}
}
catch
{
}
}
private void tabCsendFile_TabRemoved(object sender, System.EventArgs e)
{
if(this.tabCsendFile.Tabs.Count==0)
this.tabCsendFile.Visible=false;
}
#endregion
#region 重新绘制 richbox 事件
private void RTBRecordOnFrameChanged(object sender, EventArgs e)
{
this.RTBRecord.Invalidate();
}
private void OnFrameChanged(object sender, EventArgs e)
{
this.RTBSend.Invalidate();
}
#endregion
# region 显示 对话历史 按钮事件
private void butRecordshow_Click(object sender, System.EventArgs e)
{
this.currUserInfo=this.FormMain.formMain.findUser(this.Tag.ToString());
if(this.currUserInfo!=null)
this.FormMain.formMain.OpenMsgMis(this.currUserInfo.ID);
}
#endregion
#region 当 文本消息 发送成功后将自己发送的消息加入历史RICHBOX 函数
public void newTextMsg(string title,Font titleFont,Color titleColor)//将发送的消息加入历史rich
{
LanMsg.MyExtRichTextBox rich =new MyExtRichTextBox();
rich.AppendText(title);
rich.Font=titleFont;
rich.ForeColor=titleColor;
this.RTBRecord.AppendRtf(rich.Rtf);
this.RTBRecord.AppendText(" ");
LanMsg.ClassTextMsg textMsg=this.SendTextMsg;
int iniPos=this.RTBRecord.TextLength;//获得当前记录richBox中最后的位置
rich.Clear();
rich.Dispose();
if(textMsg.ImageInfo!="")//如果消息中有图片,则添加图片
{
string[] imagePos=textMsg.ImageInfo.Split('|');
int addPos=0;//
int currPos=0;//当前正要添加的文本位置
int textPos=0;
for(int i=0;i<imagePos.Length-1;i++)
{
string[] imageContent=imagePos[i].Split(',');//获得图片所在的位置、图片名称、图片宽、高
currPos=Convert.ToInt32(imageContent[0]);
this.RTBRecord.AppendText(textMsg.MsgContent.Substring(textPos,currPos-addPos));
this.RTBRecord.SelectionStart=this.RTBRecord.TextLength;
textPos += currPos-addPos ;
addPos += currPos-addPos;
LanMsg.MyPicture pic=new MyPicture();
pic.BackColor=this.RTBRecord.BackColor;
pic.SizeMode=System.Windows.Forms.PictureBoxSizeMode.AutoSize;
if(Convert.ToUInt32(imageContent[1])<96)
pic.Image=System.Drawing.Image.FromStream(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("LanMsg.Resources."+ imageContent[1] +".gif")) ;
else
{
pic.Tag=imageContent[1];
pic.Image=this.findPic(imageContent[1],this.SendGifs).Image;
}
System.Drawing.ImageAnimator.Animate(pic.Image,new System.EventHandler(this.RTBRecordOnFrameChanged));
this.RTBRecord.InsertMyControl(pic);
addPos ++;
}
this.RTBRecord.AppendText(textMsg.MsgContent.Substring(textPos,textMsg.MsgContent.Length-textPos) +"\n");
}
else//如果消息中没有图片,则直接添加消息文本
{
this.RTBRecord.AppendText(textMsg.MsgContent +"\n");
}
this.RTBRecord.Focus();
this.RTBRecord.Select(iniPos,this.RTBRecord.TextLength-iniPos);
this.RTBRecord.SelectionFont=textMsg.font ;
this.RTBRecord.Select(iniPos,this.RTBRecord.TextLength-iniPos);
this.RTBRecord.SelectionColor=textMsg.color;
this.RTBSend.Focus();
}
private LanMsg.ClassTextMsg GetSendTextMsg()//获得要发送的序列化字串
{
LanMsg.ClassTextMsg textMsg=new ClassTextMsg();
LanMsg.MyExtRichTextBox rich=new MyExtRichTextBox();
rich.Rtf=this.RTBSend.Rtf ;
textMsg.MsgContent =rich.Text;//获得消息内容
textMsg.font=this.RTBSend.Font;//获得文本字体
textMsg.color=this.RTBSend.ForeColor;//获得文本颜色
REOBJECT reObject = new REOBJECT();
LanMsg.MyPicture pic;
for (int i=0 ; i<this.RTBSend.GetRichEditOleInterface().GetObjectCount();i++)
{
this.RTBSend.GetRichEditOleInterface().GetObject(i, reObject, GETOBJECTOPTIONS.REO_GETOBJ_ALL_INTERFACES);
pic=this.findPic(reObject.dwUser.ToString(),this.SendGifs);
if(pic!=null)
{
pic.IsSent=true;//发送此图片
textMsg.ImageInfo += reObject.cp.ToString() +","+reObject.dwUser.ToString()+ ","+ pic.Image.Size.Width.ToString() +","+pic.Image.Size.Height.ToString() + "|";
}
else
{
textMsg.Imag
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -