日期:2012-12-21  浏览次数:20370 次

 

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