2011-03-21 26 views

回答

6

形式是不是絕對必要的。您可以實例化的NotifyIcon和使用,而無需創建一個表單:

Public Class AppContext 
    Inherits ApplicationContext 

    Private notifyIcon As NotifyIcon 
    Private appActive As Boolean 

    Public Sub New() 
     AddHandler Application.ApplicationExit, AddressOf OnApplicationExit 

     notifyIcon = New NotifyIcon() 
     notifyIcon.Icon = My.Resources.ActiveIcon 
     notifyIcon.Text = "The app is active." 

     AddHandler notifyIcon.MouseClick, AddressOf OnIconMouseClick 

     appActive = True 
     notifyIcon.Visible = True 

    End Sub 

    Private Sub OnApplicationExit(ByVal sender As Object, ByVal e As EventArgs) 
     If notifyIcon IsNot Nothing Then 
      notifyIcon.Dispose() 
     End If 
    End Sub 

    Private Sub OnIconMouseClick(ByVal sender As Object, ByVal e As MouseEventArgs) 

     If e.Button = MouseButtons.Left Then 
      appActive = Not appActive 
      notifyIcon.Icon = If(appActive, My.Resources.ActiveIcon, My.Resources.InactiveIcon) 
      notifyIcon.Text = If(appActive, "The app is active.", "The app is not active.") 
     Else 
      If MsgBox("Do you want to Exit?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then 
       notifyIcon.Visible = False 
       ExitThread() 
      End If 
     End If 
    End Sub 

End Class 

,然後開始從子主你的應用程序:

Public Module EntryPoint 
    Public Sub Main() 
     Dim ctx As New AppContext() 
     Application.Run(ctx) 
    End Sub 
End Module 
+0

克里斯,非常感謝你。這正是我所期待的。這工作得很好,並解決了我遇到的其他一些問題。謝謝! – avword 2011-03-27 21:50:59

0

只要把形式透明,並將其調整到1×1 .. 並添加NotifyIcon的..

而且在窗體的Load事件做到這一點:

NotifyIcon.Visible =真

然後做任何你想要的東西..

你可以創建一個上下文菜單條(當你右鍵點擊它時的一個菜單) PS:如果你這樣做,你需要去NotifyIcon屬性並設置上下文菜單條您創建..

希望它可以幫助你..

相關問題