【问题标题】:how to put an .net application in system tray when minimized?最小化时如何将.net应用程序放在系统托盘中?
【发布时间】:2010-09-09 17:13:31
【问题描述】:

谁能推荐一个好的代码示例 vb.net/c# 代码,以便在最小化时将应用程序放在系统托盘中。

【问题讨论】:

标签: .net system system-tray


【解决方案1】:

在表单中添加一个 NotifyIcon 控件,然后使用以下代码:

    private void frm_main_Resize(object sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Minimized)
        {
           this.ShowInTaskbar = false;
           this.Hide();
           notifyIcon1.Visible = true;
        }
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        this.Show();
        this.WindowState = FormWindowState.Normal;
        this.ShowInTaskbar = true;
        notifyIcon1.Visible = false;
    }

您可能不需要设置 ShowInTaskbar 属性。

【讨论】:

  • 您的样本严重不完整。
  • 您缺少表单中的声明、notifyIcon 控件以及您缺少覆盖的 dispose 方法:)
  • 虽然我注意到你编辑了,并添加了一些说明,所以忽略我的批评:)
【解决方案2】:

您可以利用名为 NotifyIcon 的内置控件。这会在显示时创建一个托盘图标。 @Phillip 有一个比较完整的代码示例。

但有一个问题:

您必须覆盖您的应用程序主窗体 Dispose 方法才能在 NotifyIcon 上调用 Dispose,否则它会在应用程序退出后留在您的托盘中。

public void Form_Dispose(object sender, EventArgs e)
{
   if (this.Disposing)
      notifyIcon1.Dispose();
}

类似的东西。

【讨论】:

  • 如果您使用设计器将通知图标放在表单上,​​则不需要这样做 - VS 生成的 Dispose() 将调用 components.Dispose() ,它应该处理所有创建的组件它作为一个容器。
【解决方案3】:

您可以通过向表单添加 NotifyIcon 并处理表单的调整大小事件来实现此目的。要从托盘返回,请处理 NotifyIcon 的双击事件。

如果你想添加一点动画,你也可以这样做......

1) 添加如下模块:

Module AnimatedMinimizeToTray
Structure RECT
    Public left As Integer
    Public top As Integer
    Public right As Integer
    Public bottom As Integer
End Structure

Structure APPBARDATA
    Public cbSize As Integer
    Public hWnd As IntPtr
    Public uCallbackMessage As Integer
    Public uEdge As ABEdge
    Public rc As RECT
    Public lParam As IntPtr
End Structure

Enum ABMsg
    ABM_NEW = 0
    ABM_REMOVE = 1
    ABM_QUERYPOS = 2
    ABM_SETPOS = 3
    ABM_GETSTATE = 4
    ABM_GETTASKBARPOS = 5
    ABM_ACTIVATE = 6
    ABM_GETAUTOHIDEBAR = 7
    ABM_SETAUTOHIDEBAR = 8
    ABM_WINDOWPOSCHANGED = 9
    ABM_SETSTATE = 10
End Enum

Enum ABNotify
    ABN_STATECHANGE = 0
    ABN_POSCHANGED
    ABN_FULLSCREENAPP
    ABN_WINDOWARRANGE
End Enum

Enum ABEdge
    ABE_LEFT = 0
    ABE_TOP
    ABE_RIGHT
    ABE_BOTTOM
End Enum

Public Declare Function SHAppBarMessage Lib "shell32.dll" Alias "SHAppBarMessage" (ByVal dwMessage As Integer, ByRef pData As APPBARDATA) As Integer
Public Const ABM_GETTASKBARPOS As Integer = &H5&
Public Const WM_SYSCOMMAND As Integer = &H112
Public Const SC_MINIMIZE As Integer = &HF020

Public Sub AnimateWindow(ByVal ToTray As Boolean, ByRef frm As Form, ByRef icon As NotifyIcon)
    ' get the screen dimensions
    Dim screenRect As Rectangle = Screen.GetBounds(frm.Location)

    ' figure out where the taskbar is (and consequently the tray)
    Dim destPoint As Point
    Dim BarData As APPBARDATA
    BarData.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(BarData)
    SHAppBarMessage(ABMsg.ABM_GETTASKBARPOS, BarData)
    Select Case BarData.uEdge
        Case ABEdge.ABE_BOTTOM, ABEdge.ABE_RIGHT
            ' Tray is to the Bottom Right
            destPoint = New Point(screenRect.Width, screenRect.Height)

        Case ABEdge.ABE_LEFT
            ' Tray is to the Bottom Left
            destPoint = New Point(0, screenRect.Height)

        Case ABEdge.ABE_TOP
            ' Tray is to the Top Right
            destPoint = New Point(screenRect.Width, 0)

    End Select

    ' setup our loop based on the direction
    Dim a, b, s As Single
    If ToTray Then
        a = 0
        b = 1
        s = 0.05
    Else
        a = 1
        b = 0
        s = -0.05
    End If

    ' "animate" the window
    Dim curPoint As Point, curSize As Size
    Dim startPoint As Point = frm.Location
    Dim dWidth As Integer = destPoint.X - startPoint.X
    Dim dHeight As Integer = destPoint.Y - startPoint.Y
    Dim startWidth As Integer = frm.Width
    Dim startHeight As Integer = frm.Height
    Dim i As Single
    For i = a To b Step s
        curPoint = New Point(startPoint.X + i * dWidth, startPoint.Y + i * dHeight)
        curSize = New Size((1 - i) * startWidth, (1 - i) * startHeight)
        ControlPaint.DrawReversibleFrame(New Rectangle(curPoint, curSize), frm.BackColor, FrameStyle.Thick)
        System.Threading.Thread.Sleep(15)
        ControlPaint.DrawReversibleFrame(New Rectangle(curPoint, curSize), frm.BackColor, FrameStyle.Thick)
    Next


    If ToTray Then
        ' hide the form and show the notifyicon
        frm.Hide()
        icon.Visible = True
    Else
        ' hide the notifyicon and show the form
        icon.Visible = False
        frm.Show()
    End If

End Sub
End Module

2) 在表单中添加一个 NotifyIcon 并添加以下内容:

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32() = SC_MINIMIZE Then
        AnimateWindow(True, Me, NotifyIcon1)
        Exit Sub
    End If
    MyBase.WndProc(m)
End Sub

Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
    AnimateWindow(False, Me, NotifyIcon1)
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 2010-10-19
    • 2012-05-01
    相关资源
    最近更新 更多