四联光电智能照明论坛

标题: RichTextBox 文件拖动  [打印本页]

作者: gxm771208    时间: 2016-10-28 12:29
标题: RichTextBox 文件拖动 
功能:设置可以拖拽的文件类型,将文本异步显示到RichTextBox。
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

class gxRichTextBox:System.Windows.Forms.RichTextBox
{

     private delegate void ReadFileOneByOne(string FilePath);
     public gxRichTextBox()
     {
         this.AllowDrop = true;//接受拖拽
         this.AcceptsTab = true;//接受TAB键
         this.DragDrop += new System.Windows.Forms.DragEventHandler(gxRichTextBox_DragDrop);
         this.DragEnter += new System.Windows.Forms.DragEventHandler(gxRichTextBox_DragEnter);
     }

     private string _gxFilter = "txt,sql,asm";
     [Description("可以打开的文件类型,以 ,分割开")]
     public string GxFilter
     {
         get { return _gxFilter; }
         set { _gxFilter = value; }
     }

     void gxRichTextBox_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
     {
         if (e.Data.GetDataPresent(DataFormats.FileDrop))
         {
             e.Effect = DragDropEffects.Copy;
         }
         else
         {
             e.Effect = DragDropEffects.None;
         }
     }

     void gxRichTextBox_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
     {
         string filepath = ((string[])e.Data.GetData(DataFormats.FileDrop))[0].ToString();
         ReadFileOneByOne rf = new ReadFileOneByOne(ReadFile);
         rf.BeginInvoke(filepath, null, null);
     }

     void ReadFile(string filepath)
     {
         byte[] buffer = new byte[1024 * 8];//每次读取多少
         long f_len = 0; //文件流的当前位置
         string fileExt = System.IO.Path.GetExtension(filepath).ToUpper();//文件类型
         foreach (string str in _gxFilter.ToUpper().Split(','))
         {
             if (fileExt == "."+str)
             {
                 System.IO.FileStream fl = null;
                 try
                 {
                     fl = new System.IO.FileStream(filepath, System.IO.FileMode.Open);
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show(ex.Message);
                 }
                 this.Text = string.Empty;
                 int effect = 0;//校验文件是否读取完毕的状态
                 do
                 {
                     effect = fl.Read(buffer, 0, buffer.Length);//循环读取文件
                     f_len += effect;                  //累加校验和
                     this.AppendText(Encoding.Default.GetString(buffer));//边读边显示
                     fl.Seek(f_len, System.IO.SeekOrigin.Begin);//设置读取指针的当前位置
                 } while (effect == buffer.Length);
                 byte[] lastbuffer = new byte[fl.Length % buffer.Length];
                 fl.Read(lastbuffer, 0, lastbuffer.Length); //截取最后没有读取的部分
                 if (fl.Length != f_len)
                 {
                     MessageBox.Show("文件数据丢失!");
                 }
                 this.AppendText(Encoding.Default.GetString(lastbuffer));//显示最后一部分
                 fl.Close();
                 fl.Dispose();
             }
         }

     }
}




欢迎光临 四联光电智能照明论坛 (http://5xhome.com/) Powered by Discuz! X3.2