您的位置:程序门 -> .net技术 -> vb.net



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


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


关于鼠标事件mousemove的一段代码[已结贴,结贴人:wjp_auhtm]
发表于:2007-02-09 23: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
发表于:2007-02-10 08:42:171楼 得分:0
能不能把你的控件命名和从属关系说一下,还有jk,mousex,mousey的定义.
发表于:2007-02-10 08:44:202楼 得分:0
是不是用鼠标将picturebox   picedit在panel   panel中拖动?不能出边框.
发表于:2007-02-10 08:49:483楼 得分:0
关键是panel的大小,位置和放置它的目的.
发表于:2007-02-10 18:23:204楼 得分:0
jk   是出于无奈加上的一个判断是鼠标按下还是拖动   还是鼠标放开的参数(鼠标按下后jk=1   开启鼠标拖动事件;鼠标拖动出设定的边界后jk=2)
picedit是一个picturebox   放在了panel控件中
mousex和mousey是鼠标点击时鼠标的坐标位置(全都是按panel控件的坐标来计算的)

希望达到的效果是:picedit控件在panel控件中拖动   达到通过panel控件浏览picedit中的图像的功能
不知为何   加入这段代码后   倒是能实现拖动效果   但是很不顺畅   不知为何   希望高手能帮忙解释一下   谢谢
发表于:2007-02-11 17:35:125楼 得分:0
...
发表于: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
发表于:2007-02-12 08:50:127楼 得分:0
出现你的问题的原因可能是因为mousemove时间的发生也是有一定时间间隔的,即每隔一段时间(零点几秒或零点零几秒)就检查一次鼠标的位置,如果在这段时间内鼠标移动越界了,而程序中有不能对此进行处理,那么因为mousemove不能及时动作,就会达不到你需要的效果.所以我写的代码中将横坐标和纵坐标分开处理,每个坐标分为三种不同状态分别处理,即在界内,出左(上)界和出右(下)界.这样即使在mousemove的时间间隔中出现了出界也能处理了,而人眼是分辨不出区别的.
发表于:2007-02-12 08:56:098楼 得分:50
initx,inity在这里相当于你的mousex,mousey,而flagx,flagy相当于你的jk,但是在纪录鼠标状态的同时还纪录了位置的状态,即是否越界.呵呵,我也是新手,要实现你的全部功能,代码可能还有问题,这段代码我是测试过的,如果不行的话在讨论吧.       :   )
发表于:2007-02-12 23:06:139楼 得分:0
谢谢高手帮忙   我感觉你的思路很系统   很有条理   这点是我现在严重缺乏的
你的代码我稍稍修改了一下   现在可以满足我的要求了   多谢高手帮忙   不过   在边界处拖动速度很快的话   还是稍微有些闪   不过已经能满足功能呢   呵呵   我这就把分给您   希望大家可以经常交流   我的qq是17824409


快速检索

最新资讯
热门点击