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

发表于:2007-06-30来源:作者:点击数: 标签:
在前面的文章中,提到过在 vb .net 中实现picturebox中图片拖动,以及控件数组方面的东西。 因为项目需要,我要实现的是,图片上有各个站点的名称,我要实现点击相应的名称,进入站点,查看相应的信息。我采取的是在图片上放一系列的label,然后点击label,
 

在前面的文章中,提到过在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

原文转自:http://www.ltesting.net