四联光电智能照明论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2511|回复: 0
打印 上一主题 下一主题

RichTextBox 文件拖动 

[复制链接]
  • TA的每日心情
    开心
    2022-6-10 09:59
  • 366

    主题

    741

    帖子

    9649

    积分

    超级版主

    Rank: 8Rank: 8

    积分
    9649
    跳转到指定楼层
    楼主
    发表于 2016-10-28 12:29:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    功能:设置可以拖拽的文件类型,将文本异步显示到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();
                 }
             }

         }
    }
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|Silian Lighting+ ( 蜀ICP备14004521号-1 )

    GMT+8, 2024-4-28 23:40 , Processed in 1.156250 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表