none
请教窗体自动靠边隐藏怎么做 RRS feed

  • 问题

  • 请教当最小化窗体和鼠标离开窗体时,窗体自动靠边隐藏; 当鼠标移到屏幕边缘时自动展开要怎么做?
    2016年12月16日 3:49

答案

  • 你好,

    根据你的描述,你可以在确保游标离开窗体边界后,触发窗体自动靠边。

    Private Sub Test_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MoveToDefaultPosition()
            AddFormDisappearingHandlers()
        End Sub
    
        Dim x As Integer
        Dim y As Integer
        Dim tim As Timer
    
        Sub MoveFormAway(sender As Object, e As EventArgs)
            ' called on timer tick event
            Dim destY = Screen.PrimaryScreen.WorkingArea.Height - 100
    
            If y >= destY Then
                tim.Enabled = False
                Exit Sub
            End If
    
            ' interval for form position changes
            tim.Interval = 10
            y += 2
            Me.Location = New Point(x, y)
    
        End Sub
    
        Sub FormBodyLeave(sender As Object, e As EventArgs)
            ' If the mouse has not left the outside of the form,
            ' i.e. it has entered a control on the form, then exit:
            If Me.ClientRectangle.Contains(PointToClient(Cursor.Position)) Then
                Exit Sub
            End If
    
            If tim Is Nothing OrElse Not tim.Enabled Then
                x = Me.Location.X
                y = Me.Location.Y
                tim = New Timer
                ' initial interval until form starts running away
                tim.Interval = 10
                AddHandler tim.Tick, AddressOf MoveFormAway
                tim.Enabled = True
            End If
    
        End Sub
    
        Sub FormBodyEnter(sender As Object, e As EventArgs)
            If tim IsNot Nothing Then
                tim.Enabled = False
            End If
    
            MoveToDefaultPosition()
    
        End Sub
    
        Sub AddFormDisappearingHandlers()
            AddHandler Me.MouseEnter, AddressOf FormBodyEnter
            AddHandler Me.MouseLeave, AddressOf FormBodyLeave
    
        End Sub
    
        Sub MoveToDefaultPosition()
            x = Screen.PrimaryScreen.WorkingArea.Width - Me.Width
            y = Screen.PrimaryScreen.WorkingArea.Height - Me.Height
            Me.Location = New Point(x, y)
    
        End Sub

    我只是尝试实现了鼠标离开窗体时,窗体自动靠边功能,至于最小化窗体将窗体自动靠边的功能我还没有实现,因为没有找到最小化相关的事件。

    希望对你有所帮助。

    Best Regards,

    Neda Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 leon1526 2016年12月16日 10:10
    2016年12月16日 7:26
    版主

全部回复

  • 你好,

    根据你的描述,你可以在确保游标离开窗体边界后,触发窗体自动靠边。

    Private Sub Test_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MoveToDefaultPosition()
            AddFormDisappearingHandlers()
        End Sub
    
        Dim x As Integer
        Dim y As Integer
        Dim tim As Timer
    
        Sub MoveFormAway(sender As Object, e As EventArgs)
            ' called on timer tick event
            Dim destY = Screen.PrimaryScreen.WorkingArea.Height - 100
    
            If y >= destY Then
                tim.Enabled = False
                Exit Sub
            End If
    
            ' interval for form position changes
            tim.Interval = 10
            y += 2
            Me.Location = New Point(x, y)
    
        End Sub
    
        Sub FormBodyLeave(sender As Object, e As EventArgs)
            ' If the mouse has not left the outside of the form,
            ' i.e. it has entered a control on the form, then exit:
            If Me.ClientRectangle.Contains(PointToClient(Cursor.Position)) Then
                Exit Sub
            End If
    
            If tim Is Nothing OrElse Not tim.Enabled Then
                x = Me.Location.X
                y = Me.Location.Y
                tim = New Timer
                ' initial interval until form starts running away
                tim.Interval = 10
                AddHandler tim.Tick, AddressOf MoveFormAway
                tim.Enabled = True
            End If
    
        End Sub
    
        Sub FormBodyEnter(sender As Object, e As EventArgs)
            If tim IsNot Nothing Then
                tim.Enabled = False
            End If
    
            MoveToDefaultPosition()
    
        End Sub
    
        Sub AddFormDisappearingHandlers()
            AddHandler Me.MouseEnter, AddressOf FormBodyEnter
            AddHandler Me.MouseLeave, AddressOf FormBodyLeave
    
        End Sub
    
        Sub MoveToDefaultPosition()
            x = Screen.PrimaryScreen.WorkingArea.Width - Me.Width
            y = Screen.PrimaryScreen.WorkingArea.Height - Me.Height
            Me.Location = New Point(x, y)
    
        End Sub

    我只是尝试实现了鼠标离开窗体时,窗体自动靠边功能,至于最小化窗体将窗体自动靠边的功能我还没有实现,因为没有找到最小化相关的事件。

    希望对你有所帮助。

    Best Regards,

    Neda Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 leon1526 2016年12月16日 10:10
    2016年12月16日 7:26
    版主
  • 多谢Neda的回复,
    2016年12月16日 10:11