微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

章鱼哥—VB.NET 控件中你不知道的属性之——AllowDrop 应用详解

如何将桌面或者文件夹中的图片拖拽到窗体或控件上呢?这就需要用到AllowDrop属性了。废话不多说,直接上源码,里面我注释的非常详细,相信大家一目了然。

'**********************************************************************
'作者:章鱼哥,QQ:3107073263 群:309816713
'如有不懂的或者需要更多源程序的请联系我
'主要内容:
'    本例子主要讲述AllowDrop 属性的使用,在认的情况下,该属性为false 这里要设置true
'    本文以form窗体为例,该方法同样适用于button\panel等具有AllowDrop属性的控件
'实现功能
'    将一个图片文件鼠标拖到控件上时,该图片会以拖拽点为起点,在控件上显示图片

'**********************************************************************
Public Class Form1
    Dim picture As Image '定义一个image变量,保存拖拽的图片
    Dim picturelocation As New Point '保存图片的起点位置坐标
    '*****************************************************************
    '在控件的三个事件中分别调用函数。分别是paint事件、dragenter事件、DragDrop事件
    '函数分别是 DrawingImage()、Drag_Enter()、Drag_Drop() 这三个函数具有通用性,读者可以在任意控件的上述三个事件中分别调用
    '具体代码和注释如下:
    '*****************************************************************************
    '窗体的paint事件
    Private Sub Form1_Paint(ByVal sender As System.Object,ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        DrawingImage(e) '绘制图片函数
    End Sub
    '窗体dragenter事件
    Private Sub Form1_dragenter(ByVal sender As System.Object,ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.dragenter
        Drag_Enter(e) '过滤拖拽文件函数,
    End Sub
    '窗体DragDrop事件
    Private Sub Form1_DragDrop(ByVal sender As System.Object,ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
        Drag_Drop(sender,e) '提取图片和坐标函数
    End Sub

    '绘制图片函数,将获取图片绘制到控件中
    Private Sub DrawingImage(ByVal e As System.Windows.Forms.PaintEventArgs)
        If picture IsNot nothing And Not picturelocation = New Point(0,0) Then '如果获取到了图片
            e.Graphics.DrawImage(picture,picturelocation) '将图片绘制到控件
        End If
    End Sub
    '过滤拖拽文件函数,只接受位图和文件格式
    Private Sub Drag_Enter(ByVal e As System.Windows.Forms.DragEventArgs)
        If e.Data.GetDataPresent(DataFormats.Bitmap) Or e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.copy '将文件或位图复制到控件中,有五种选项,读者可自行测试
        Else
            e.Effect = DragDropEffects.None '不接受拖拽的文件
        End If
    End Sub
    '提取图片和坐标函数
    Private Sub Drag_Drop(ByVal sender As System.Object,ByVal e As System.Windows.Forms.DragEventArgs)
        If e.Data.GetDataPresent(DataFormats.Bitmap) Then '如果是位图格式,直接赋值获取
            picture = CType(e.Data.GetData(DataFormats.Bitmap),Image)
            picturelocation = sender.PointToClient(New Point(e.X,e.Y))
        End If
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then '如果是文件获取文件的地址,再通过地址获取图片
            picture = Image.FromFile(CType(e.Data.GetData(DataFormats.FileDrop),String())(0))
            'MsgBox(CType(e.Data.GetData(DataFormats.FileDrop),String())(0))
            picturelocation = sender.PointToClient(New Point(e.X,e.Y))
        End If
        sender.Invalidate() '重绘控件,这句一定要有,它将触发paint事件
    End Sub


End Class


运行效果图如下:

1、拖拽之前


2、拖拽之后

原文地址:https://www.jb51.cc/vb/257774.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐