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

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

概述如何将桌面或者文件夹中的图片拖拽到窗体或控件上呢?这就需要用到AllowDrop属性了。废话不多说,直接上源码,里面我注释的非常详细,相信大家一目了然。 '**********************************************************************'作者:章鱼哥,QQ:3107073263 群:309816713'如有不懂的或者需要更多源程序的请

如何将桌面或者文件夹中的图片拖拽到窗体或控件上呢?这就需要用到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 SubEnd Class


运行效果图如下:

1、拖拽之前


2、拖拽之后

总结

以上是内存溢出为你收集整理的章鱼哥—VB.NET 控件中你不知道的属性之——AllowDrop 应用详解全部内容,希望文章能够帮你解决章鱼哥—VB.NET 控件中你不知道的属性之——AllowDrop 应用详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1272343.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存