隐藏应用程序时不执行Sub或Function

时间:2017-09-20 11:16:29

标签: vb.net

我有一个VB.NET应用程序,使用NotifyIcon在启动时隐藏。

有一个每10秒执行一次的计时器......在计时器中:

如果存在文件,则不会执行任何操作。但如果它不存在,我将执行:

MySubOrFunction()

如果应用程序被隐藏,则不执行MySubOrFunction()代码...正如我上面告诉你的那样“我的应用程序在启动时隐藏”

我尝试了“Public Sub”,“Private Sub”,同样也用于“功能”......我也尝试过“共享功能”,但它无法工作,因为我的一些代码需要访问另一个不在里面的代码功能/子。

我试图保持表格打开,一切顺利,但我希望隐藏它:(

任何帮助将不胜感激!

谢谢! : - )

编辑:示例在这里(抱歉遗漏它):

在Form_Load事件:

Timer2.Enabled = True
Timer2.Start()

Timer2每5秒执行一次,这里是Timer2_Tick

Timer1.Enabled = True
Timer1.Start()

这是Timer1_Tick

MySubOrFunction("start")

这是MySubOrFunction

Private Sub MySubOrFunction(ByVal status As String)
    If status = "start" Then
        ' My Code Here
        Return
    End If

    If status = "stop" Then
        Return ' stop the function
    End If
End Sub

以下是我隐藏申请的方式:

Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            NotifyIcon1.Visible = True
            NotifyIcon1.Icon = SystemIcons.Application
            NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
            NotifyIcon1.BalloonTipTitle = "AppName"
            NotifyIcon1.BalloonTipText = "AppDesc"
            NotifyIcon1.ShowBalloonTip(50000)
            'Me.Hide()
            ShowInTaskbar = False
        End If
    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Me.Visible = True
        'Me.Show()
        ShowInTaskbar = True
        Me.WindowState = FormWindowState.Normal
        NotifyIcon1.Visible = False
    End Sub

Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
        If Not Me.IsHandleCreated Then
            Me.CreateHandle()
            value = False
        End If
        MyBase.SetVisibleCore(value)
    End Sub

当应用程序未被隐藏时,此工作正常......这些只是示例,而不是完整的代码!

2 个答案:

答案 0 :(得分:2)

问题是由于您在Form_Load()中隐藏表单的方式,SetVisibleCore未执行。

Form_HandleCreated()中启用计时器,它应该可以正常工作。

答案 1 :(得分:0)

我用来隐藏表单的方式就是问题!

我删除了NotifyIcon并删除了以下内容:

Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            NotifyIcon1.Visible = True
            NotifyIcon1.Icon = SystemIcons.Application
            NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
            NotifyIcon1.BalloonTipTitle = "AppName"
            NotifyIcon1.BalloonTipText = "AppDesc"
            NotifyIcon1.ShowBalloonTip(50000)
            'Me.Hide()
            ShowInTaskbar = False
        End If
    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Me.Visible = True
        'Me.Show()
        ShowInTaskbar = True
        Me.WindowState = FormWindowState.Normal
        NotifyIcon1.Visible = False
    End Sub

Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
        If Not Me.IsHandleCreated Then
            Me.CreateHandle()
            value = False
        End If
        MyBase.SetVisibleCore(value)
    End Sub

这是在启动时隐藏应用程序的新方法

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' hide application on startup
    Me.Opacity = 0.0
    Me.ShowInTaskbar = False
    ' ---------------------
End Sub

Private Sub Form1_Shown(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Shown

        Me.Visible = False

End Sub