by Jim
Nov 04, 2006 11:17 PM
Getting the location of the system tray from vb.net or C# isn't terribly hard. But it isn't documented either, so figuring it out takes a little research. The trick here is knowing the names of the taskbar and system tray windows. This can be discovered using the Spy++ tool. For those of you who don't remember the days before .NET, Spy++ is in your Visual Studio tools folder in the start menu. It was once very handy, but I don't remember the last time I used it.
Private Declare Ansi Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Ansi Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, _
ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
Private Declare Function GetWindowRect Lib "user32.dll" ( _
ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean
Private Structure RECT
Public left As Integer
Public top As Integer
Public right As Integer
Public bottom As Integer
End Structure
Private Function GetSystemTrayLocation() As Rectangle
'Get the location of the system tray
Dim rect As New RECT
Dim retval As Rectangle = Nothing
Dim hTaskbarHandle As IntPtr = FindWindow("Shell_TrayWnd", Nothing)
If hTaskbarHandle <> 0 Then
Dim hSystemTray As IntPtr = FindWindowEx(hTaskbarHandle, IntPtr.Zero, _
"TrayNotifyWnd", Nothing)
If hSystemTray <> 0 Then
GetWindowRect(hSystemTray, rect)
retval = Rectangle.FromLTRB( _
rect.left, rect.top, rect.right, rect.bottom)
End If
End If
Return retval
End Function