| 发表于:2007-02-12 08:37:506楼 得分:0 |
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 | | |
|