在前面的文章中,提到過在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
新聞熱點
疑難解答
圖片精選