【问题标题】:Drag and drop an image into a RichTextBox将图像拖放到 RichTextBox 中
【发布时间】: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


【解决方案1】:

尝试摆脱 Sub New 函数中的 InitializeComponent() 调用。当我这样做时,我能够检测到 DragEnter 事件。这是我测试的代码(我创建了一个简单的 WinForm 并在其上放置了一个名为 DragAndDropTextBox 的 RichTextBox):

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DragAndDropTextBox.AllowDrop = True
End Sub

Private Sub DragAndDropTextBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragEnter

    Debug.Print("Entering text box region")

    ' 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

    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

End Class

当您将自己的自定义控件添加到表单时,InitializeComponent() 调用应该出现在您的代码中(我相信)。否则,我认为您不需要调用它。

【讨论】:

  • 我相信 InitializeComponent 调用会设置 UI,除非您手动创建所有控件,否则需要调用它。为了确定,我确实尝试将其删除,但找不到我的 RichTextBox。不过,谢谢你的建议。
【解决方案2】:

事实证明,当从 exe 运行代码时,拖放功能可以正常工作,但不能从 Visual Studio 中运行。更多搜索出现了this 答案,该答案指出当以管理员身份运行时,拖放在 Visual Studio 中不起作用。我以正常权限运行它,代码工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多