【VB.NET】窗体自动隐藏
好久没发文章了,主要最近发生的事情太多了,希望大家能在这个庚子年平和安康。
分享一个很久以前的东西,可以像QQ一样隐藏窗体到屏幕的上方,也会在鼠标移动到窗体边缘时自动弹出。
不过因为代码有些久远,所以如果副屏幕的位置与主屏幕不一样,则会引发窗体位置混乱的BUG,暂时不想修了,以后再说吧。
用法:
Dim hf As HideForm
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
hf = New HideForm(Me, True)
End Sub
代码:Public Class HideForm
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
''' <summary>设置窗体边界1</summary>
Public H1 As Integer = 10
''' <summary>设置窗体边界2</summary>
Public H2 As Integer = 5
Sub New()
End Sub
Sub New(ByVal frm As Form, Optional ByVal isStart As Boolean = True)
Me.Form = frm
Start = isStart
End Sub
''' <summary>设置目标窗体</summary>
Property Form() As Form
Get
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>
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
End Set
End Property
''' <summary>设置计时器时间</summary>
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
If F.Top < 0 Then
F.Top = -(H2)
ElseIf F.Left < 0 Then
F.Left = -(H2)
ElseIf F.Left + F.Width >= S.Width Then
F.Left = S.Width - F.Width + H1
End If
Else
If F.Top <= H2 Then
F.Top = H1 - F.Height
ElseIf F.Left <= H2 Then
F.Left = H1 - F.Width
ElseIf F.Left + F.Width >= S.Width - H2 Then
F.Left = S.Width - H1
End If
End If
End Sub
End Class
下一页是封装好的控件代码本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。