关于鼠标事件mousemove的一段代码

wjp_auhtm 2007-02-09 11:18:38
为什么我实验后 在拖动图片到达边界处时 会越界或拖动不顺畅呢 是不是这样限制图片移动范围的方法不对啊

'鼠标拖动图片
Private Sub Picedit_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picedit.MouseDown
If Not (Picedit.Image Is Nothing) Then
If e.Button = MouseButtons.Left Then
Me.Cursor = System.Windows.Forms.Cursors.Hand
mouseX = e.X
mousey = e.Y
jk = 1
Else
Exit Sub
End If
End If
End Sub

Private Sub Picedit_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picedit.MouseMove
If Not (Picedit.Image Is Nothing) Then
If e.Button = MouseButtons.Left Then
If jk = 1 Then
If Picedit.Width < panel.Width Then
Exit Sub
End If
If Picedit.Height < panel.Height Then
Exit Sub
End If
If Picedit.Width >= panel.Width And Picedit.Width >= panel.Width Then
Picedit.Left = Picedit.Left + e.X - mouseX
Picedit.Top = Picedit.Top + e.Y - mousey
Picedit.Refresh()
End If

If Picedit.Width + Picedit.Left <= panel.Width Then
Picedit.Left = panel.Width - Picedit.Width
Picedit.Refresh()
jk = 2

End If
If Picedit.Height + Picedit.Top <= panel.Height Then
Picedit.Top = panel.Height - Picedit.Height
Picedit.Refresh()
jk = 2

End If
If Picedit.Left >= 0 Then
Picedit.Left = 0
Picedit.Refresh()
jk = 2

End If
If Picedit.Top >= 0 Then
Picedit.Top = 0
Picedit.Refresh()
jk = 2

End If
Else
Exit Sub
End If
End If
End If
End Sub

Private Sub Picedit_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picedit.MouseUp
If Picedit.Image Is Nothing Then
Exit Sub
End If
If jk = 2 Then
If Picedit.Width + Picedit.Left < panel.Width Then
Picedit.Left = panel.Width - Picedit.Width
Picedit.Refresh()
Exit Sub
End If
If Picedit.Height + Picedit.Top < panel.Height Then
Picedit.Top = panel.Height - Picedit.Height
Picedit.Refresh()
Exit Sub
End If
If Picedit.Left > 0 Then
Picedit.Left = 0
Picedit.Refresh()
Exit Sub
End If
If Picedit.Top > 0 Then
Picedit.Top = 0
Picedit.Refresh()
Exit Sub
End If
Me.Cursor = System.Windows.Forms.Cursors.Default
jk = 3
End If
End Sub
...全文
435 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjp_auhtm 2007-02-12
  • 打赏
  • 举报
回复
谢谢高手帮忙 我感觉你的思路很系统 很有条理 这点是我现在严重缺乏的
你的代码我稍稍修改了一下 现在可以满足我的要求了 多谢高手帮忙 不过 在边界处拖动速度很快的话 还是稍微有些闪 不过已经能满足功能呢 呵呵 我这就把分给您 希望大家可以经常交流 我的qq是17824409
magicbacon 2007-02-12
  • 打赏
  • 举报
回复
initX,initY在这里相当于你的mouseX,mouseY,而flagX,flagY相当于你的jK,但是在纪录鼠标状态的同时还纪录了位置的状态,即是否越界.呵呵,我也是新手,要实现你的全部功能,代码可能还有问题,这段代码我是测试过的,如果不行的话在讨论吧. : )
magicbacon 2007-02-12
  • 打赏
  • 举报
回复
出现你的问题的原因可能是因为MouseMove时间的发生也是有一定时间间隔的,即每隔一段时间(零点几秒或零点零几秒)就检查一次鼠标的位置,如果在这段时间内鼠标移动越界了,而程序中有不能对此进行处理,那么因为MouseMove不能及时动作,就会达不到你需要的效果.所以我写的代码中将横坐标和纵坐标分开处理,每个坐标分为三种不同状态分别处理,即在界内,出左(上)界和出右(下)界.这样即使在MouseMove的时间间隔中出现了出界也能处理了,而人眼是分辨不出区别的.
magicbacon 2007-02-12
  • 打赏
  • 举报
回复
Public Class Form1

' 在窗体中加入Panel控件panel、PictureBox控件Picedit
' Picedit放在panel中,鼠标拖动时,Picedit在panel中移动但不越界,
' 即panel是Picedit的活动范围

Dim initX, initY As Int32
Dim flagX, flagY As Int32

Private Sub Picedit_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picedit.MouseDown
If Not (Picedit.Image Is Nothing) Then
If e.Button = Windows.Forms.MouseButtons.Left Then
Me.Cursor = System.Windows.Forms.Cursors.Hand
initX = e.X ' 纪录初始位置
initY = e.Y
flagX = 1 ' 图片已载入且左健按下时将标志位置1
flagY = 1
Else
Exit Sub
End If
End If
End Sub

Private Sub Picedit_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picedit.MouseMove
Select Case flagX ' 处理横坐标
Case 0 ' 不符合移动条件,不做任何操作
Exit Sub
Case 1 ' 横坐标在范围之内
Picedit.Left = Picedit.Left - initX + e.X
If Picedit.Left < panel.Left Then
flagX = 2
ElseIf Picedit.Left + Picedit.Width > panel.Left + panel.Width Then
flagX = 3
End If
Case 2 ' 横坐标超出左边框
Picedit.Left = panel.Left
If -initX + e.X > 0 Then
Picedit.Left = Picedit.Left - initX + e.X
flagX = 1 ' 回到范围之内,标志复位到1
End If
Case 3 ' 横坐标超出右边框
Picedit.Left = panel.Left + panel.Width - Picedit.Width
If -initX + e.X < 0 Then
Picedit.Left = Picedit.Left - initX + e.X
flagX = 1 ' 回到范围之内,标志复位到1
End If
End Select
Select Case flagY ' 处理纵坐标
Case 1 ' 横坐标在范围之内
Picedit.Top = Picedit.Top - initY + e.Y
If Picedit.Top < panel.Top Then
flagY = 2
ElseIf Picedit.Top + Picedit.Height > panel.Top + panel.Height Then
flagY = 3
End If
Case 2 ' 纵坐标超出下边框
Picedit.Top = panel.Top
If -initY + e.Y > 0 Then
Picedit.Top = Picedit.Top - initY + e.Y
flagY = 1 ' 回到范围之内,标志复位到1
End If
Case 3 ' 纵坐标超出下边框
Picedit.Top = panel.Top + panel.Height - Picedit.Height
If -initY + e.Y < 0 Then
Picedit.Top = Picedit.Top - initY + e.Y
flagY = 1 ' 回到范围之内,标志复位到1
End If
End Select
End Sub

Private Sub Picedit_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picedit.MouseUp
flagX = flagY = 0
Me.Cursor = System.Windows.Forms.Cursors.Default
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
flagX = flagY = 0
End Sub
End Class
wjp_auhtm 2007-02-11
  • 打赏
  • 举报
回复
...
magicbacon 2007-02-10
  • 打赏
  • 举报
回复
关键是panel的大小,位置和放置它的目的.
magicbacon 2007-02-10
  • 打赏
  • 举报
回复
是不是用鼠标将PictureBox Picedit在Panel panel中拖动?不能出边框.
magicbacon 2007-02-10
  • 打赏
  • 举报
回复
能不能把你的控件命名和从属关系说一下,还有jK,mouseX,mouseY的定义.
wjp_auhtm 2007-02-10
  • 打赏
  • 举报
回复
jk 是出于无奈加上的一个判断是鼠标按下还是拖动 还是鼠标放开的参数(鼠标按下后jk=1 开启鼠标拖动事件;鼠标拖动出设定的边界后jk=2)
picedit是一个picturebox 放在了panel控件中
mousex和mousey是鼠标点击时鼠标的坐标位置(全都是按panel控件的坐标来计算的)

希望达到的效果是:picedit控件在panel控件中拖动 达到通过panel控件浏览picedit中的图像的功能
不知为何 加入这段代码后 倒是能实现拖动效果 但是很不顺畅 不知为何 希望高手能帮忙解释一下 谢谢

16,721

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧