拖动窗体

来源:互联网 发布:天津理工大学软件宿舍 编辑:程序博客网 时间:2024/06/10 18:40

1。窗体和控件的拖动代码,在窗体或控件的mousemove里加载 

 Static Dim ox, oy As Int16
        If e.Button = Windows.Forms.MouseButtons.Left Then
            sender.top = e.Y + sender.top - oy
            sender.left = e.X + sender.left - ox
        Else
            ox = e.X
            oy = e.Y
        End If
2。在网上找到的关于picturebox图片拖动,要实现在拖动图片的同时,所有的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

原创粉丝点击