有没有办法在允许 drop 之前检查目标(它属于哪个 .exe)?
我打算实现的是允许将控件拖放到 Word 或 Excel 上,并根据目标应用程序,传递适当的文件。
编辑:这是我在 VB 中尝试过的代码
@David 感谢您的代码。我用按钮控件(WPF)尝试了类似的事情,并在数据对象中添加了一个文件路径。我遇到了堆栈不平衡异常。
这是代码(在 VB.Net 中尝试) - 这给了我这个错误:对 PInvoke 函数 'TestApplication!TestApplication.MainWindow::GetCursorPos' 的调用使堆栈不平衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查 PInvoke 签名的调用约定和参数是否与目标非托管签名匹配。
我忘了什么吗?
EDIT进行了一些更改,现在可以使用。
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Collections.Specialized
Class MainWindow
<DllImport("user32.dll")> _
Private Shared Function WindowFromPoint(ByVal xPoint As Integer, ByVal yPoint As Integer) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function GetCursorPos(lpPoint As Point) As Boolean
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function GetProcessId(hWnd As IntPtr) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetWindowThreadProcessId(hWnd As IntPtr, lpdwProcessId As Integer) As UInteger
End Function
Private MouseIsDown As Boolean = Nothing
Private Sub DropButton_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles DropButton.PreviewMouseLeftButtonDown
MouseIsDown = True
End Sub
Private Sub DropButton_MouseMove(sender As Object, e As MouseEventArgs) Handles DropButton.MouseMove
If MouseIsDown Then
Dim data As New DataObject()
Dim DropList As New StringCollection
DropList.Add("c:\file.txt")
data.SetFileDropList(DropList)
DragDrop.DoDragDrop(CType(e.OriginalSource, DependencyObject), data, DragDropEffects.Move)
End If
End Sub
Private Sub DropButton_GiveFeedback(sender As Object, e As GiveFeedbackEventArgs) Handles DropButton.GiveFeedback
Dim a = Mouse.GetPosition(Me)
If a <> Nothing Then
Dim hWnd As IntPtr = WindowFromPoint(a.X, a.Y)
If hWnd <> Nothing Then
Dim processId As Integer
GetWindowThreadProcessId(hWnd, processId)
Dim proc As Process = Process.GetProcessById(processId)
label1.Content = proc.MainWindowTitle
End If
End If
End Sub
End Class