VB控件间的拖放

新建工程,放置控件Picture1,Text1,Text2,复制下面的代码运行

Text1和Text2可以互相拖放,也可以将Picture1拖放给Text1,Text2.

Private Sub Form_Load()
    Picture1.OLEDragMode = 1
    Text1.OLEDropMode = 1
    Text1.OLEDragMode = 1
    Text2.OLEDropMode = 1
    Text2.OLEDragMode = 1
End Sub

Private Sub Picture1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
    AllowedEffects = 1
    Data.Clear
    Data.SetData "来自Picture1", 1 '设置拖放的内容
End Sub

Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim s As String
    s = Data.GetData(1)
    Text1.SelLength = 0
    Text1.SelText = s '在光标处插入
End Sub
Private Sub Text1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
    AllowedEffects = 1
    Data.Clear
    Data.SetData Text1.SelText, 1 '设置拖放的内容
End Sub


Private Sub Text2_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim s As String
    s = Data.GetData(1)
    Text2.SelLength = 0
    Text2.SelText = s '在光标处插入
End Sub
Private Sub Text2_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
    AllowedEffects = 1
    Data.Clear
    Data.SetData Text2.SelText, 1 '设置拖放的内容
End Sub

关于事件疑问自行百度,这里说明一下Data.SetData:

语法

object.SetData [data], [format]

SetData 方法语法包含下面部分:

部分 描述
object 必需的。对象表达式,其值是“应用于”列表中的一个对象。
data 可选的变体型,包含要传送到 DataObject 对象的数据。
format 可选的常数或值,规定所传送数据的格式,如“设置值”中所述。

 

 

 

 

 

 

设置值

format 设置如下:

常数 描述
vbCFText 1 文本(.txt文件)
vbCFBitmap 2 位图(.bmp文件)
vbCFMetafile 3 元文件(.wmf文件)
vbCFEMetafile 14 增强元文件(.emf 文件)
vbCFDIB 8 设备无关位图 (DIB)
vbCFPalette 9 调色板
vbCFFiles 15 文件列表
vbCFRTF -16639 丰富文本格式(.rtf文件)
posted @ 2017-07-21 06:49  JustXIII  阅读(1463)  评论(0编辑  收藏  举报