如何在 Windows 启动时最小化应用程序 - Visual Basic [英] How to minimize app at windows startup - visual basic
问题描述
这段代码有什么问题?我无法在启动时关闭我的应用程序.如果我将 me.close()
更改为它实现的另一个值,但对于 me.close()
则不会.我是编码和 Visual Basic 的新手
What's wrong with this code? I can't close my app at startup. If I change me.close()
with another value it implements but for me.close()
it doesn't. I'm new at coding and Visual Basic
Dim oktoclose As Boolean
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If Not oktoclose Then
e.Cancel = True
Me.Hide()
Else
AddHandler My.Application.StartupNextInstance, AddressOf MyApplication_StartupNextInstance
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.TbTableAdapter.Fill(Me.Data1DataSet.tb) Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", Application.ProductName, Application.ExecutablePath & Space(1) & "/onboot", RegistryValueKind.String)
AddHandler My.Application.StartupNextInstance, AddressOf MyApplication_StartupNextInstance
For Each arg As String In Environment.GetCommandLineArgs()
If arg = "/onboot" Then
me.close()
End If
Next
End Sub
Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs)
If noti1.Visible Then
Me.Show()
End If
e.BringToForeground = True
End Sub
推荐答案
使用 Form.Shown
事件.Form.Load
在窗口完全创建之前 被引发,因此还没有窗口可以关闭/隐藏.
Use the Form.Shown
event instead. Form.Load
is raised before the window is fully created, thus there is no window to close/hide yet.
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
For Each arg As String In Environment.GetCommandLineArgs()
If arg = "/onboot" Then
Me.Close()
End If
Next
End Sub
<小时>
为了回应您的评论,为了避免表单在启动时闪烁,在 Load
事件中将其 Opacity
设置为 0:
In response to your comment, to avoid the form flashing at startup set its Opacity
to 0 in the Load
event:
Dim CloseOnShow As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
...your other code here...
For Each arg As String In Environment.GetCommandLineArgs()
If arg = "/onboot" Then
Me.Opacity = 0.0
CloseOnShow = True
End If
Next
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If CloseOnShow Then
Me.Close()
End If
End Sub
然后,在再次显示表单之前,将 Opacity
设置回 1:
Then, before you show the form again set the Opacity
back to 1:
Me.Opacity = 1.0
这篇关于如何在 Windows 启动时最小化应用程序 - Visual Basic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!