In VB.net, image dragging and label control arrays in picturebox are combined to synchronize labels and images.

Source: Internet
Author: User

In the previous article, we mentioned how to implement image dragging and control array in picturebox in VB.net.

As required by the project, the image contains the names of various sites. To achieve this, click the corresponding name to enter the site and view the corresponding information. I chose to put a series of labels on the image, and then click the label to enter the corresponding site. In this way, I encountered a problem. When dragging the image, all labels are also dragged synchronously.

The following code implements this function:

Imports system. Drawing
Namespace winform. Main
Public class mainwindow
Inherits system. Windows. Forms. Form

---- "Code generated by the Windows Form Designer "---

Public sub new ()
Mybase. New ()

'The call is required by the Windows Form Designer.
Initializecomponent ()

'Add any initialization after initializecomponent () is called.
Me. windowstate = windows. Forms. formwindowstate. maximized
Loadtree ()
Me. l_user_content.text = myform. loginuser. get_username
'Obtain the initial coordinate of the picturebox.
Me. m_leftx = me. picturebox1.location. x
Me. m_lefty = me. picturebox1.location. Y
'Get the scaling rate of the image.
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 a double-click label binding.
Bindarray ()
End sub

------------- Code generated by the Windows Form Designer ----------------------

'Process image dragging

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
'Scaling Rate
Dim m_strecthx as double
Dim m_strecthy as double
'Label start position of 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 the label
Dim labels as new common. labelarray (me)

'When the mouse is pressed, the mouse is changed to a hand shape and the current mouse position is recorded.
Private sub picturebox#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

'Redraw the Image Based on the offset.
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

'Process the mouse button lifting event, calculate the mouse movement offset based on the mouse position saved when the mouse is pressed, and the current mouse position, so as to call the function of moving the image, move the image
Private sub picturebox#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

'Use an event to process the label of the site on the pipe network diagram. You need to bind the label before processing.
Public sub bindarray ()
Labels. additem (Me. l_gw_ry)
Labels. additem (Me. l_station_zf)
End sub

'Obtain the starting position of the label for 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
'Repaint the label based on the position of the picture.
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. Maid = new system. Drawing. Font ("", 9.0 !, System. Drawing. fontstyle. Regular, system. Drawing. graphicsunit. Point, ctype (134, byte ))
Trim (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 (cost-m_driftx + l_gw_ry.size.width)> (Me. picturebox1.size. width) then

Rochelle gw_ry.visible = false
Else
Rochelle gw_ry.visible = true
End if

If (cost-m_driftx) <me. picturebox1.location. X or (l_station_zf.location.x-m_driftx + l_station_zf.size.width)> (Me. picturebox1.size. width) then
Rochelle station_zf.visible = false
Else
Rochelle 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.