我的应用程序最小化时,我的托盘图标停止响应!

时间:2010-01-13 09:46:59

标签: c# system-tray

找到this question并按照那里的说明,再加上许多链接后,我设法让我的应用程序显示为系统托盘图标,当我最小化它时,它会从任务栏中消失。< / p>

但是 - 奇怪的行为!表单打开时,系统托盘图标正常工作。但是一旦我最小化它,托盘图标就会停止响应任何类型的鼠标点击。

可能发挥作用的其他事实:表单由从主线程分离的线程启动,并以.ShowDialog()打开。还有其他几个线程在后台运行。

任何想法出了什么问题?

2 个答案:

答案 0 :(得分:1)

f.ShowDialog();替换为Application.Run(f);,其中f是包含Form的变量,它应该可以正常工作。当您隐藏表单时,问题是ShowDialog返回(并且它的消息循环结束)。 Application.Run提供了一个正确的消息循环,您的窗口在隐藏后可以正常工作,并且可以使用系统托盘图标再次显示。

答案 1 :(得分:0)

不太确定您的代码是什么样的,但下面是我从其中一个应用中提取的一些代码。它用VB编写但不应该太难转换。关键是要创建一个ApplicationContext类来托管您的代码。此代码在显示表单然后重复关闭或最小化或最大化时没有问题。

Public Class NotifyApplicationContext
Inherits ApplicationContext

Private components As System.ComponentModel.IContainer
Private Notify As System.Windows.Forms.NotifyIcon
Private Menu As System.Windows.Forms.ContextMenu
Private mnuForm As System.Windows.Forms.MenuItem
Private F As Form

Public Sub New()
    InitializeContext()
End Sub

Private Sub InitializeContext()
    Me.components = New System.ComponentModel.Container
    Me.Notify = New System.Windows.Forms.NotifyIcon(Me.components)
    SetupContextMenu()
    Notify.ContextMenu = Me.Menu
    Notify.Icon = New Icon("YourApplicationIcon", 16, 16)
    Notify.Text = "Your Application Text"
    Notify.Visible = True
    F = New Form
    F.Show()
End Sub

Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    MyBase.Dispose(disposing)
End Sub

Protected Overrides Sub ExitThreadCore()
    MyBase.ExitThreadCore()
End Sub

Private Sub SetupContextMenu()
    Me.Menu = New System.Windows.Forms.ContextMenu
    Me.mnuForm = New System.Windows.Forms.MenuItem
    Me.Menu.MenuItems.Add(mnuForm)
    mnuForm.Index = 7
    mnuForm.Text = "FormText"
    AddHandler mnuForm.Click, AddressOf Me.mnuTemp_Click
End Sub

Private Sub mnuForm_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    If F IsNot Nothing Then
        If F.Visible = True Then
            F.Close()
            F.Dispose()
            F = Nothing
        End If
    Else
        F = New Form
        F.Show()
    End If
    Notify.Text = "Change Application Text Here"
End Sub

结束班

Public Class Startup

<STAThread()> _
Public Shared Sub Main()
    Dim N As ApplicationContext = New NotifyApplicationContext
    Application.Run(N)
End Sub

结束班