国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 怀宁县| 大安市| 蓬溪县| 许昌县| 略阳县| 金华市| 襄垣县| 沁阳市| 湄潭县| 淮阳县| 于田县| 丰城市| 郑州市| 太仓市| 尉氏县| 揭阳市| 长武县| 霍邱县| 合阳县| 内乡县| 依安县| 新宾| 台南市| 连城县| 安宁市| 娄底市| 鲁甸县| 锦屏县| 洛扎县| 吉隆县| 西乡县| 无棣县| 新竹市| 花垣县| 梅河口市| 运城市| 庆云县| 蓬莱市| 湟中县| 女性| 漠河县|