⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formsendmsg.cs

📁 kuiMsg是一款用.net C# 开发的即时消息开源软件,适合.net即时消息软件开发者用。 主要功能: 支持文件传输(p2p); 支持GIF动画表情( 彻底消除闪屏 :) );
💻 CS
📖 第 1 页 / 共 4 页
字号:
            this.butSend.Enabled = t;
            this.RTBSend.ReadOnly = !t;
            OutTime = 0;
            this.timerCheckSendIsSuccess.Enabled = !t;//开始检测消息是否发送成功
        }

        #endregion

        #region 检查 消息是否发送成功 timer事件
        private void timerCheckSendIsSuccess_Tick(object sender, System.EventArgs e)
        {
            if (currUserInfo != null)
            {
                OutTime++;
                if (OutTime == 50 && !currUserInfo.SendIsSuccess)//如果消息没有发送成功
                {
                    MessageBox.Show("消息发送不成功(原因可能是对方已经脱机或网络出现故障)。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    EnBut(true);//启用发送功能
                    this.RTBSend.Focus();
                }

                if (currUserInfo.SendIsSuccess)//如果消息发送成功 
                {
                    EnBut(true);//启用发送功能

                    if (this.RTBSend.Text != "")
                        dataAccess.addMsgToDB(this.SendTextMsg.msgContent, FormAccess.selfInfo.UserID, currUserInfo.UserID, this.SendTextMsg.Version.ToString(), System.DateTime.Now.ToString(), this.SendTextMsg.imageInfo, true);//将消息添加进数据库

                    this.MsgToRichTextBox(this.SendTextMsg, true);//将消息加入到richTextBox控件

                    this.sendSelfImage();//发送附加的自定义图片
                    this.RTBSend.Clear();
                }
            }
        }
        #endregion

        #region 发送自定义的图片文件
        private void sendSelfImage()//发送图片文件
        {
            foreach (IMLibrary.MyPicture pic in this.SendGifs)
            {
                if (pic.IsSent)
                {
                    currUserInfo = FormAccess.findUser(this.Tag.ToString());
                    if (currUserInfo != null)
                    {
                        System.IO.FileInfo f = new System.IO.FileInfo(Application.StartupPath + "\\sendImage\\" + pic.Tag.ToString() + ".gif");
                        this.ImageTransfers(true,pic.Tag.ToString(), f.FullName, f.Extension, f.Length, currUserInfo.IP, currUserInfo.Port);
                    }
                }
            }
            this.SendGifs.Clear();
        }
        #endregion

        #region 发送或接收图片文件 函数
        /// <summary>
        /// 发送或接收文件
        /// </summary>
        /// <param name="IsSend">标记是发送还是接收</param>
        /// <param name="fileName">接收或发送的文件名</param>
        /// <param name="Extension">接收或发送文件的扩展名</param>
        /// <param name="FileSize">文件尺寸</param>
        /// <param name="Ip">接收者或发送者的IP地址</param>
        /// <param name="Port">接收者或发送者的UDP端口号</param>
        public void ImageTransfers(bool IsSend, string FileName, string fullFileName, string Extension, long FileSize, System.Net.IPAddress Ip, int Port)//接收文件请求
        { 
            //添加filesSend控件
            IMLibrary.p2pFileTransmit imageTransmit = new IMLibrary.p2pFileTransmit();
            imageTransmit.fileTransmitted += new IMLibrary.p2pFileTransmit.fileTransmittedEventHandler(imageTransmit_fileTransmitted);//图片传输结束
            imageTransmit.fileTransmitCancel += new IMLibrary.p2pFileTransmit.fileTransmitCancelEventHandler(imageTransmit_fileTransmitCancel);//图片传输取消或异常中断

            if (IsSend)
            {
                string fileInfo = FormAccess.selfInfo.UserID + "|" + FileName + "|" + Extension + "|" + FileSize.ToString();//初次请求发送文件时要先发送“控件参数”到对方,请求对方创建“文件发送控件”并建立连接
                byte[] fileinfo = IMLibrary.TextEncoder.textToBytes(fileInfo);
                byte[] buf = new byte[fileinfo.Length + 2];
                buf[0] = 255;//合法用户标识
                buf[1] = 25;//消息类型=发送图片
                Buffer.BlockCopy(fileinfo, 0, buf, 2, fileinfo.Length);
                imageTransmit.SetParameter(true, fullFileName, FileName, (int)FileSize, currUserInfo.IP, currUserInfo.Port, Extension, (ushort)(currUserInfo.MTU - 200), buf);
            }
            else
            {
                System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(Application.StartupPath + "\\ArrivalImage");
                if (!dInfo.Exists)
                    dInfo.Create();

                imageTransmit.SetParameter(false, fullFileName, FileName, (int)FileSize, Ip, Port, Extension, 750, null);
                System.Threading.Thread.Sleep(2000);
                imageTransmit.startIncept(Application.StartupPath + "\\ArrivalImage\\" + FileName + Extension);//自动接收图片
            }
        }

        void imageTransmit_fileTransmitCancel(object sender, fileTransmitEvnetArgs e)
        {
            IMLibrary.p2pFileTransmit p2pImage  = sender as IMLibrary.p2pFileTransmit;
            if (p2pImage != null) p2pImage.Dispose();
        }

        void imageTransmit_fileTransmitted(object sender, fileTransmitEvnetArgs e)
        {
            if (!e.isSend)//如果是图片接收者,则将传输完成的图片显示出来
            {
                IMLibrary.MyPicture pic = this.findPic(Convert.ToString(e.fileName), this.ArrivalGifs);
                if (pic != null)
                {
                    pic.Image = System.Drawing.Image.FromFile(e.fullFileName);//显示图片
                    this.RTBRecord.Invalidate();
                }
            }

            IMLibrary.p2pFileTransmit p2pImage = sender as IMLibrary.p2pFileTransmit;
            if (p2pImage != null) p2pImage.Dispose();//将图片传输组件删除
        }

        #endregion

        #region RichBox 关联 菜单事件
        private void dotNetBarManager1_ItemClick(object sender, System.EventArgs e)
        {
            switch ((sender as DevComponents.DotNetBar.ButtonItem).Name)
            {
                case "复制":
                    if (this.RTBRecord.Focus())
                        this.RTBRecord.Copy();
                    if (this.RTBSend.Focus())
                        this.RTBSend.Copy();
                    break;
                case "粘贴":
                    if (this.RTBRecord.Focus())
                    { }
                    if (this.RTBSend.Focus())
                        this.RTBSend.Paste();
                    break;
                case "全选":
                    if (this.RTBRecord.Focus())
                        this.RTBRecord.SelectAll();
                    if (this.RTBSend.Focus())
                        this.RTBSend.SelectAll();
                    break;
                case "剪切":
                    if (this.RTBRecord.Focus())
                    { }
                    if (this.RTBSend.Focus())
                        this.RTBSend.Cut();
                    break;

            }
        }

        #endregion

        #region RichBox 超链接 单击事件
        private void RTBRecord_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(e.LinkText);
            }
            catch
            {
                //MessageBox.Show("无法打开链接。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }
        #endregion

        #region 对话记录richbox 文本改变后 事件
        private void RTBRecord_TextChanged(object sender, System.EventArgs e)
        {
            //this.RTBRecord.Focus();
            //this.RTBRecord.Select(this.RTBRecord.Text.Length, 1);
            //this.RTBSend.Focus();
        }
        #endregion

        private void RTBfocus()
        {
            this.RTBRecord.Focus();
            //this.RTBRecord.Select(this.RTBRecord.Text.Length, 0);
            this.RTBRecord.ScrollToCaret();
            this.RTBSend.Focus();
        }

        #region 打开共享 按钮单击事件
        private void butOpenShared_Click(object sender, System.EventArgs e)
        {
            try
            {
                string sharedComputerName = "\\\\" + Convert.ToString(this.Tag);
                System.Diagnostics.Process.Start(sharedComputerName);
            }
            catch
            {
                MessageBox.Show("无法打开对方(" + Convert.ToString(this.Tag) + ")的共享文件夹(原因可能是对方没有开机或没有设置共享以及其它网络故障造成的)。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion

        #region 发送文件请求事件及其函数

        private void butSendFile_Click(object sender, System.EventArgs e)
        {
            currUserInfo = FormAccess.findUser(this.Tag.ToString() );
            if (currUserInfo == null)return;

            System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
            fd.Multiselect = true;
            fd.Filter = "所有文件|*.*";
            if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                for (int i = 0; i < fd.FileNames.Length; i++)
                {
                    System.IO.FileInfo f = new System.IO.FileInfo(fd.FileNames [i]);
                    this.FileTransfers(true, f.Name, f.FullName, f.Extension, f.Length, currUserInfo.IP, currUserInfo.Port);
                }
            }
        }

        #endregion
         
        #region 发送或接收文件 函数
        /// <summary>
        /// 发送或接收文件
        /// </summary>
        /// <param name="IsSend">标记是发送还是接收</param>
        /// <param name="fileName">接收或发送的文件名</param>
        /// <param name="Extension">接收或发送文件的扩展名</param>
        /// <param name="FileSize">文件尺寸</param>
        /// <param name="Ip">接收者或发送者的IP地址</param>
        /// <param name="Port">接收者或发送者的UDP端口号</param>
        public void FileTransfers(bool IsSend, string  FileName, string fullFileName, string Extension, long FileSize, System.Net.IPAddress Ip, int Port)//接收文件请求
        {
            //添加Tab

            DevComponents.DotNetBar.TabItem tabItem = this.tabCsendFile.CreateTab(Convert.ToString(this.tabCsendFile.Tabs.Count + 1), this.tabCsendFile.Tabs.Count);

            //添加filesSend控件
            p2pFileTransmit fSend = new p2pFileTransmit();
            tabItem.AttachedControl.Controls.Add(fSend);

            if (IsSend)
            {
                string fileInfo = FormAccess.selfInfo.UserID + "|" +  FileName + "|" + Extension + "|" + FileSize.ToString();//初次请求发送文件时要先发送“控件参数”到对方,请求对方创建“文件发送控件”并建立连接
                byte[] fileinfo = IMLibrary.TextEncoder.textToBytes(fileInfo);
                byte[] buf = new byte[fileinfo.Length + 2];
                buf[0] = 255;//合法用户标识
                buf[1] = 14;//消息类型=发送文件
                Buffer.BlockCopy(fileinfo, 0, buf, 2, fileinfo.Length);
                fSend.SetParameter(true ,fullFileName, FileName, (int)FileSize, currUserInfo.IP, currUserInfo.Port, Extension, (ushort)(currUserInfo.MTU - 200), buf);

                AppendSystemRtf("等待对方接收文件 " +  FileName + "(" + IMLibrary.FileOpt.GetSizeStr((int)FileSize) + ") 。请等待回应或取消文件传输");
            }
            else
            {
                fSend.SetParameter(false , fullFileName, FileName,(int)FileSize, Ip, Port, Extension, 750, null);
                AppendSystemRtf("对方要传文件 " +  FileName + "(" + IMLibrary.FileOpt.GetSizeStr((int)FileSize) + ")" + " 给您,请接收或取消文件传输");
            }

            fSend.fileTransmitBefore += new p2pFileTransmit.fileTransmitBeforeEventHandler(fSend_fileTransmitBefore);//文件开始传输事件
            fSend.fileTransmitCancel += new p2pFileTransmit.fileTransmitCancelEventHandler(fSend_fileTransmitCancel);//文件传输取消事件
            fSend.fileTransmitError += new p2pFileTransmit.fileTransmitErrorEventHandler(fSend_fileTransmitError);//文件传输错误事件 
           // fSend.fileTransmitOutTime += new p2pFileTransmit.fileTransmitOutTimeEventHandler(fSend_fileTransmitOutTime);//文件传输超时事件
            fSend.fileTransmitted += new p2pFileTransmit.fileTransmittedEventHandler(fSend_fileTransmitted);//文件传输完成事件 

            fSend.Dock = System.Windows.Forms.DockStyle.Fill;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -