VB.net / UI / 任意移动窗口

VB.net—UI设计—任意移动窗口

设计窗口应用的时候,希望窗体能简洁异型,不要标题栏,能够按住窗体任意位置拖动窗口。以下代码可以实现:(原谅我把VB.net写出了Python风格…)

Winform

Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
        If e.Button <> MouseButtons.Left Then:mouseX = e.Location.X:mouseY = e.Location.Y:Exit SubElse:Me.Top = Me.Top + e.Location.Y - mouseY:Me.Left = Me.Left + e.Location.X - mouseX:Exit SubEnd If:End Sub

wpf(可能还需要修改一下)

    Private Sub frmSettings_MouseMove(sender As Object, e As Input.MouseEventArgs) Handles Me.MouseMove
        If e.LeftButton = MouseButtonState.Released Then:mouseX = e.GetPosition(Me).X : mouseY = e.GetPosition(Me).Y:Exit Sub
        ElseMe.Top = Me.Top + e.GetPosition(Me).Y - mouseY:Me.Left = Me.Left + e.GetPosition(Me).X - mouseX:Exit SubEnd IfEnd Sub

2021年补充:
修改为以下风格:
新建一个module,在Module里创建以下过程

    ''' 
    ''' 使窗口可以自由移动,用法:
    '''         AddHandler Me.MouseMove, AddressOf WindowMove
    ''' 
    ''' 
    ''' 
    Private mouseX, mouseY
    Public Sub WindowMove(sender As Object, e As Input.MouseEventArgs) ' Handles Me.MouseMove
        If Mouse.LeftButton = MouseButtonState.Released Then
            mouseX = e.GetPosition(sender).X:
            mouseY = e.GetPosition(sender).Y
            Exit Sub
        Else
            sender.Top = sender.Top + e.GetPosition(sender).Y - mouseY
            sender.Left = sender.Left + e.GetPosition(sender).X - mouseX
            Exit Sub
        End If

    End Sub

则任意窗口体内的初始化过程中只需声明:

Private Sub MainWindow_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
    AddHandler Me.MouseMove, AddressOf WindowMove
    ....
    
End Sub

你可能感兴趣的:(窗口移动,vb.net,ui)