天天看點

winform實作拖曳功能。.net拖曳實作 drag

平時使用的軟體很多都有拖曳的功能,感覺功能很強大,使用者體驗非常好.

例如:使用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