这一页是封装的控件,用法更简单,编译运行一次之后,直接在VS的工具栏中拖动控件即可。
此控件支持左、右、上方隐藏窗体,不过还是老规矩:仅支持主屏幕。
Imports System.ComponentModel
Imports System.ComponentModel.Design

Public Class HideFormControl
    Inherits Component

    Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As Point) As Int32

    Dim F As Form
    Dim WithEvents T As New Timer
    Dim _Start As Boolean = False

    Dim P As Point
    Dim R As Rectangle
    Dim S As Rectangle = My.Computer.Screen.Bounds

    Dim H1 As Integer = 10 '窗体边界1
    Dim H2 As Integer = 5 '窗体边界2

    Enum FX
        Left
        Right
        Top
    End Enum

    ''' <summary>鼠标进入窗体</summary>
    ''' <param name="frm">目标窗体</param>
    ''' <param name="MousP">鼠标位置</param>
    Event MouseInFrm(ByVal frm As Form, ByVal MousP As Point)
    ''' <summary>窗体隐藏</summary>
    ''' <param name="frmFX">隐藏方向</param>
    Event FrmHide(ByVal frmFX As FX)
    ''' <summary>窗体显示</summary>
    ''' <param name="frmFX">隐藏方向</param>
    Event FrmShow(ByVal frmFX As FX)
    ''' <summary>窗体自动隐藏开始</summary>
    ''' <param name="IsStart">是否已经开始</param>
    Event FrmHideStart(ByVal IsStart As Boolean)

    Sub New()
    End Sub
    ''' <summary>构造函数</summary><param name="frm">指定窗体</param><param name="isStart">是否立刻开始</param>
    Sub New(ByVal frm As Form, Optional ByVal isStart As Boolean = True)
        Me.Form = frm
        Start = isStart
    End Sub

    ''' <summary>设置窗体边界1</summary>
    <Description("设置窗体边界1"), DefaultValue(10)> _
    Property FormB1() As Integer
        Get
            Return H1
        End Get
        Set(ByVal value As Integer)
            H1 = value
        End Set
    End Property
    ''' <summary>设置窗体边界2</summary>
    <Description("设置窗体边界2"), DefaultValue(5)> _
    Property FormB2() As Integer
        Get
            Return H2
        End Get
        Set(ByVal value As Integer)
            H2 = value
        End Set
    End Property

    ''' <summary>设置目标窗体</summary>
    <Description("设置目标窗体"), DefaultValue(GetType(Form), Nothing)> _
    Property Form() As Form
        Get
            If F Is Nothing Then '从设计器中寻找主窗体
                Dim host As IDesignerHost = Nothing
                If Site IsNot Nothing Then
                    host = Site.GetService(GetType(IDesignerHost))
                End If
                If host IsNot Nothing AndAlso TypeOf host.RootComponent Is Form Then
                    F = host.RootComponent
                End If
            End If
            Return F
        End Get
        Set(ByVal value As Form)
            F = value
            If value IsNot Nothing Then
                R = F.RestoreBounds
            Else
                R = Nothing
            End If
        End Set
    End Property
    ''' <summary>是否开始自动隐藏</summary>
    <Description("是否开始自动隐藏"), DefaultValue(False)> _
    Property Start() As Boolean
        Get
            Return _Start
        End Get
        Set(ByVal value As Boolean)
            If F Is Nothing AndAlso value Then Throw New Exception("未初始化窗体!")
            T.Enabled = value
            F.TopMost = value
            _Start = value
            RaiseEvent FrmHideStart(value)
        End Set
    End Property
    ''' <summary>设置计时器时间</summary>
    <Description("设置计时器时间"), DefaultValue(100)> _
    Property SetTimerInterval() As Integer
        Get
            Return T.Interval
        End Get
        Set(ByVal value As Integer)
            T.Interval = value
        End Set
    End Property

    Private Sub T_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles T.Tick
        GetCursorPos(P)    '获取鼠标位置
        If P.X > F.Left AndAlso P.X < F.Right AndAlso P.Y > F.Top AndAlso P.Y < F.Bottom Then
            RaiseEvent MouseInFrm(F, P)
            If F.Top < 0 Then
                F.Top = -(H2)
                RaiseEvent FrmShow(FX.Top)
            ElseIf F.Left < 0 Then
                F.Left = -(H2)
                RaiseEvent FrmShow(FX.Left)
            ElseIf F.Left + F.Width >= S.Width Then
                F.Left = S.Width - F.Width + H1
                RaiseEvent FrmShow(FX.Right)
            End If
        Else
            If F.Top <= H2 Then
                F.Top = H1 - F.Height
                RaiseEvent FrmHide(FX.Top)
            ElseIf F.Left <= H2 Then
                F.Left = H1 - F.Width
                RaiseEvent FrmHide(FX.Left)
            ElseIf F.Left + F.Width >= S.Width - H2 Then
                F.Left = S.Width - H1
                RaiseEvent FrmHide(FX.Right)
            End If
        End If
    End Sub

End Class