(VB.NET)为WinForms创建简单闪屏的快捷方法[duplicate]

vawmfj5a  于 2023-02-19  发布在  .NET
关注(0)|答案(2)|浏览(180)
    • 此问题在此处已有答案**:

How to create a Splash screen for VB.net program(3个答案)
5天前关闭。
我的程序加载花了~5 - 10秒钟,有时使用它的人最终会试图再次打开它,这导致了问题。我找到了一个快速简单的方法来制作"闪屏"(在某种意义上)在执行时立即弹出一段时间。我发现WinForm EXE加载中事件的第一个顺序是Handle Created。答案不是一个 * true * splashscreen,但是对于可以很容易地添加到项目中的几行代码,我想有些人会喜欢它。

bpsygsoo

bpsygsoo1#

下面的代码将在运行EXE时立即显示MessageBox,并在10秒后关闭。

Imports System.Threading

Private Sub Control1_HandleCreated(ByVal sender As Object, ByVal e As EventArgs) Handles Me.HandleCreated
    Dim SplashScreen As New Thread(
        Sub()
            CreateObject("WScript.Shell").Popup("Program Initializing, Please Wait...",10, "Setup Tool")
        End Sub)
    SplashScreen.Start()
End Sub

我使用Threading,这样MessageBox就不会冻结代码,并且无论是否按下“确定”按钮,程序都会打开。执行常规MessageBox.Show()将阻止任何代码运行,直到用户单击“确定”。

igetnqfo

igetnqfo2#

我发现实现闪屏的最好方法是通过消息和/或进度条或动画轮通知用户,如下所示。
有一个启动窗体,例如Form 1,让它执行所有繁琐的启动过程,这些过程可能会导致任何动画或进度条图形在事件队列中停滞。从工具箱中向Form 1添加一个“BackgroundWorker”对象,在我的例子中,我将其命名为BackgroundWorker 1。
在启动这些例程之前(通常在Form1_Load事件中),请调用BackgroundWorker。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        
        CallBackgroundWork()

        StartRoutines() 'this is the heavy lifting routines to get the app working. Set the LoadingStatusflag (declared as a Global Variable"
to various values to tell the splashscreen to display different messages

        Loadingstatus = 10 'triggers splashform to exit
        CancelBackgroundWork()

    End Sub

这些是其他的潜艇来支援

Sub CallBackgroundWork()

        BackgroundWorker1.WorkerSupportsCancellation = True
        BackgroundWorker1.WorkerReportsProgress = True

        ' call this method to start your asynchronous Task.
        BackgroundWorker1.RunWorkerAsync()

    End Sub

    Sub CancelBackgroundWork()

        ' to cancel the task, just call the BackgroundWorker1.CancelAsync method.

        BackgroundWorker1.CancelAsync()

    End Sub

    Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        '' The asynchronous task we want to perform goes here
        FormSplash.Show()

    End Sub

我的闪屏有一些标签控件和图片框,FormSplash_Load事件运行40毫秒的秒表循环并加载一系列图像(总共24个)。当闪屏处于活动状态时,它会一直运行。通过在Form 1中的加载序列的不同部分将全局变量Loadingstatus设置为不同的值,它可以触发循环例程以显示不同的消息示例。当你不能直接访问线程间的对象时,线程间通信的一种简单方式无论Form 1中的加载例程在另一个线程中运行时有多密集,轮子都会旋转。我使用秒表循环,因为启动计时器对我来说不起作用-可能是启动窗体中的事件队列问题。

Private Sub FormSplash_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.Show()

        Me.Opacity = 1  'show this form

        'now start a loop that gets ended by other thread through variable Loadingstatus flag
        Dim ggtimer As New Stopwatch, lastvalue As Integer, FProgPosition as integer

        ggtimer.Start()
        lastvalue = ggtimer.ElapsedMilliseconds

nextimage:
        FProgPosition += 1
        If FProgPosition = 24 Then FProgPosition = 1 'has 24 frames in the animated image

        Do  'loop for 40 ms
            If ggtimer.ElapsedMilliseconds - lastvalue > 40 Then
                lastvalue = ggtimer.ElapsedMilliseconds
                Exit Do
            End If
        Loop

        PictureBoxProgress1.Image = FProgIMG(FProgPosition)
        PictureBoxProgress1.Refresh()

        If Loadingstatus = 10 Then GoTo endsplash

        If Loadingstatus = 1 Then

            If CoreTempRunning = False Then
                Me.LabelCoreTemp.Text = "CoreTemp is NOT Running"
                Me.LabelCoreTemp.ForeColor = Color.White
                'insert cross picturebox
                PictureBoxCoreTemp.Image = My.Resources.ResourceManager.GetObject("Cross24x24")
                loaderrorflag2 = True
            Else
                Me.LabelCoreTemp.Text = "CoreTemp is Running"
                Me.LabelCoreTemp.ForeColor = Color.White
                'insert tick picturebox
                PictureBoxCoreTemp.Image = My.Resources.ResourceManager.GetObject("Tick24x24")
                loaderrorflag2 = False
            End If
            Me.PictureBoxCoreTemp.Visible = True
            Me.PictureBoxCoreTemp.Refresh()

            Me.LabelCoreTemp.Left = Me.Width * 2 / 3 - Me.LabelCoreTemp.Width
            Me.LabelCoreTemp.Refresh()

        GoTo nextimage
endsplash:

        ggtimer.Stop()
        Me.Opacity = 0.01
        Me.Hide()

    End Sub

相关问题