Introduction
In my Tray Calendar application I wanted to make the "About" box and Options dialog
appear to expand and contract into and out of the system tray. The animation was taken care of by using the
DrawAnimatedRects
function (see the article "Using the DrawAnimatedRects() function"), but I need to know where the system tray was.
Finding the System Tray
My first thought was to simply use FindWindow
using the window name "TrayNotifyWnd",
but unfortunately this did not work as planned. Instead I was able to get a handle to "Shell_TrayWnd",
and from there work my way down using EnumChildWindows
to get a hold of the system tray
and the clock window. Subtracting the size of the system clock window from that of the system tray
then gave me the working area of the system tray.
The code is shown below:
BOOL CALLBACK FindTrayWnd(HWND hwnd, LPARAM lParam)
{
TCHAR szClassName[256];
GetClassName(hwnd, szClassName, 255);
if (_tcscmp(szClassName, _T("TrayNotifyWnd")) == 0)
{
CRect *pRect = (CRect*) lParam;
::GetWindowRect(hwnd, pRect);
return TRUE;
}
if (_tcscmp(szClassName, _T("TrayClockWClass")) == 0)
{
CRect *pRect = (CRect*) lParam;
CRect rectClock;
::GetWindowRect(hwnd, rectClock);
if (rectClock.bottom < lpRect->bottom-5)
lpRect->top = rectClock.bottom;
else
lpRect->right = rectClock.left;
return FALSE;
}
return TRUE;
}
void GetTrayWndRect(LPRECT lprect)
{
#define DEFAULT_RECT_WIDTH 150
#define DEFAULT_RECT_HEIGHT 30
HWND hShellTrayWnd = ::FindWindow(_T("Shell_TrayWnd"), NULL);
if (hShellTrayWnd)
{
::GetWindowRect(hShellTrayWnd, lprect);
EnumChildWindows(hShellTrayWnd, FindTrayWnd, (LPARAM)lprect);
return;
}
APPBARDATA appBarData;
appBarData.cbSize=sizeof(appBarData);
if (SHAppBarMessage(ABM_GETTASKBARPOS,&appBarData))
{
switch(appBarData.uEdge)
{
case ABE_LEFT:
case ABE_RIGHT:
lprect->top = appBarData.rc.bottom-100;
lprect->bottom = appBarData.rc.bottom-16;
lprect->left = appBarData.rc.left;
lprect->right = appBarData.rc.right;
break;
case ABE_TOP:
case ABE_BOTTOM:
lprect->top = appBarData.rc.top;
lprect->bottom = appBarData.rc.bottom;
lprect->left = appBarData.rc.right-100;
lprect->right = appBarData.rc.right-16;
break;
}
return;
}
if (hShellTrayWnd)
{
::GetWindowRect(hShellTrayWnd, lprect);
if (lprect->right - lprect->left > DEFAULT_RECT_WIDTH)
lprect->left = lprect->right - DEFAULT_RECT_WIDTH;
if (lprect->bottom - lprect->top > DEFAULT_RECT_HEIGHT)
lprect->top = lprect->bottom - DEFAULT_RECT_HEIGHT;
return;
}
SystemParametersInfo(SPI_GETWORKAREA,0,lprect, 0);
lprect->left = lprect->right - DEFAULT_RECT_WIDTH;
lprect->top = lprect->bottom - DEFAULT_RECT_HEIGHT;
}
Updates
Compensation was made for the case where the system clock is above the system tray,
and Matthew Ellis improved the GetTrayWndRect to provide more fallbacks.
Chris Maunder is the co-founder of
CodeProject, DeveloperMedia and ContentLab, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.
In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project,
CodeProject.AI.
In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. While at CodeProject, Chris' roles included Architecture and coding, Product Development, Content Creation, Community Growth, Client Satisfaction and Systems Automation, and many, many sales meetings. All while keeping his sense of humour.