Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / MFC
Article

Finding the position and dimensions of the Windows system tray

Rate me:
Please Sign up or sign in to vote.
4.80/5 (9 votes)
20 Sep 2000CPOL 170.3K   45   23
A simple method to get the coordinates of the system tray

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)
{
    // 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;
        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;
    }
    // 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;
    appBarData.cbSize=sizeof(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
        switch(appBarData.uEdge)
        {
        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;
            break;
            
        case ABE_TOP:
        case ABE_BOTTOM:
            // We want to minimize to the right of the taskbar
            lprect->top    = appBarData.rc.top;
            lprect->bottom = appBarData.rc.bottom;
            lprect->left   = appBarData.rc.right-100;
            lprect->right  = appBarData.rc.right-16;
            break;
        }
        return;
    }
    
    // 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;
        
        return;
    }
    
    // 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;
}

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.

License

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


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, 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. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
QuestionMinor Error Pin
PsychUK24-Jun-12 21:26
PsychUK24-Jun-12 21:26 
Questionany code in C# ?? Pin
kiquenet.com21-Jan-11 5:29
professionalkiquenet.com21-Jan-11 5:29 
GeneralAdapt window size Pin
Anonymous8-May-03 10:51
Anonymous8-May-03 10:51 
QuestionHow to change the size of TrayClockWClass window? Pin
24-May-02 2:54
suss24-May-02 2:54 
GeneralMinor bug Pin
Tony Truong16-Apr-02 16:08
Tony Truong16-Apr-02 16:08 
GeneralRe: Minor bug Pin
SonicMouse12-Sep-06 21:40
SonicMouse12-Sep-06 21:40 
QuestionHow to remove icon after the process has been killed? Pin
mjingli24-Jan-02 12:06
mjingli24-Jan-02 12:06 
Generalresizing systray Pin
22-Oct-01 10:56
suss22-Oct-01 10:56 
GeneralRe: resizing systray Pin
Peter Maria Engeli6-Apr-06 7:30
Peter Maria Engeli6-Apr-06 7:30 
QuestionHow can i Minimize to system tray at startup? Pin
19-Jun-01 3:00
suss19-Jun-01 3:00 
I want to minimize my program to the tray at startup if the user selects "start minimized" in the shortcut properties. My current approach for minimizing to the tray is calling ShowWindow(SW_HIDE) in OnSize() (CDialog derived class). Once the program is running, this takes care of all minimize-operations (EVEN the "minimize all windows" feature, which does not send any SYS_COMMAND SC_MINIMIZE at all. That's a problem with other approaches, eg, try it on mIRC, it will minimize to the taskbar). It doesn't have the fancy DrawAnimatedRects() feature, but I can
live with that. The problem is with "start minimized".

At program startup, OnSize() is indeed called with the MINIMIZE parameter, just as it should be when "start minimized" is activated. But instead of hiding the window, the call to ShowWindow(SW_HIDE) just makes it pop up again, restored to normal size, and visible. A click on the
minimize button makes it go away into the tray, but this is what I'd like to get rid of.
Any suggestions/pointers to info?

AnswerRe: How can i Minimize to system tray at startup? Pin
Peter Maria Engeli6-Apr-06 7:42
Peter Maria Engeli6-Apr-06 7:42 
QuestionHow can I display text in systray Pin
17-Apr-01 3:08
suss17-Apr-01 3:08 
AnswerRe: How can I display text in systray Pin
17-Oct-01 5:48
suss17-Oct-01 5:48 
GeneralRe: How can I display text in systray Pin
Karthik Murugan7-Feb-05 23:54
Karthik Murugan7-Feb-05 23:54 
GeneralRe: How can I display text in systray Pin
ThatsAlok10-Jun-05 20:24
ThatsAlok10-Jun-05 20:24 
General[Message Deleted] Pin
Alexandre Wendt Shima28-May-00 11:57
Alexandre Wendt Shima28-May-00 11:57 
GeneralRe: How to hide another app icon? Pin
Javier21-Sep-00 23:52
Javier21-Sep-00 23:52 
GeneralRe: How to hide another app icon? Pin
Leo Davidson26-Sep-00 2:49
Leo Davidson26-Sep-00 2:49 
GeneralMessage Closed Pin
12-Jan-00 4:23
Michael Krebs12-Jan-00 4:23 
GeneralRe: Q: Modifying content of these windows Pin
Chris Maunder2-Mar-00 14:03
cofounderChris Maunder2-Mar-00 14:03 
GeneralRe: Q: Modifying content of these windows Pin
webguy10-Jul-00 10:35
webguy10-Jul-00 10:35 
GeneralRe: Q: Modifying content of these windows Pin
IceKarma8-Sep-00 18:50
IceKarma8-Sep-00 18:50 

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

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