谁能帮我看看这段代码为何运行状态下动态生成的控件移动缓慢。
Public Class Frm_WarehouseInlfoDim MousePos2 As Point
Dim draging As Boolean = False
Dim DragRect As Rectangle
Dim TitleHeight As Integer = 30 '窗体标题栏的高度
'Dim VPoint As Point
Public Sub canMove(ByVal vObj As Control)
AddHandler vObj.MouseDown, AddressOf Controls_Down
AddHandler vObj.MouseMove, AddressOf Controls_Move
AddHandler vObj.MouseMove, AddressOf Controls_MouseUp
End Sub
Private Sub Controls_Down(ByVal sender As Object, ByVal e As MouseEventArgs)
'VPoint = New Point(e.X, e.Y)
'sender.BringToFront()
If draging = False Then
draging = True
MousePos2 = New Point(MousePosition.X - Left - sender.Left, MousePosition.Y - Top - sender.Top)
MakeRect()
End If
End Sub
Private Sub MakeRect()
DragRect = New Rectangle(Left + MousePos2.X, Top + MousePos2.Y, ClientSize.Width - 40, ClientSize.Height - 40 - TitleHeight)
End Sub
Private Sub Controls_Move(ByVal sender As Object, ByVal e As MouseEventArgs)
'If Form.MouseButtons = Windows.Forms.MouseButtons.Left Then
' If sender.left = 0 Then
' CType(sender, Control).Location = New Point(0, sender.Top + e.Y - VPoint.Y)
' End If
'End If
If draging Then
If MousePosition.X < DragRect.Left Then
sender.Left = 0
ElseIf MousePosition.X > DragRect.Right Then
sender.Left = ClientSize.Width - sender.Width
Else
sender.Left = MousePosition.X - Left - MousePos2.X
End If
If MousePosition.Y < DragRect.Top Then
sender.Top = 0
ElseIf MousePosition.Y > DragRect.Bottom Then
sender.Top = Height - sender.Height - TitleHeight
Else
sender.Top = MousePosition.Y - Top - MousePos2.Y
End If
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim test As New Button
test.Text = "Test "
Me.Controls.Add(test)
Call Me.canMove(test)
End Sub
Private Sub Controls_MouseUp(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs)
draging = False
End Sub
End Class
--------------------编程问答-------------------- 有个非常小的错误
AddHandler vObj.MouseMove, AddressOf Controls_MouseUp
改成
AddHandler vObj.MouseUp, AddressOf Controls_MouseUp
就OK.. --------------------编程问答-------------------- 就是,mousemove多了一个
补充:.NET技术 , VB.NET