实现将文件拖拽到richtextbox进行显示

            this.richTextBox1.AllowDrop = true;//设置允许拖拽
            this.richTextBox1.DragEnter += new DragEventHandler(this.richTextBox1_DragEnter);//注册事件
            this.richTextBox1.DragDrop += new DragEventHandler(this.richTextBox1_DragDrop);

        private void richTextBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }


        private void richTextBox1_DragDrop(object sender, DragEventArgs e)
        {
            Array arrayfilename = (Array)e.Data.GetData(DataFormats.FileDrop);//有可能直接拖拽很多文件 这里生成一个所有文件全路径的列表

            string filename = arrayfilename.GetValue(0).ToString();//选择第一个文件 当然循环可实现加载所有文件

            using (StreamReader sr = new StreamReader(filename, System.Text.Encoding.Default))
            {
                string strred = sr.ReadToEnd().ToString();
                richTextBox1.Text = strred;
            }
        }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常感谢您的提问。关于您的问题,可以通过以下代码实现: ```c# public partial class Form1 : Form { [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); [DllImport("user32.dll")] static extern IntPtr LoadImage(IntPtr hInst, string lpszName, uint uType, int cxDesired, int cyDesired, uint fuLoad); private const uint WM_SETREDRAW = 0x000B; private const uint OIC_DOCUMENT = 0x00000003; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //打开文件对话框 OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "NC files (*.nc)|*.nc"; openFileDialog1.Title = "Select a NC File"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { richTextBox1.Clear(); IntPtr hIcon = LoadImage(IntPtr.Zero, "shell32.dll", OIC_DOCUMENT, 16, 16, 0x00000000); richTextBox1.InsertImage(hIcon, new FileInfo(openFileDialog1.FileName).Name + "\n"); StreamReader sr = new StreamReader(openFileDialog1.FileName); richTextBox1.AppendText(sr.ReadToEnd()); sr.Close(); } } private void richTextBox1_VScroll(object sender, EventArgs e) { SendMessage(richTextBox1.Handle, WM_SETREDRAW, 0, 0); richTextBox1.Invalidate(); SendMessage(richTextBox1.Handle, WM_SETREDRAW, 1, 0); } } public static class RichTextBoxExtensions { public static void InsertImage(this RichTextBox box, Image image, string caption) { Clipboard.SetImage(image); if (box.CanPaste(DataFormats.GetFormat(DataFormats.Bitmap))) { box.SelectionStart = 0; box.Paste(); box.AppendText(caption + "\n"); } } } ``` 在这个例子中,利用SendMessage函数和WM_SETREDRAW消息实现了快速滚动和加速地绘制内容。同时,用InsertImage函数将NC文件的图标插入到RichTextBox中。 希望能够帮到您,感谢您的提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值