<p>can anyone please suggest a <strong>good code example</strong> of vb.net/c# code to put the application in system tray when minized.</p>

[{'answer_id': 76120, 'author': 'Phillip Wells', 'author_id': 3012, 'author_profile': 'https://Stackoverflow.com/users/3012', 'pm_score': 5, 'selected': True, 'text': '<p>Add a NotifyIcon control to your form, then use the following code:</p>\n\n<pre><code>    private void frm_main_Resize(object sender, EventArgs e)\n    {\n        if (this.WindowState == FormWindowState.Minimized)\n        {\n           this.ShowInTaskbar = false;\n           this.Hide();\n           notifyIcon1.Visible = true;\n        }\n    }\n\n    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)\n    {\n        this.Show();\n        this.WindowState = FormWindowState.Normal;\n        this.ShowInTaskbar = true;\n        notifyIcon1.Visible = false;\n    }\n</code></pre>\n\n<p>You may not need to set the ShowInTaskbar property.</p>\n'}, {'answer_id': 76160, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 2, 'selected': False, 'text': '<p>You can leverage a built in control called NotifyIcon. This creates a tray icon when shown. @Phillip has a code example that is somewhat complete.</p>\n\n<p>There is a gotcha though:</p>\n\n<p>You must override your applications main form Dispose method to call Dispose on NotifyIcon, otherwise it will stay in your tray after application exits.</p>\n\n<pre><code>public void Form_Dispose(object sender, EventArgs e)\n{\n   if (this.Disposing)\n      notifyIcon1.Dispose();\n}\n</code></pre>\n\n<p>Something like that.</p>\n'}, {'answer_id': 76708, 'author': 'Sean Gough', 'author_id': 12842, 'author_profile': 'https://Stackoverflow.com/users/12842', 'pm_score': 0, 'selected': False, 'text': '<p>You can do this by adding a NotifyIcon to your form and handling the form\'s resize event.  To get back from the tray handle the NotifyIcon\'s double-click event.</p>\n\n<p>If you want to add a little animation you can do this too...</p>\n\n<p>1) Add the following module:</p>\n\n<pre><code>Module AnimatedMinimizeToTray\nStructure RECT\n    Public left As Integer\n    Public top As Integer\n    Public right As Integer\n    Public bottom As Integer\nEnd Structure\n\nStructure APPBARDATA\n    Public cbSize As Integer\n    Public hWnd As IntPtr\n    Public uCallbackMessage As Integer\n    Public uEdge As ABEdge\n    Public rc As RECT\n    Public lParam As IntPtr\nEnd Structure\n\nEnum ABMsg\n    ABM_NEW = 0\n    ABM_REMOVE = 1\n    ABM_QUERYPOS = 2\n    ABM_SETPOS = 3\n    ABM_GETSTATE = 4\n    ABM_GETTASKBARPOS = 5\n    ABM_ACTIVATE = 6\n    ABM_GETAUTOHIDEBAR = 7\n    ABM_SETAUTOHIDEBAR = 8\n    ABM_WINDOWPOSCHANGED = 9\n    ABM_SETSTATE = 10\nEnd Enum\n\nEnum ABNotify\n    ABN_STATECHANGE = 0\n    ABN_POSCHANGED\n    ABN_FULLSCREENAPP\n    ABN_WINDOWARRANGE\nEnd Enum\n\nEnum ABEdge\n    ABE_LEFT = 0\n    ABE_TOP\n    ABE_RIGHT\n    ABE_BOTTOM\nEnd Enum\n\nPublic Declare Function SHAppBarMessage Lib "shell32.dll" Alias "SHAppBarMessage" (ByVal dwMessage As Integer, ByRef pData As APPBARDATA) As Integer\nPublic Const ABM_GETTASKBARPOS As Integer = &amp;H5&amp;\nPublic Const WM_SYSCOMMAND As Integer = &amp;H112\nPublic Const SC_MINIMIZE As Integer = &amp;HF020\n\nPublic Sub AnimateWindow(ByVal ToTray As Boolean, ByRef frm As Form, ByRef icon As NotifyIcon)\n    \' get the screen dimensions\n    Dim screenRect As Rectangle = Screen.GetBounds(frm.Location)\n\n    \' figure out where the taskbar is (and consequently the tray)\n    Dim destPoint As Point\n    Dim BarData As APPBARDATA\n    BarData.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(BarData)\n    SHAppBarMessage(ABMsg.ABM_GETTASKBARPOS, BarData)\n    Select Case BarData.uEdge\n        Case ABEdge.ABE_BOTTOM, ABEdge.ABE_RIGHT\n            \' Tray is to the Bottom Right\n            destPoint = New Point(screenRect.Width, screenRect.Height)\n\n        Case ABEdge.ABE_LEFT\n            \' Tray is to the Bottom Left\n            destPoint = New Point(0, screenRect.Height)\n\n        Case ABEdge.ABE_TOP\n            \' Tray is to the Top Right\n            destPoint = New Point(screenRect.Width, 0)\n\n    End Select\n\n    \' setup our loop based on the direction\n    Dim a, b, s As Single\n    If ToTray Then\n        a = 0\n        b = 1\n        s = 0.05\n    Else\n        a = 1\n        b = 0\n        s = -0.05\n    End If\n\n    \' "animate" the window\n    Dim curPoint As Point, curSize As Size\n    Dim startPoint As Point = frm.Location\n    Dim dWidth As Integer = destPoint.X - startPoint.X\n    Dim dHeight As Integer = destPoint.Y - startPoint.Y\n    Dim startWidth As Integer = frm.Width\n    Dim startHeight As Integer = frm.Height\n    Dim i As Single\n    For i = a To b Step s\n        curPoint = New Point(startPoint.X + i * dWidth, startPoint.Y + i * dHeight)\n        curSize = New Size((1 - i) * startWidth, (1 - i) * startHeight)\n        ControlPaint.DrawReversibleFrame(New Rectangle(curPoint, curSize), frm.BackColor, FrameStyle.Thick)\n        System.Threading.Thread.Sleep(15)\n        ControlPaint.DrawReversibleFrame(New Rectangle(curPoint, curSize), frm.BackColor, FrameStyle.Thick)\n    Next\n\n\n    If ToTray Then\n        \' hide the form and show the notifyicon\n        frm.Hide()\n        icon.Visible = True\n    Else\n        \' hide the notifyicon and show the form\n        icon.Visible = False\n        frm.Show()\n    End If\n\nEnd Sub\nEnd Module\n</code></pre>\n\n<p>2) Add a NotifyIcon to your form an add the following:</p>\n\n<pre><code>Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)\n    If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32() = SC_MINIMIZE Then\n        AnimateWindow(True, Me, NotifyIcon1)\n        Exit Sub\n    End If\n    MyBase.WndProc(m)\nEnd Sub\n\nPrivate Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick\n    AnimateWindow(False, Me, NotifyIcon1)\nEnd Sub\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/76079', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13337/']