Results 1 to 9 of 9

Thread: How Do I Minimize A Form To Its NotifyIcon In The System Tray?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    How Do I Minimize A Form To Its NotifyIcon In The System Tray?

    I need to make a form minimize to it's system tray icon instead of minimizing to its task bar button, how?

    Thanks,
    Louix.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How Do I Minimize A Form To Its NotifyIcon In The System Tray?

    Strictly speaking you don't actually minimise a form to the system tray. That's a conceptual thing rather than what actually happens. You simply add a NotifyIcon to the form and set it's Visible property to True.

    You can handle the SizeChanged event of the form and test the WindowState property. If it's Minimized you Hide the form and Show the NotifyIcon, otherwise you do the opposite.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How Do I Minimize A Form To Its NotifyIcon In The System Tray?

    Hmmm... I tested what I was sure had worked before and it didn't. This does:
    vb.net Code:
    1. Private Sub Form1_SizeChanged(ByVal sender As Object, _
    2.                               ByVal e As EventArgs) Handles Me.SizeChanged
    3.     Me.UpdateTrayState(Me.WindowState = FormWindowState.Minimized)
    4. End Sub
    5.  
    6. Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As Object, _
    7.                                          ByVal e As MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
    8.     Me.UpdateTrayState(False)
    9.     Me.WindowState = FormWindowState.Normal
    10. End Sub
    11.  
    12. Private Sub UpdateTrayState(ByVal minimiseToTray As Boolean)
    13.     Me.Visible = Not minimiseToTray
    14.     Me.NotifyIcon1.Visible = minimiseToTray
    15. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: How Do I Minimize A Form To Its NotifyIcon In The System Tray?

    No, what I mean is I need the minimize animation to fly over to the far right of the screen instead of minimizing to its task bar button. Hence "I need to make a form minimize to it's system tray icon instead of minimizing to its task bar button".

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How Do I Minimize A Form To Its NotifyIcon In The System Tray?

    It's also worth noting that setting the form's ShowInTaskbar property instead of its Visible property will allow you to restore the form using Alt+Tab, although it will not be activated automatically when you restore it that way.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How Do I Minimize A Form To Its NotifyIcon In The System Tray?

    Quote Originally Posted by Louix
    No, what I mean is I need the minimize animation to fly over to the far right of the screen instead of minimizing to its task bar button. Hence "I need to make a form minimize to it's system tray icon instead of minimizing to its task bar button".
    Well that's not possible because, as I said, there really is no such thing as minimising to the system tray. Form's are simply minimised or not. You can hide the form's Taskbar icon and display an icon in the Notification Area if you want but that doesn't mean that the form was minimised to there.

    An icon on the Taskbar directly represents a form. An icon in the Notification Area does not. A single form might create multiple icons in the system tray if you want or, as is usually the case, the entire application is represented by a single icon. There is no direct relationship between a form and a system tray icon so there is no animation to display such a relationship.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: How Do I Minimize A Form To Its NotifyIcon In The System Tray?

    Okay, thank you very much for your help anyway, it might help with another part of this app.

  8. #8
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: How Do I Minimize A Form To Its NotifyIcon In The System Tray?

    Here is some code that I got from Google search.

    vb.net Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.  
    4.     Structure RECT
    5.         Public left As Integer
    6.         Public top As Integer
    7.         Public right As Integer
    8.         Public bottom As Integer
    9.     End Structure
    10.  
    11.     Structure APPBARDATA
    12.         Public cbSize As Integer
    13.         Public hWnd As IntPtr
    14.         Public uCallbackMessage As Integer
    15.         Public uEdge As ABEdge
    16.         Public rc As RECT
    17.         Public lParam As IntPtr
    18.     End Structure
    19.  
    20.     Enum ABMsg
    21.         ABM_NEW = 0
    22.         ABM_REMOVE = 1
    23.         ABM_QUERYPOS = 2
    24.         ABM_SETPOS = 3
    25.         ABM_GETSTATE = 4
    26.         ABM_GETTASKBARPOS = 5
    27.         ABM_ACTIVATE = 6
    28.         ABM_GETAUTOHIDEBAR = 7
    29.         ABM_SETAUTOHIDEBAR = 8
    30.         ABM_WINDOWPOSCHANGED = 9
    31.         ABM_SETSTATE = 10
    32.     End Enum
    33.  
    34.     Enum ABNotify
    35.         ABN_STATECHANGE = 0
    36.         ABN_POSCHANGED
    37.         ABN_FULLSCREENAPP
    38.         ABN_WINDOWARRANGE
    39.     End Enum
    40.  
    41.     Enum ABEdge
    42.         ABE_LEFT = 0
    43.         ABE_TOP
    44.         ABE_RIGHT
    45.         ABE_BOTTOM
    46.     End Enum
    47.  
    48.     Public Declare Function SHAppBarMessage Lib "shell32.dll" Alias "SHAppBarMessage" (ByVal dwMessage As Integer, ByRef pData As APPBARDATA) As Integer
    49.  
    50.     Private Const ABM_GETTASKBARPOS As Integer = &H5&
    51.     Private Const WM_SYSCOMMAND As Integer = &H112
    52.     Private Const SC_MINIMIZE As Integer = &HF020
    53.  
    54.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    55.         If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32() = SC_MINIMIZE Then
    56.             AnimateWindow(True)
    57.             Exit Sub
    58.         End If
    59.  
    60.         MyBase.WndProc(m)
    61.     End Sub
    62.  
    63.     Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
    64.         AnimateWindow(False)
    65.     End Sub
    66.  
    67.     Private Sub AnimateWindow(ByVal ToTray As Boolean)
    68.         ' get the screen dimensions
    69.         Dim screenRect As Rectangle = Screen.PrimaryScreen.GetBounds(Me.Location)
    70.  
    71.         ' figure out where the taskbar is (and consequently the tray)
    72.         Dim destPoint As Point
    73.         Dim BarData As APPBARDATA
    74.         BarData.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(BarData)
    75.         SHAppBarMessage(ABMsg.ABM_GETTASKBARPOS, BarData)
    76.         Select Case BarData.uEdge
    77.             Case ABEdge.ABE_BOTTOM, ABEdge.ABE_RIGHT
    78.                 ' Tray is to the Bottom Right
    79.                 destPoint = New Point(screenRect.Width, screenRect.Height)
    80.  
    81.             Case ABEdge.ABE_LEFT
    82.                 ' Tray is to the Bottom Left
    83.                 destPoint = New Point(0, screenRect.Height)
    84.  
    85.             Case ABEdge.ABE_TOP
    86.                 ' Tray is to the Top Right
    87.                 destPoint = New Point(screenRect.Width, 0)
    88.  
    89.         End Select
    90.  
    91.         ' setup our loop based on the direction
    92.         Dim a, b, s As Single
    93.         If ToTray Then
    94.             a = 0
    95.             b = 1
    96.             s = 0.05
    97.         Else
    98.             a = 1
    99.             b = 0
    100.             s = -0.05
    101.         End If
    102.  
    103.         ' "animate" the window
    104.         Dim curPoint As Point, curSize As Size
    105.         Dim startPoint As Point = Me.Location
    106.         Dim dWidth As Integer = destPoint.X - startPoint.X
    107.         Dim dHeight As Integer = destPoint.Y - startPoint.Y
    108.         Dim startWidth As Integer = Me.Width
    109.         Dim startHeight As Integer = Me.Height
    110.         Dim i As Single
    111.         For i = a To b Step s
    112.             curPoint = New Point(startPoint.X + i * dWidth, startPoint.Y + i * dHeight)
    113.             curSize = New Size((1 - i) * startWidth, (1 - i) * startHeight)
    114.             ControlPaint.DrawReversibleFrame(New Rectangle(curPoint, curSize), Me.BackColor, FrameStyle.Thick)
    115.             System.Threading.Thread.Sleep(15)
    116.             ControlPaint.DrawReversibleFrame(New Rectangle(curPoint, curSize), Me.BackColor, FrameStyle.Thick)
    117.         Next
    118.  
    119.         If ToTray Then
    120.             ' hide the form and show the notifyicon
    121.             Me.Hide()
    122.             NotifyIcon1.Visible = True
    123.         Else
    124.             ' hide the notifyicon and show the form
    125.             NotifyIcon1.Visible = False
    126.             Me.Show()
    127.         End If
    128.     End Sub
    129.  
    130. End Class

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How Do I Minimize A Form To Its NotifyIcon In The System Tray?

    Cool! Never say never.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width