不通过标题栏移动窗体
我们在设计程序时,需要隐藏程序的标题栏,但这样一来就给我们移动窗体带来不少的麻烦. 这时,必须要求我们编写代码来实现移动窗体的操作.
以下是我通过用控件(图片框)的 mousedown 和 mouseup 事件的组合来实现.
Imports System.Windows.Forms.Cursors
Public Class Form1
Public x, x1, x2 As Integer
Public y, y1, y2 As Integer
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
x = Me.Left
y = Me.Top
x1 = e.X
y1 = e.Y
'当按下鼠标左键时,改变鼠标光标的形状
If e.Button = Windows.Forms.MouseButtons.Left Then
PictureBox1.Cursor = SizeAll
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
Dim y3, x3 As Integer
x2 = e.X
y2 = e.Y
' 只有用左键才能够移动窗体
If e.Button = Windows.Forms.MouseButtons.Left Then
If x2 <> x1 Or y2 <> y1 Then
x3 = x2 - x1
Me.Left = x + x3
y3 = y2 - y1
Me.Top = y + y3
End If
End If
'将鼠标光标恢复为默认
PictureBox1.Cursor = [Default]
End Sub
End Class