0

我制作了一个淡入然后淡出并加载另一个表单的表单。当新表单已加载时,它会关闭启动表单。

问题是当它关闭这个启动表单时,它会杀死我的应用程序。

Public Class Splash
Dim appearance As Boolean = False

Private Sub Splash_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Opacity = 0
    FadeIn.Start()
End Sub

Private Sub FadeIn_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FadeIn.Tick
    If Not appearance Then
        Opacity += 0.015
    End If
    If Opacity = 1 Then
        appearance = True
    End If
    If appearance = True Then
        Opacity -= 0.015
        If Opacity = 0 Then
            Form1.Show()
        End If
    End If
End Sub
End Class

Public Class Form1

Private Function WebLoad()
    While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
        Application.DoEvents()
    End While
    Return 0
End Function
Private Function Login(ByVal User As String, ByVal Pass As String)
    WebBrowser1.Navigate("http://caan/SC5/SC_Login/aspx/login_launch.aspx?SOURCE=ESOLBRANCHLIVE")
    WebLoad()
    WebBrowser1.Document.GetElementById("txtUserName").SetAttribute("value", User)
    WebBrowser1.Document.GetElementById("txtPassword").SetAttribute("value", Pass)

    Dim allImgTags As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
    If allImgTags IsNot Nothing Then
        For Each img As HtmlElement In allImgTags
            If img.GetAttribute("src").Contains("/images/SC5Login07.jpg") Then
                img.InvokeMember("Click")
                Exit For
            End If
        Next img
    End If
    Return 0
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Login(user, pass)
    WebBrowser2.Navigate("http://caan/SC5/SC_PartsCentre/aspx/partscentre_frameset.aspx")
    WebBrowser1.Navigate("http://caan/SC5/SC_RepairJob/aspx/RepairJob_frameset.aspx")
    Splash.Close()
End Sub
End Class

我知道有一个适当的闪屏表单,但它不会保持打开足够长的时间。在淡出时它只是关闭并启动我的应用程序

我的主要问题是如何阻止 splash.close() 关闭我的整个应用程序?

回答

现在整理一下,不让我在底部回答...

用于在visualbasic express中提供的闪屏,并将我在msdn上找到的几行添加到应用程序事件中

应用事件

Namespace My

' The following events are available for MyApplication:
' 
' Startup: Raised when the application starts, before the startup form is created.
' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
' UnhandledException: Raised if the application encounters an unhandled exception.
' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
Partial Friend Class MyApplication
    Protected Overrides Function OnInitialize( _
ByVal commandLineArgs As  _
System.Collections.ObjectModel.ReadOnlyCollection(Of String)) As Boolean 
        Me.MinimumSplashScreenDisplayTime = 7000
        Return MyBase.OnInitialize(commandLineArgs)
    End Function

End Class
End Namespace

闪屏1.VB

Public NotInheritable Class SplashScreen1
Dim appearance As Boolean = False

Private Sub SplashScreen1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If My.Application.Info.Title <> "" Then
        ApplicationTitle.Text = My.Application.Info.Title
    Else
        ApplicationTitle.Text = System.IO.Path.GetFileNameWithoutExtension(My.Application.Info.AssemblyName)
    End If

    Version.Text = System.String.Format(Version.Text, My.Application.Info.Version.Major, My.Application.Info.Version.Minor)

    Copyright.Text = My.Application.Info.Copyright

    Opacity = 0
    Fade.Start()
End Sub

Private Sub MainLayoutPanel_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MainLayoutPanel.Paint
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fade.Tick
    Fade.Interval = 100
    If Not appearance Then
        Opacity += 0.1
    End If

    If appearance = True Then        
        Opacity -= 0.1
        End If
    If Opacity = 1 Then
        Fade.Interval = 5000
        appearance = True
    End If
End Sub
End Class
4

2 回答 2

4

您应该检查Shutdown mode option项目的应用程序页面http://msdn.microsoft.com/en-us/library/tzdks800(v=vs.90).aspx并告诉我们 和 的Shutdown modeStartup form

我猜Shutdown mode设置为When startup form closesStartup form设置为Splash

在这种情况下,尝试设置Shutdown modeOn last window close,它应该可以解决您的问题。

于 2013-05-19T11:59:30.717 回答
0

默认情况下,启动屏幕会保持打开状态两秒钟,如果“启动表单”的“加载”时间比“加载”时间长,则启动时间会更长。

因此,要让您的初始屏幕保持打开状态,无论您需要多长时间,只需阻止主窗体加载,直到发出信号。一种方法是使用 ManualResetEvent。

在此设置中,“启动形式”为Form1,“启动画面”为Splash,“关机模式”为When startup form closes

在 Splash 中,我添加了一个 Shared ManualResetEvent 并且仅在表单完成动画时发出信号:

Public Class Splash

    Private appearance As Boolean = False

    Public Shared MRE As New System.Threading.ManualResetEvent(False)

    Private Sub Splash_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Opacity = 0
        FadeIn.Start()
    End Sub

    Private Sub FadeIn_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FadeIn.Tick
        If Not appearance Then
            Opacity += 0.015
        Else
            Opacity -= 0.015
        End If

        If Opacity = 1 Then
            appearance = True
        ElseIf Opacity = 0 Then
            MRE.Set()
        End If
    End Sub

End Class

现在,在 Form1 中,我们针对 Shared ManualResetEvent 调用 WaitOne():

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim MySplash As Splash = DirectCast(My.Application.SplashScreen, Splash)
        Splash.MRE.WaitOne() ' wait for the splash to signal

        ' ... other code ...

    End Sub

End Class

现在 Form1 在启动屏幕完成动画之前不会出现,并且应用程序将正确地将 Form1 作为“启动表单”,因此当启动关闭并打开 Form1 时它不会关闭。另请注意,我们没有明确打开或关闭代码中的任何表单;这是由框架本身自动为我们完成的。

使用这种方法,主窗体在启动屏幕完成之前不会打开。此外,如果您更改动画长度,则不必进入并更改 MinimumSplashScreenDisplayTime(),因为不涉及猜测。

于 2013-05-19T16:28:44.807 回答