vb.net为通知表单设置动画.线程和大小问题



我创建了一个类用作通知窗口(类似于toast通知,在我们的系统中禁用(。我使用一个定时器对象来暂停关闭表单,并使用一个后台工作人员来处理它从屏幕底部滑入的动画。出于调试目的,表单只输出自己的大小和屏幕边界。

Imports System.ComponentModel
Public Class ASNotify
Public Sub New(ByVal title As String, ByVal msg As String, ByVal Optional timeout As Integer = 5000)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.Text = title
Me.NotifyMessage.Text = $"{Me.Width}x{Me.Height}{vbCrLf}{My.Computer.Screen.WorkingArea.Size.Width}x{My.Computer.Screen.WorkingArea.Size.Height}"
TimeoutTimer.Interval = timeout
TimeoutTimer.Enabled = True
AnimationWorker.RunWorkerAsync()
End Sub
Private Sub AnimationWorker_DoWork(sender As Object, e As DoWorkEventArgs) Handles AnimationWorker.DoWork
Dim xloc As Integer = My.Computer.Screen.WorkingArea.Size.Width - Me.Width
Dim yloc As Integer = My.Computer.Screen.WorkingArea.Size.Height
For x As Integer = 0 To Me.Height
MoveWindow(xloc, yloc - x)
Threading.Thread.Sleep(2)
Next
End Sub
Private Sub MoveWindow(xloc As Integer, yloc As Integer)
If InvokeRequired Then
Invoke(Sub() MoveWindow(xloc, yloc))
Else
Location = New Drawing.Point(xloc, yloc)
End If
End Sub
Private Sub TimeoutTimer_Tick(sender As Object, e As EventArgs) Handles TimeoutTimer.Tick
Me.Close()
End Sub

End Class

我从另一个表单调用

Private Sub NotifyUser(ByRef a As Alert.Alert)
Dim notify As New ASNotify(a.Location, a.Comment, 5000)
notify.Show()
End Sub

我通过按下表格上的按钮来调用sub,它非常有效。。。。。有时

反复触发通知窗口会使其在屏幕上弹出为两种不同大小之一,尽管显示大小的内容始终为264x81和1920x1040

偶尔我会遇到一个例外,即"Location=new Drawing.Point(xloc,yloc("这行是从创建它的线程之外的线程调用的,尽管调用了Invoke。

将计时器启动和animationworker启动移动到窗体的Load方法,而不是New。

Private Sub ASNotify_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TimeoutTimer.Enabled = True
AnimationWorker.RunWorkerAsync()
End Sub

最新更新