您的位置:首页 > 其它

实现运行以后改变控件的大小并能拖动控件

2007-12-24 14:19 489 查看
Public Class Form1
'改变控件大小
Private Sub MoveControls(ByVal x As Integer, ByVal mypanel As Panel, ByVal mypicture As PictureBox)
mypicture.Left = mypicture.Left + x
mypanel.Width = mypicture.Left - mypanel.Left
End Sub

'当鼠标落到picturebox上时,鼠标变成水平改变大小状态,如果是左键被按下,则改变大小
Private Sub PictureBoxMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim mypicture As PictureBox = CType(sender, PictureBox)
mypicture.Cursor = Cursors.SizeWE
Dim picturename As String = mypicture.Name
Dim panelname As String = picturename.Substring(0, picturename.Length - 2)
Dim mypanel As Panel = CType(Me.Controls(panelname), Panel)
If e.Button = Windows.Forms.MouseButtons.Left Then
MoveControls(e.X, mypanel, mypicture)
End If
End Sub

Dim x As Integer = 0
Dim y As Integer = 0
'当鼠标移动到panel上时,鼠标变成移动状态,如果左键被按下,则把鼠标的位置得到
Private Sub PanelMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim mypanle As Panel = CType(sender, Panel)
mypanle.Cursor = Cursors.SizeAll
If e.Button = Windows.Forms.MouseButtons.Left Then
x = e.X
y = e.Y
End If
End Sub
'如果鼠标放下,同时鼠标的位置被改变,移动panel位置
Private Sub PanelMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim mypanel As Panel = CType(sender, Panel)
Dim picturename As String = mypanel.Name & "_p"
Dim mypicture As PictureBox = CType(Me.Controls(picturename), PictureBox)
If x <> 0 And y <> 0 Then
mypanel.Left = mypanel.Left + x
mypicture.Left = mypicture.Left + x
mypanel.Top = mypanel.Top + y
mypicture.Top = mypicture.Top + y
End If
x = 0
y = 0
End Sub
'动态创建两个控件,做测试
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mypanel As New Panel
mypanel.Name = "test"
mypanel.BackColor = Color.Blue
mypanel.Top = 100
mypanel.Left = 100
mypanel.Width = 100
mypanel.Height = 15
AddHandler mypanel.MouseUp, AddressOf PanelMouseUp
AddHandler mypanel.MouseMove, AddressOf PanelMouseMove
Dim mypic As New PictureBox
mypic.Name = mypanel.Name & "_P"
mypic.BackColor = Color.Black
mypic.Top = mypanel.Top
mypic.Left = mypanel.Left + mypanel.Width
mypic.Width = 3
mypic.Height = mypanel.Height
AddHandler mypic.MouseMove, AddressOf PictureBoxMouseMove
Me.Controls.Add(mypanel)
Me.Controls.Add(mypic)
End Sub
End Class
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: