在Windows启动时运行没有表单的程序

时间:2017-10-04 02:29:45

标签: vb.net

基本上我想要的是显示:

  

notifyicon.visible = true

我的意思是在Windows启动时显示一个托盘图标,但不应显示程序表单,我怎么能实现它?

我知道通过添加到注册表,您可以在下面的启动示例中运行该程序

Dim regkey As RegistryKey
        regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", True)
        If (runonstartupToolStripMenuItem.Checked = True) Then
            ' Add the value in the registry so that the application runs at startup
            regkey.SetValue("Your Application Name", Application.ExecutablePath.ToString())
        Else
            ' Remove the value from the registry so that the application doesn't start
            regkey.DeleteValue("Your Application Name", False)
        End If

但是这将运行整个程序,并且将显示我不想要的表单,除非用户手动启动它。

2 个答案:

答案 0 :(得分:2)

将此代码添加到表单中:

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Me.Hide()                    ' <= Required
      Me.ShowInTaskbar = False     ' <= Required
      NotifyIcon1.Visible = True  
      NotifyIcon1.ShowBalloonTip(10000)
 End Sub

当窗口启动时程序打开时,应该打开一个唯一的参数, 当找到唯一参数时,表单将被隐藏, 相反,如果用户打开程序,将没有参数,然后表单可以显示。

答案 1 :(得分:0)

我使用复选框来设置和取消设置:

Private Sub cbStartup_CheckedChanged(sender As Object, e As EventArgs) Handles cbStartup.CheckedChanged
        Dim applicationName As String = Application.ProductName
        Dim applicationPath As String = Application.ExecutablePath

        If cbStartup.Checked = True Then
            Dim regKey As Microsoft.Win32.RegistryKey
            regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
            regKey.SetValue(applicationName, """" & applicationPath & """")
            regKey.Close()
        Else
            Dim regKey As Microsoft.Win32.RegistryKey
            regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
            regKey.DeleteValue(applicationName, False)
            regKey.Close()
        End If
    End Sub

在Windows 10 64Bit上运行VS 2015测试。