Click here to Skip to main content
Click here to Skip to main content

Finding the position and dimensions of the Windows system tray

, 20 Sep 2000 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple method to get the coordinates of the system tray


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:

    // lParam will contain a pointer to a CRect structure that will be used to 
    // store the coordinates of the tray

    TCHAR szClassName[256];
    GetClassName(hwnd, szClassName, 255);

    // Did we find the Main System Tray? If so, then get its size and keep going
    if (_tcscmp(szClassName, _T("TrayNotifyWnd")) == 0)
        CRect *pRect = (CRect*) lParam;
        ::GetWindowRect(hwnd, pRect);
        return TRUE;

    // Did we find the System Clock? If so, then adjust the size of 
    // the rectangle we have and quit (clock will be found after the 
    // system tray)
    if (_tcscmp(szClassName, _T("TrayClockWClass")) == 0)
        CRect *pRect = (CRect*) lParam;
        CRect rectClock;
        ::GetWindowRect(hwnd, rectClock);
        // The system clock may be either to the right or above the system 
        // tray area. Adjust accordingly
        if (rectClock.bottom < lpRect->bottom-5)  // 5 = fudge factor
            lpRect->top = rectClock.bottom;
            lpRect->right = rectClock.left;
        return FALSE;
    return TRUE;
void GetTrayWndRect(LPRECT lprect)

    HWND hShellTrayWnd = ::FindWindow(_T("Shell_TrayWnd"), NULL);
    if (hShellTrayWnd)
        ::GetWindowRect(hShellTrayWnd, lprect);
        EnumChildWindows(hShellTrayWnd, FindTrayWnd, (LPARAM)lprect);
    // OK, we failed to get the rect from the quick hack. Either explorer isn't
    // running or it's a new version of the shell with the window class names
    // changed (how dare Microsoft change these undocumented class names!) So, we
    // try to find out what side of the screen the taskbar is connected to. We
    // know that the system tray is either on the right or the bottom of the
    // taskbar, so we can make a good guess at where to minimize to
    APPBARDATA appBarData;
    if (SHAppBarMessage(ABM_GETTASKBARPOS,&appBarData))
        // We know the edge the taskbar is connected to, so guess the rect of the
        // system tray. Use various fudge factor to make it look good
        case ABE_LEFT:
        case ABE_RIGHT:
            // We want to minimize to the bottom of the taskbar
            lprect->top    = appBarData.rc.bottom-100;
            lprect->bottom = appBarData.rc.bottom-16;
            lprect->left   = appBarData.rc.left;
            lprect->right  = appBarData.rc.right;
        case ABE_TOP:
        case ABE_BOTTOM:
            // We want to minimize to the right of the taskbar
            lprect->top    =;
            lprect->bottom = appBarData.rc.bottom;
            lprect->left   = appBarData.rc.right-100;
            lprect->right  = appBarData.rc.right-16;
    // Blimey, we really aren't in luck. It's possible that a third party shell
    // is running instead of explorer. This shell might provide support for the
    // system tray, by providing a Shell_TrayWnd window (which receives the
    // messages for the icons) So, look for a Shell_TrayWnd window and work out
    // the rect from that. Remember that explorer's taskbar is the Shell_TrayWnd,
    // and stretches either the width or the height of the screen. We can't rely
    // on the 3rd party shell's Shell_TrayWnd doing the same, in fact, we can't
    // rely on it being any size. The best we can do is just blindly use the
    // window rect, perhaps limiting the width and height to, say 150 square.
    // Note that if the 3rd party shell supports the same configuraion as
    // explorer (the icons hosted in NotifyTrayWnd, which is a child window of
    // Shell_TrayWnd), we would already have caught it above
    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;
    // OK. Haven't found a thing. Provide a default rect based on the current work
    // area
    SystemParametersInfo(SPI_GETWORKAREA,0,lprect, 0);
    lprect->left = lprect->right - DEFAULT_RECT_WIDTH;
    lprect->top  = lprect->bottom - DEFAULT_RECT_HEIGHT;


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.
His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.
He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.
Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

QuestionMinor Error PinmemberPsychUK24-Jun-12 22:26 
Questionany code in C# ?? Pinmemberalhambra-eidos21-Jan-11 6:29 
GeneralAdapt window size PinsussAnonymous8-May-03 11:51 
I have an MFC project, I'm just trying to make it so the size is adaptable to different screen resolutions. For example, the program runs correct in a screen of size
1024 by 768, but only parts of it show in a smaller screen.
I have used the following line in my OnMove function, but it's not working.
SystemParametersInfo(SPI_GETWORKAREA,0,lprect, 0);
I would greatly appreciate your help.

QuestionHow to change the size of TrayClockWClass window? PinmemberQiu Guohua24-May-02 3:54 
GeneralMinor bug PinmemberTony Truong16-Apr-02 17:08 
GeneralRe: Minor bug PinmemberThisIsANameOK12-Sep-06 22:40 
QuestionHow to remove icon after the process has been killed? Pinmembermjingli24-Jan-02 13:06 
Generalresizing systray PinmemberC Zeeshan22-Oct-01 11:56 
GeneralRe: resizing systray PinmemberPeter Maria Engeli6-Apr-06 8:30 
QuestionHow can i Minimize to system tray at startup? PinmemberMattias19-Jun-01 4:00 
AnswerRe: How can i Minimize to system tray at startup? PinmemberPeter Maria Engeli6-Apr-06 8:42 
QuestionHow can I display text in systray PinmemberAnonymous17-Apr-01 4:08 
AnswerRe: How can I display text in systray PinmemberC Zeeshan17-Oct-01 6:48 
GeneralRe: How can I display text in systray PinmemberKarthik Murugan8-Feb-05 0:54 
GeneralRe: How can I display text in systray PinmemberThatsAlok10-Jun-05 21:24 
General[Message Deleted] PinsussAlexandre Wendt Shima28-May-00 12:57 
GeneralRe: How to hide another app icon? Pinsussjavier22-Sep-00 0:52 
GeneralRe: How to hide another app icon? PinsussLeo Davidson26-Sep-00 3:49 
GeneralQ: Modifying content of these windows PinsussMichael Krebs12-Jan-00 5:23 
GeneralRe: Q: Modifying content of these windows PinsussChris Maunder2-Mar-00 15:03 
GeneralRe: Q: Modifying content of these windows Pinsusswebguy10-Jul-00 11:35 
GeneralRe: Q: Modifying content of these windows PinsussJeff Connell8-Sep-00 19:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 21 Sep 2000
Article Copyright 2000 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid