参考:http://blog.iordanov.info/?p=32

实现RichTextBox的拖放功能只要设置EnableAutoDragDrop = true就自动实现了,非常简单。但是要实现一些个性化的控制,就有一点复杂了。

我需要实现的是:如果拖放文件进来,不想默认的一样嵌入一个对象连接,而是把文件上传;其他情况就想默认一样。

首先得设定AllowDrop = true;但是vs的控件的事件设定里面是没有DragEnter和DragDrop事件的,只能手工写代码。

   //添加事件
    private void AddRtb1Event()
        {
            rtb1.AllowDrop = true;
            
            rtb1.DragEnter += new DragEventHandler(rtb1_DragEnter);
            rtb1.DragDrop += new DragEventHandler(rtb1_DragDrop);
            
        }       
        private void rtb1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy  ;
            String[] supportedFormats = e.Data.GetFormats(true);
            if (supportedFormats != null)
            {
                List<string> sfList = new List<string>(supportedFormats);
                if (sfList.Contains(DataFormats.FileDrop.ToString()) )
                {
                    rtb1.EnableAutoDragDrop = false;
                }
                else
                {
                    rtb1.EnableAutoDragDrop = true;

                }
            }

            
        }

        private void rtb1_DragDrop(object sender, DragEventArgs e)
        {
            String[] supportedFormats = e.Data.GetFormats(true);
            if (supportedFormats != null)
            {
                List<string> sfList = new List<string>(supportedFormats);
                if (sfList.Contains(DataFormats.FileDrop.ToString()))
                {
                    string[] fileList = (string[])((System.Array)e.Data.GetData(DataFormats.FileDrop));
                    foreach (string fileName in fileList)
                    {
                        MessageBox.Show(fileName);
                    }
                }
            }
            rtb1.EnableAutoDragDrop = true;
        }

    }

 

相关文章:

  • 2022-12-23
  • 2021-06-06
  • 2021-11-02
  • 2021-09-08
  • 2021-06-07
  • 2022-12-23
  • 2021-11-30
  • 2021-10-13
猜你喜欢
  • 2022-12-23
  • 2022-02-15
  • 2021-08-16
  • 2021-07-21
  • 2021-09-17
  • 2021-08-15
  • 2021-10-28
相关资源
相似解决方案