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事件代码
例如:使用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
相关文章推荐
- .NET vs2010中使用IrisSkin2.dll轻松实现winForm窗体换肤功能
- winform实现拖曳功能
- 用C#实现换肤功能 - winform
- 在winform界面上实现伸缩功能
- .NET 邮件传送功能实现(smtp.qq.com)
- winform 实现打印功能
- 在winform窗体中显示图片并实现特殊功能
- C# WinForm 实现增删改查等功能(Access版) 系列之七-图片处理
- C# WinForm 实现增删改查等功能(Access版) 系列之三-更新
- .net 实现word、excel、ppt、pdf预览功能
- 如何用C#在winform中实现简单的查找功能
- winform(c#)中实现打印机相关功能
- c# winform 实现对postgresql数据库的自动备份还原功能
- C# WinForm 实现增删改查等功能(Access版) 系列之四-删除
- Eclipse实现类似.Net中的Region功能
- C#实现的简易含undo/redo功能的winForm
- winform + INotifyPropertyChanged + IDataErrorInfo + ErrorProvider实现自动验证功能
- .Net实现文件下载功能
- 使用.net FtpWebRequest 实现FTP常用功能
- 怎样用.net实现更新原文件功能?