欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

启动时显示以及关闭splash窗体

程序员文章站 2024-03-18 12:30:28
...

自动出现一个启动窗体,其实可以在项目属性那里初始屏幕中选择一个窗体,然后加个定时器让它关闭:
启动时显示以及关闭splash窗体
但是,这个方法在主窗体需要加载数据的时候不能准确获得数据是否加载完毕。
本文采用了一个另外的方法:
主窗体:包含一个label和一个timer
启动时显示以及关闭splash窗体
Splash窗体:包含一个label和一个progressbar。窗体的TopMost属性为True,确保Splash窗体显示在主窗体上面,同时由于无法准确获知主窗体载入数据的时间,所以设置progressbar 的Style属性为Marquee
启动时显示以及关闭splash窗体
模块中的代码:
 Public MainFormState As Integer
主窗体代码:
Public Class Form1     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load         MainFormState = 0         Dim frm2 As New Form2         frm2.Show()
        Timer1.Start()     End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick         Timer1.Stop()         Label1.Text = "启动中……"
        Console.WriteLine("状态:" & MainFormState)
        Threading.Thread.Sleep(1000)         Dim sum As Integer = 0         For i As Integer = 0 To 1000000             sum += 1             Application.DoEvents()         Next         MainFormState = 1         Label1.Text = "正在导入数据"         Console.WriteLine("状态:" & MainFormState)
        For i As Integer = 0 To 1000000             sum += 1             Application.DoEvents()         Next
        MainFormState = 2         Label1.Text = "启动完成"         Console.WriteLine("状态:" & MainFormState)     End Sub End Class

Splash窗体代码:
Public Class Form2
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.CheckForIllegalCrossThreadCalls = False
        Dim thrCheckState As New Threading.Thread(AddressOf checkstate)
        thrCheckState.Start()

    End Sub

    Sub checkstate()

        Dim blLoaded As Boolean = False
        Do While (blLoaded = False)
            Select Case MainFormState
                Case 0
                    Label1.Text = "启动中……"
                    blLoaded = False
                Case 1
                    Label1.Text = "正在导入数据"
                    blLoaded = False
                Case Else
                    Label1.Text = "启动完成"
                    blLoaded = True
            End Select
        Loop
        Me.Close()
    End Sub

End Class

运行时:
启动时显示以及关闭splash窗体
为什么主窗体要加入timer,然后再timer中加载数据?大家可以尝试把加载数据的代码修改到Form_load 中试一下。

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看vb.net 教程 目录

相关标签: vb.net splash