📜  vb.net 无标题栏拖动窗口 - C# (1)

📅  最后修改于: 2023-12-03 15:35:34.149000             🧑  作者: Mango

VB.NET 无标题栏拖动窗口 - C#

在 VB.NET 中,通过设置 Form 的 BorderStyle 为 None 可以实现无标题栏的窗口,但此时鼠标无法拖动窗口。本文将介绍如何在无标题栏窗口中实现鼠标拖动窗口效果。

实现步骤
  1. 更改窗口样式为无边框
Me.FormBorderStyle = FormBorderStyle.None
  1. 添加鼠标按下事件
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
    '记录鼠标按下时的坐标
    Me.mouseOffset = New Point(-e.X, -e.Y)
End Sub
  1. 添加鼠标移动事件
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
    '鼠标左键按下并且移动窗口
    If e.Button = Windows.Forms.MouseButtons.Left Then
        Dim mousePos As Point = Control.MousePosition
        mousePos.Offset(Me.mouseOffset.X, Me.mouseOffset.Y)
        Me.Location = mousePos
    End If
End Sub
  1. 声明鼠标位置偏移量
Private mouseOffset As Point
完整代码
Public Class Form1

    Private mouseOffset As Point

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        '记录鼠标按下时的坐标
        Me.mouseOffset = New Point(-e.X, -e.Y)
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        '鼠标左键按下并且移动窗口
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim mousePos As Point = Control.MousePosition
            mousePos.Offset(Me.mouseOffset.X, Me.mouseOffset.Y)
            Me.Location = mousePos
        End If
    End Sub

End Class

以上就是在 VB.NET 中实现无标题栏拖动窗口的方法,希望对您有所帮助。