目标是让Drag n Drop操作类似于从Windows资源管理器中拖动文件并将其放入Word(或类似文件)中。问题是,当我尝试我的代码时,我得到两个不同的结果。我已经搜索并尝试了各种方法来解决问题,但我无法实现一个非常好的解决方案。这是我当前的Drag n Drop函数代码:
'Drag n Drop
Private Sub IconBoxMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles IconBox.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim dataObj As New DataObject(DataFormats.FileDrop, New String() {IconPath})
LayoutBox.DoDragDrop(dataObj, DragDropEffects.Move)
End If
End Sub
结果如下:
如果将文件从Windows资源管理器拖放到Word,则在左侧。在右侧,如果您将我的代码拖放到Word。我还尝试了其他DataFormats以查看它是否会改变某些内容,然后单词(或类似的)不接受该文件。
答案 0 :(得分:0)
我刚刚找到了解决方案。神奇的答案是.emf。
这是我的最终代码:
'Drag n Drop
Private Sub IconBoxMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles IconBox.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim emfTempPath As String = Path.GetTempFileName()
Try
Dim svgIcon As SvgDocument = SvgDocument.Open(IconPath)
Using bufferGraphics As Graphics = Graphics.FromHwndInternal(IntPtr.Zero)
Using metafile = New Metafile(emfTempPath, bufferGraphics.GetHdc())
Using graphics As Graphics = Graphics.FromImage(metafile)
svgIcon.Draw(graphics)
End Using
End Using
End Using
Dim dataObj As New DataObject(DataFormats.FileDrop, New String() {emfTempPath})
LayoutBox.DoDragDrop(dataObj, DragDropEffects.Move)
Finally
File.Delete(emfTempPath)
End Try
End If
End Sub