【发布时间】:2015-07-24 20:28:41
【问题描述】:
我正在更新在 VB 4 中完成的代码,其中我有一个 RichTextBox。我需要能够将图像从 Windows 资源管理器拖放到 RTB。不幸的是,我无法让拖放工作。
我创建了一个更简单的 Windows 窗体程序来尝试解决此问题,但没有取得任何进展。我首先将 AllowDrop 设置为 True。
Public Sub New()
InitializeComponent()
Me.DragAndDropTextBox.AllowDrop = True
End Sub
然后我为 RTB 创建处理程序。这些直接取自MSDN。
Private Sub DragAndDropTextBox_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub DragAndDropTextBox_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragDrop
System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragDrop
Dim img As Image
img = Image.FromFile(e.Data.GetData(DataFormats.FileDrop, False))
Clipboard.SetImage(img)
Me.DragAndDropTextBox.SelectionStart = 0
Me.DragAndDropTextBox.Paste()
End Sub
当我在资源管理器中抓取一张图像并将其拖到我的窗口上时,我得到一个带有斜线的圆圈。我在每个处理程序的第一行设置了断点,但它们永远不会到达。我看了几页,它们似乎都给出了相同的过程,所以我一定遗漏了一些简单的东西。
我现在并不担心将图像粘贴到文本框中;我知道我需要为此努力。我只是想捕捉图像,但似乎没有调用处理程序方法。
更新
经过大量实验,我发现实际问题出在我的 Visual Studio 2010 上,我始终以管理员身份运行它。当我从 exe 运行程序时,拖放工作。当我尝试在调试中从 VS 运行时,它没有。有没有人经历过这种情况?
如果有人能对此有所了解,我将不胜感激。
【问题讨论】:
-
您在 DragEnter 中的代码仅将其设置为接受
DataFormats.Text。图片不是文字 -
当您从 Windows 资源管理器中拖放“图像”时,实际上您拖放的只是一个包含图像的文件。所以你的 drop source 必须支持 drop 文件。并且您必须自己处理丢弃的文件并加载图像。
-
这是一个很好的观点,我应该明白这一点。我对代码进行了更改,但我仍然没有运气。正如我所说,没有到达断点,所以我假设 DragEnter 和 DragDrop 方法实际上从未被调用过。
标签: vb.net image visual-studio-2010 drag-and-drop windows-explorer