开发者

My tray icon stops responding when my app is minimized!

开发者 https://www.devze.com 2022-12-16 01:26 出处:网络
After finding this question and following 开发者_JS百科the instructions there, plus following many of the links, I managed to get my app to appear as a system tray icon, and it disappears from the tas

After finding this question and following 开发者_JS百科the instructions there, plus following many of the links, I managed to get my app to appear as a system tray icon, and it disappears from the task bar when I minimize it.

BUT - weird behavior! When the form is open, the system tray icon works fine. But as soon as I minimize it, the tray icon stops responding to any kind of mouse click.

Other facts that may come into play: the form is started up by a thread spun off from the main thread, and it is opened with .ShowDialog(). There are also several other threads running in the background.

Any ideas what's going wrong?


Replace f.ShowDialog(); with Application.Run(f); where f is the variable holding your Form and it should work fine. The problem is that ShowDialog returns (and it's message loop ends) when you hide the form. Application.Run provides a proper message loop and your window works after it has been hidden and can be show again using the system tray icon.


Not too sure what your code looks like, but below is some code I pulled from one of our apps. Its written in VB but should not be too hard for you to convert. The key is to create an ApplicationContext class to host your code. This code has no problems showing a form and then closing it repeatedly or with minimizing or maximizing.

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

End Class

Public Class Startup

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

End Class

0

精彩评论

暂无评论...
验证码 换一张
取 消