您的位置:首页 > 其它

winform实现拖曳功能。.net拖曳实现 drag

2009-07-17 22:21 337 查看
平时使用的软件很多都有拖曳的功能,感觉功能很强大,用户体验非常好.

例如:使用EditPlus的时候,可以在某个文件夹中一次选取好几个文件,然后直接拖到EditPlus里面(当然,EditPlus要先开起来)。它就会自动创建N个窗口,把这些文件读取进去。

又例如:.net反编译工具Reflector。可以将需要反编译的.exe或.dll直接拖到Reflector里面,它就会读取这个.exe或.dll将其反编译出来。操作非常方便。

以下,是我用vb.net做的一个小例子。贴出核心代码。

1。新建一个winform工程。

2。在表单上拖入一个System.Windows.Forms.ListBox。命名为:listExeOrDll

3。实现事件DragOver和DragDrop

贴出代码如下:

DragOver事件代码

]'把对象拖进控件,但未放开鼠标按键时触发
Private Sub listExeOrDll_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles listExeOrDll.DragOver
'如果拖進來的不是一個文件對象,就返回
If Not TypeName(e.Data).Equals("DataObject") Then
e.Effect = DragDropEffects.None
Return
End If
'如果拖进来的对象不包含文件,就退出此过程
Dim dtobj As DataObject = CType(e.Data, DataObject)
If Not dtobj.ContainsFileDropList Then
e.Effect = DragDropEffects.None
Return
End If
If ((e.KeyState And (8 + 32)) = (8 + 32) And _
e.KeyState And DragDropEffects.Link = DragDropEffects.Link) Then
'KeyState 8+32 = CTRL + ALT

'link drag - and - drop effect.
e.Effect = DragDropEffects.Link
ElseIf ((e.KeyState And 32) = 32 And _
(e.AllowedEffect And DragDropEffects.Link) = DragDropEffects.Link) Then

'ALT KeyState for link
e.Effect = DragDropEffects.Link
ElseIf ((e.KeyState And 4) = 4 And _
(e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move) Then

'Shift KeyState for move
e.Effect = DragDropEffects.Move

ElseIf ((e.KeyState And 8) = 8 And _
(e.AllowedEffect And DragDropEffects.Copy) = DragDropEffects.Copy) Then

'CTRL KeyState for copy
e.Effect = DragDropEffects.Copy

ElseIf ((e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move) Then

e.Effect = DragDropEffects.Move

End If

End Sub
'DragDrop事件.拖放完成时触发
Private Sub listExeOrDll_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles listExeOrDll.DragDrop
Dim tpname As String = TypeName(e.Data)
If tpname.Equals("DataObject") Then
Dim dtobj As DataObject = CType(e.Data, DataObject)
If dtobj.ContainsFileDropList Then
Dim filename As String = dtobj.GetFileDropList.Item(0).ToString()
Console.WriteLine("文件名稱:" & filename)
If filename.LastIndexOf(".exe") > 0 _
OrElse filename.LastIndexOf(".EXE") > 0 _
OrElse filename.LastIndexOf(".dll") > 0 _
OrElse filename.LastIndexOf(".DLL") > 0 Then
Console.WriteLine("可反射文件")
'創建反射對象
'反射拖進來的文件
Dim asm As Reflection.Assembly = Reflection.Assembly.LoadFrom(filename)
If asm IsNot Nothing Then
Dim tps As Type() = asm.GetTypes()
If tps IsNot Nothing Then
Me.listExeOrDll.Items.Clear()
lblDll.Text = filename
For Each tp As Type In tps
'判斷tp是否是System.Windows.Forms.Form的子類
Dim bl As Boolean = tp.IsSubclassOf(GetType(Windows.Forms.Form))
If Not bl Then
bl = tp.IsSubclassOf(GetType(Windows.Forms.UserControl))
End If
If bl Then
'如果是,就加入到listbox中
Me.listExeOrDll.Items.Add(tp.ToString())
End If
Next
End If
End If
''根據字符串,得到目標類別
'Dim objType As Type = asm.GetType("折讓單系統." & frmName)
''根據類別,實例化一個目標對象
'Dim obj As Object = Activator.CreateInstance(objType)
Else
Console.WriteLine("不可反射文件")
If True Then 'filename.LastIndexOf(".txt") > 0 OrElse filename.LastIndexOf(".vb") > 0 Then
Dim strs As String() = IO.File.ReadAllLines(filename, System.Text.Encoding.Default)
If strs.Length > 0 Then
Me.listExeOrDll.Items.Clear()
For Each s As String In strs
listExeOrDll.Items.Add(s)
Next
End If

End If
End If
End If
Else
Console.WriteLine("对象无效")
End If
End Sub
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: