VB picturebox控件的拖动事件.
悬赏分:130 - 解决时间:2007-6-30 23:22
我想用鼠标拖动图片,达到那种好像在拖动时图片粘在鼠标上的效果.
请问怎么样才能够实现啊...............
就用DRAGDROP那些好像不能是吗.......
提问者: wujunchenghaha - 秀才 二级 最佳答案
所谓拖动,就是按下鼠标左键并移动鼠标位置,这需要在按下鼠标左键后让程序知道开始拖动并记下当前的X、Y坐标值,在鼠标移动时根据之前的X、Y坐标和当前的位移量重新设置控件的位置,然后在放开鼠标左键时通知程序已经停止拖动
这里要注意的另外一件事是:如果在Picture控件的Mousemove事件中写移动Picture位置的代码的话,Picture位置的变化又会触发Picture的Mousemove事件,这样循环地产生事件会产生预料外的结果,而且如果鼠标过快地移动到Picture外面,可能会使图片停止跟随鼠标,为避免这种问题,这里用API函数Setcapture把鼠标移动的事件绑定到Form上,自己体会一下吧
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Dim blnDragging As Boolean
Dim offsetX As Single, offsetY As Single
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If blnDragging Then
Picture1.Move X - offsetX, Y - offsetY
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
blnDragging = False
ReleaseCapture
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
blnDragging = True
SetCapture Me.hwnd
offsetX = X
offsetY = Y
End If
End Sub
_____________________
最好是写一个副程式,尽量简单点的哦,还有三角形啊,圆形啊。意思是我开了两个form,form1有picture1,我想在form2有控件按了之后,可以在form1的picture1里画到相应的形状,本来程序开始的时候form1的picture 是画线的,我要做一个类似windows画图工具那个东东啊,请大家帮帮我。(追加50分),谢谢
提问者: 渐渐散开的墨 - 试用期 一级 最佳答案
Option Explicit
Private aX, aY As Integer
Private Sub Form_Load()
Me.AutoRedraw = True
Me.ScaleMode = 3
Shape1.Visible = False
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
aX = X
aY = Y
Shape1.Left = aX
Shape1.Top = aY
Shape1.Width = 0
Shape1.Height = 0
Shape1.Visible = True
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
If X >= aX Then
Shape1.Width = X - aX
Else
Shape1.Left = X
Shape1.Width = aX - X
End If
If Y >= aY Then
Shape1.Height = Y - aY
Else
Shape1.Top = Y
Shape1.Height = aY - Y
End If
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Shape1.Visible = False
Me.Line (Shape1.Left, Shape1.Top)-(Shape1.Left + Shape1.Width, Shape1.Top + Shape1.Height), , B
End Sub
____________________________-
本文档由A5下载 http://down.admin5.com收集整理,版权归原作者所有。
A5下载提供海量源码,软件,素材,教程文档下载。
如果您恰好喜欢打篮球,请登录www.siboding.com
购买正品低价的斯伯丁篮球
评论0
最新资源