首页 > 编程 > .NET > 正文

vb.net中实现picturebox中图片拖动和label控件数组结合,实现label和图片同步

2024-07-10 13:00:40
字体:
来源:转载
供稿:网友
 

在前面的文章中,提到过在vb.net中实现picturebox中图片拖动,以及控件数组方面的东西。

因为项目需要,我要实现的是,图片上有各个站点的名称,我要实现点击相应的名称,进入站点,查看相应的信息。我采取的是在图片上放一系列的label,然后点击label,进入相应的站点,这样就遇到了一个问题,要实现在拖动图片的同时,所有的label也同步拖动。

下面的代码实现了这个功能:

imports system.drawing
namespace winform.main
    public class mainwindow
        inherits system.windows.forms.form

---- " windows 窗体设计器生成的代码 "---

 public sub new()
            mybase.new()

            '该调用是 windows 窗体设计器所必需的。
            initializecomponent()

            '在 initializecomponent() 调用之后添加任何初始化
            me.windowstate = windows.forms.formwindowstate.maximized
            loadtree()
            me.l_user_content.text = myform.loginuser.get_username
            '得到picturebox的初始位置坐标
            me.m_leftx = me.picturebox1.location.x
            me.m_lefty = me.picturebox1.location.y
            '得到图片的缩放率
            me.m_strecthx = me.picturebox1.image.size.width / me.picturebox1.size.width
            me.m_strecthy = me.picturebox1.image.size.height / me.picturebox1.size.height

            '添加label双击的绑定
            bindarray()
        end sub

------------- windows 窗体设计器生成的代码----------------------

   '处理图片拖动

        private m_leftx as integer = 152
        private m_lefty as integer = 0
        dim m_mouseposx as integer
        dim m_mouseposy as integer
        dim m_driftx as integer
        dim m_drifty as integer
        '缩放率
        dim m_strecthx as double
        dim m_strecthy as double
        'label起始相对于picturebox的位置
        dim m_l_gw_ry_x as integer
        dim m_l_gw_ry_y as integer

        dim m_l_station_zf_x as integer
        dim m_l_station_zf_y as integer
        '是否第一次缩放
        dim m_l_first as boolean = true
        '处理label
        dim labels as new common.labelarray(me)

   '当鼠标按下时,将鼠标变成手形,并且记录下当前鼠标的位置
        private sub picturebox1_mousedown(byval sender as object, byval e as system.windows.forms.mouseeventargs) handles picturebox1.mousedown           

            me.cursor = system.windows.forms.cursors.hand
            m_mouseposx = e.x
            m_mouseposy = e.y

           getlabelpositon()

 end sub

'根据偏移量计算出的图片位置,重画图片
        private sub picturemove(byval sender as object, byval e as system.windows.forms.mouseeventargs)
            dim mybit as new system.drawing.bitmap(picturebox1.image)

            dim mypicgrh as system.drawing.graphics = me.picturebox1.creategraphics
            mypicgrh.clear(me.picturebox1.backcolor)
                        mypicgrh.drawimageunscaled(mybit, m_leftx - 152, m_lefty)
            stretchlabel()
            mybit.dispose()
            mypicgrh.dispose()

        end sub

  '处理鼠标按键抬起的事件,根据鼠标按下时保存的鼠标位置,和当前鼠标的位置,计算鼠标移动偏移量,借此调用移动图片的函数,移动图片
        private sub picturebox1_mouseup(byval sender as object, byval e as system.windows.forms.mouseeventargs) handles picturebox1.mouseup
            m_driftx = m_mouseposx - e.x
            m_drifty = m_mouseposy - e.y
            m_leftx = m_leftx - m_driftx
            m_lefty = m_lefty - m_drifty
            picturemove(sender, e)
            me.cursor = system.windows.forms.cursors.arrow

        end sub

   '将管网图上的站点的label用一个事件来处理,处理之前需要绑定
        public sub bindarray()
            labels.additem(me.l_gw_ry)
            labels.additem(me.l_station_zf)
        end sub

  '得到label的起始相对于picture的位置
        public sub getlabelpositon()
            m_l_gw_ry_x = (me.l_gw_ry.location.x - me.picturebox1.location.x) * me.m_strecthx + me.picturebox1.location.x
            m_l_gw_ry_y = me.l_gw_ry.location.y * me.m_strecthy

            m_l_station_zf_x = (me.l_station_zf.location.x - me.picturebox1.location.x) * me.m_strecthx + me.picturebox1.location.x
            m_l_station_zf_y = me.l_station_zf.location.y * me.m_strecthy
        end sub
        '根据picture的位置重绘label
        public sub stretchlabel()
            if m_l_first = true then
                me.l_gw_ry.font = new system.drawing.font("宋体", 9.0!, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ctype(134, byte))
                l_gw_ry.setbounds(me.m_l_gw_ry_x - m_driftx, me.m_l_gw_ry_y - m_drifty, l_gw_ry.width * me.m_strecthx, l_gw_ry.height * me.m_strecthy)

                me.l_station_zf.font = new system.drawing.font("宋体", 9.0!, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ctype(134, byte))
                l_station_zf.setbounds(me.m_l_station_zf_x - m_driftx, me.m_l_station_zf_y - m_drifty, l_station_zf.width * me.m_strecthx, l_station_zf.height * me.m_strecthy)
            else
               
                if ((l_gw_ry.location.x - m_driftx) < me.picturebox1.location.x) or ((l_gw_ry.location.x - m_driftx + l_gw_ry.size.width) > (me.picturebox1.size.width)) then

                    l_gw_ry.visible = false
                else
                    l_gw_ry.visible = true
                end if

                if (l_station_zf.location.x - m_driftx) < me.picturebox1.location.x or (l_station_zf.location.x - m_driftx + l_station_zf.size.width) > (me.picturebox1.size.width) then
                    l_station_zf.visible = false
                else
                    l_station_zf.visible = true
                end if

                l_gw_ry.setbounds(l_gw_ry.location.x - m_driftx, l_gw_ry.location.y - m_drifty, l_gw_ry.width, l_gw_ry.height)
                l_station_zf.setbounds(l_station_zf.location.x - m_driftx, l_station_zf.location.y - m_drifty, l_station_zf.width, l_station_zf.height)

            end if
            m_l_first = false
        end sub
    end class

end namespace

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表