
'定义一个布尔变量,用于标识控件是否可拖动
Dim canDrag As Boolean
Private Sub Form_Load()
'当窗体加载时,设置其值为false(即控件不可拖动)
canDrag = False
End Sub
Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'当按下鼠标时,控件可以拖动,但是这个动作不在这里执行
canDrag = True
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If canDrag Then '如果canDrag为真,这一动鼠标时就拖动控件
Label1.Left = Label1.Left + X
Label1.Top = Label1.Top + Y
End If
End Sub
Private Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
'松开渣凳鼠标时,控件不可拖动
canDrag = False
End Sub
拖放是用鼠标拖动一个对象到其它对象的活动。在图形 *** 作过程中,拖放是最常用的功能之一,下面我们来看看怎样用 VB 实现拖放功能。首先介绍与拖放有关的控件:
1.属性:DragMode 决定拖动 *** 作的初始化是人工方式还是自动方式,DragIcon 确定在拖动过程中显示的培轿指针的图标形状;
2.方法:Drag 开始,结束或取消拖动控件配友肆;
3.事件:MouseDown 事件发生于用户按下鼠标按钮时,DragOver 事件发生于拖动 *** 作完成时,DragDrop 事件发生于拖动 *** 作正在进行时。
然后编写一个小程序,这个程序能实现在窗口中或窗口间拖动图标的功能。建立窗口 Form1 和 Form2,在窗口中都加入 Image1,为它们设置初始显示的图片。键入以下代码(本程序在 VB5.0/6.0,Window95/98/NT4.0 环境下通过):
' Form1 下程序代码为:
Option Explicit
Dim dragx As Single
Dim dragy As Single
Const BEGIN_DRAG=1
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
Image1.Picture=Source 'Sourse为被拖动的控件
Form2.Image1.Picture=LoadPicture("")
Image1.Move(X-dragx),(Y-dragy) ' X,Y为告谨鼠标所在目标窗体或控件的当前坐标
End Sub
Private Sub Form_Load()
Load Form2
Form2.Show 0
End Sub
Private Sub Image1_MouseDown(Button As Integer,Shift As Integer,X As Single,Y As Single)
dragx=X
dragy=Y
Image1.Drag BEGIN_DRAG'开始拖动 *** 作
Image1.DragIcon = LoadPicture("按下鼠标时想显示的光标")
End Sub
' Form2 下程序代码为:
Option Explicit
Dim dragx As Single
Dim dragy As Single
Const BEGIN_DRAG=1
Private Sub Form_DragDrop(Source As Control,X As Single,Y As Single)
Image1.Picture=Source
Form1.Image1.Picture=LoadPicture("")
Image1.Move(X-dragx),(Y-dragy)
End Sub
Private Sub Image1_MouseDown(Button As Integer,Shift As Integer,X As Single,Y As Single)
dragx=X
dragy=Y
Image1.Drag BEGIN_DRAG
Image1.DragIcon=LoadPicture("按下鼠标时想显示的光标")
这是因为Frame控件也是一个容器,从Frame2外进入Frame2内,相当尺型于从一迅困正个坐标系进入了另一个坐标系。再未进入Frame2时,Frame1的位置是相对于它所在的窗体(确切的说是该窗体的左上角坐标(0,0))确定的,一旦进入Frame2,Frame1的位置就相对于Frame2的左上角坐标(亩悔0,0)确定。你可以在设计时作个试验,窗体上放一个Frame控件和一个按钮控件,按钮控件在Frame控件外面的时候的坐标设定为一个值(如90,90),当你把该按钮放到Frame控件上的时候,同样设定这个坐标值,你看看什么效果。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)