在允许删除之前,有没有办法检查目标(它属于哪个.exe)?在允许删除之前检查目标窗口
我打算实现的是允许控制是拖&掉在Word或Excel中,并根据所目标应用是,通过相应的文件。
编辑: 这里是我在VB中尝试了代码
@大卫感谢代码。我用Button控件(WPF)尝试了类似的东西,并在数据对象中添加了一个文件路径。我得到一个堆栈不平衡异常。
这里是代码(尝试在VB.Net中) - 这是给我这个错误: 调用PInvoke函数'TestApplication!TestApplication.MainWindow :: GetCursorPos'已经失去了堆栈。这很可能是因为托管的PInvoke签名与非托管目标签名不匹配。检查PInvoke签名的调用约定和参数是否与目标非托管签名相匹配。
我忘记了什么吗?
编辑做了一些改变,现在它的工作。
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
我忘了提及我只是在我的例子中显示窗口标题。您可以使用proc.MainModule.FileName获取应用程序的.exe名称。 – David 2013-03-06 20:40:25
没错,但问题是你已经不得不告诉DragDrop你的数据对象是什么,我没有看到任何改变它的方法。我想你可以将正确类型的文件保存到没有扩展名的文件中,每次接收应用程序从反馈中改变。 – 2013-03-06 20:40:32
@David感谢您的代码。我用Button控件(WPF)尝试了类似的东西,并在数据对象中添加了一个文件路径。我得到一个堆栈不平衡异常。 – pingvj 2013-03-07 08:47:38