我有一个只有一个标签的窗体。当它出现时,我需要能够在屏幕上移动表格。我在谷歌的许多帖子中尝试了以下代码。但我无法让它发挥作用。任何人都知道这里出了什么问题?谢谢你的帮助!
Public Class frmmsg
Inherits System.Windows.Forms.Form
'Windows Form Designer generated code here
Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer
Private Sub frmmsg_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown, Me.MouseClick, label2.MouseClick, lblMessage.MouseClick
If e.Button = Windows.Forms.MouseButtons.Left Then
drag = True
mousex = Windows.Forms.Cursor.Position.X - Me.Left
mousey = Windows.Forms.Cursor.Position.Y - Me.Top
End If
End Sub
Private Sub frmmsg_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove, label2.MouseMove, lblMessage.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
If drag Then
Me.Top = Windows.Forms.Cursor.Position.Y - mousey
Me.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End If
End Sub
Private Sub frmmsg_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp, label2.MouseUp, lblMessage.MouseUp
drag = False
End Sub
Sub ShowMessage(ByVal psText$)
If Not Me.Visible Then Me.Show()
lblMessage.Text = psText
Me.Refresh()
End Sub
End Class
'这是从不同的类
调用上述表单的方式<NonSerialized()> Private mfrmmsg As frmmsg
If IsNothing(mfrmmsg) OrElse mfrmmsg.IsDisposed Then
mfrmmsg = New frmmsg
End If
mfrmmsg.ShowMessage(psText)
mfrmmsg.TopMost = True