代码之家  ›  专栏  ›  技术社区  ›  steffenj

如何使Windows窗体.NET应用程序显示为任务栏图标?

  •  19
  • steffenj  · 技术社区  · 17 年前

    你是如何处理鼠标按钮点击图标的?

    6 回复  |  直到 7 年前
        1
  •  21
  •   Jimi    7 年前

    NotifyIcon

    Private Sub frmMain_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            Me.ShowInTaskbar = False
        Else
            Me.ShowInTaskbar = True
        End If
    End Sub
    
    Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
        Me.WindowState = FormWindowState.Normal
    End Sub
    

    我偶尔会使用引出序号文本来通知用户,即:

     Me.NotifyIcon1.ShowBalloonTip(3000, "This is a notification title!!", "This is notification text.", ToolTipIcon.Info)
    
        2
  •  6
  •   Carl    17 年前

    可以将工具箱中的NotifyIcon组件添加到主窗体中。

    这有一些事件,比如MouseDoubleClick,可以用来处理各种事件。

    编辑:如果希望图标属性在systray中正确显示,则必须确保将其设置为有效的.ico文件。

        3
  •  1
  •   Jimi    7 年前

    Tom's answer ,我只想在最小化应用程序时使图标可见。
    要执行此操作,请设置 Visible = False NotifyIcon 并使用下面的代码。

    我也有下面的代码,以隐藏图标在关闭过程中,防止恼人的

    Private Sub Form_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            Hide()
            NotifyIcon1.Visible = True
            NotifyIcon1.ShowBalloonTip(3000, NotifyIcon1.Text, "Minimized to tray", ToolTipIcon.Info)
        End If
    End Sub
    
    Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
        Show()
        Me.WindowState = FormWindowState.Normal
        Me.Activate()
        NotifyIcon1.Visible = False
    End Sub
    
    Private Sub Form_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        NotifyIcon1.Visible = False
        Dim index As Integer
        While index < My.Application.OpenForms.Count
            If My.Application.OpenForms(index) IsNot Me Then
                My.Application.OpenForms(index).Close()
            End If
            index += 1
        End While
    End Sub
    

    如果要添加右键单击菜单:

    VB.NET: How to Make a Right Click Menu for a Tray Icon

    设置用于承载托盘图标上下文菜单的表单

    • 在属性中,将FormBorderStyle设置为“无”。
    • 将ShowInTaskbar设置为False(因为我们不希望在右键单击任务栏图标时任务栏中出现图标!)。
    • 将StartPosition设置为手动。
    • 将TopMost设置为True。
    • 将ContextMenuStrip添加到新表单中,并根据需要命名它。

    后面的表单代码如下所示:

    Private Sub Form_Deactivate(sender As Object, e As EventArgs) Handles Me.Deactivate
        Me.Close()
    End Sub
    
    Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ContextMenuStrip1.Show(Cursor.Position)
        Me.Left = ContextMenuStrip1.Left + 1
        Me.Top = ContextMenuStrip1.Top + 1
    End Sub
    
    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        MainForm.NotifyIcon1.Visible = False
        End
    End Sub
    

    然后我将notifyicon鼠标事件更改为( TrayIconMenuForm 是用于提供上下文菜单的表单的名称):

    Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
        Select Case e.Button
            Case Windows.Forms.MouseButtons.Left
                Show()
                Me.WindowState = FormWindowState.Normal
                Me.Activate()
                NotifyIcon1.Visible = False
            Case Windows.Forms.MouseButtons.Right
                TrayIconMenuForm.Show() 'Shows the Form that is the parent of "traymenu"
                TrayIconMenuForm.Activate() 'Set the Form to "Active", that means that that will be the "selected" window
                TrayIconMenuForm.Width = 1 'Set the Form width to 1 pixel, that is needed because later we will set it behind the "traymenu"
                TrayIconMenuForm.Height = 1 'Set the Form Height to 1 pixel, for the same reason as above
            Case Else
                'Do nothing
        End Select
    End Sub
    
    推荐文章