VB.net in the implementation of PictureBox in the picture drag and label control array combination, to achieve label and picture synchronization

Source: Internet
Author: User
Tags integer relative
Controls | arrays

In the previous article, you mentioned the implementation of PictureBox in vb.net, as well as the control array.

Because the project needs, what I want to realize is that the picture has the name of each site, I want to realize click the corresponding name, go to the site, view the corresponding information. I take a series of labels on the picture, and then click on the label, go to the appropriate site, so that I encountered a problem, to achieve the drag picture, all the label also drag synchronously.

The following code implements this feature:

Imports System.Drawing
Namespace Winform.main
Public Class MainWindow
Inherits System.Windows.Forms.Form

----The code generated by the Windows Forms Designer---

Public Sub New ()
MyBase.New ()

' This call is required by the Windows Forms Designer.
InitializeComponent ()

' Add any initialization after the InitializeComponent () call
Me.windowstate = Windows.Forms.FormWindowState.Maximized
Loadtree ()
Me.l_user_content. Text = MyForm.LogInUser.get_UserName
' Gets the initial position coordinates of the PictureBox
ME.M_LEFTX = me.picturebox1.location.x
Me.m_lefty = ME.PICTUREBOX1.LOCATION.Y
' Get the zoom rate of the picture
ME.M_STRECTHX = Me.picturebox1.image.size.width/me.picturebox1.size.width
Me.m_strecthy = Me.picturebox1.image.size.height/me.picturebox1.size.height

' Add label double-click Binding
Bindarray ()
End Sub

-------------the code generated by the Windows Forms Designer----------------------

' Handle picture drag

        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
  & nbsp;     ' Zoom
        Dim m_strecthx as Double
         Dim m_strecthy as Double
        The position of the label starting relative to the 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
' Whether to scale for the first time
Dim M_l_first as Boolean = True
' Process label
Dim Labels as New Common.labelarray (Me)

' When the mouse is pressed, turn the mouse into a hand and record the position of the current mouse
Private Sub Picturebox1_mousedown (ByVal sender as Object, ByVal e as System.Windows.Forms.MouseEventArgs) Handles picture Box1.mousedown

Me.cursor = System.Windows.Forms.Cursors.Hand
M_MOUSEPOSX = e.x
M_mouseposy = E.y

Getlabelpositon ()

End Sub

' The position of the picture calculated from the offset, repaint the picture
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

' Handles the mouse button to raise the event, according to the mouse when the mouse clicks the position which saves, and the current mouse position, calculates the mouse movement offset, borrows this to call moves the picture the function, moves the picture
Private Sub Picturebox1_mouseup (ByVal sender as Object, ByVal e as System.Windows.Forms.MouseEventArgs) Handles Picturebo X1. 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

' The label of the site on the pipe map is handled with an event that needs to be bound before processing
Public Sub Bindarray ()
Labels.additem (Me.l_gw_ry)
Labels.additem (ME.L_STATION_ZF)
End Sub

' Get the start of the label relative to the picture position
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
' Redraw the label according to the position of the picture
Public Sub Stretchlabel ()
If M_l_first = True Then
Me.l_gw_ry. Font = New System.Drawing.Font ("XXFarEastFont-Arial", 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 ("XXFarEastFont-Arial", 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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.