Solved

Finding the screen position of a system tray icon

Posted on 2001-07-10
2
319 Views
Last Modified: 2013-11-20
hi gurus,

i have a system tray icon and would like to display a window at or around it. Is there any way i can get the location or dimensions of my tray icon?

thanks,
Gavin
0
Comment
Question by:mrpinko
2 Comments
 
LVL 2

Expert Comment

by:vbk_bgm
ID: 6272500
Hi,
The sys tray is a child window of the task bar. Hence you can obtain the rect in the foll way.
HWND hWndTask = FindWindow(_T("Shell_TrayWnd"), _T(""));
HWND hWndTray = NULL;
RECT rect;
for(HWND hWnd = GetWindow(hWndTask,GW_CHILD); hWnd != NULL; hWnd = GetWindow(hWnd,GW_HWNDNEXT))
    {
         TCHAR ClassName[MAX_PATH];
         if (GetClassName(hWnd,ClassName,MAX_PATH))
         {
              if (0 == lstrcmp(_T  ("TrayNotifyWnd"),ClassName))
              {
                   hWndTray = hWnd;
                   break;
              }
         }
    }
   
//get the rect in screen coordinates    
GetWindowRect(hWndTray,&rect);
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 100 total points
ID: 6272518
static VOID GetTrayWndRect(LPRECT lpTrayRect)
{
  // First, we'll use a quick hack method. We know that the taskbar is a window
  // of class Shell_TrayWnd, and the status tray is a child of this of class
  // TrayNotifyWnd. This provides us a window rect to minimize to. Note, however,
  // that this is not guaranteed to work on future versions of the shell. If we
  // use this method, make sure we have a backup!
  HWND hShellTrayWnd=FindWindowEx(NULL,NULL,TEXT("Shell_TrayWnd"),NULL);
  if(hShellTrayWnd)
  {
    HWND hTrayNotifyWnd=FindWindowEx(hShellTrayWnd,NULL,TEXT("TrayNotifyWnd"),NULL);
    if(hTrayNotifyWnd)
    {
      GetWindowRect(hTrayNotifyWnd,lpTrayRect);
      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
     lpTrayRect->top=appBarData.rc.bottom-100;
     lpTrayRect->bottom=appBarData.rc.bottom-16;
     lpTrayRect->left=appBarData.rc.left;
     lpTrayRect->right=appBarData.rc.right;
     break;

      case ABE_TOP:
      case ABE_BOTTOM:
     // We want to minimize to the right of the taskbar
     lpTrayRect->top=appBarData.rc.top;
     lpTrayRect->bottom=appBarData.rc.bottom;
     lpTrayRect->left=appBarData.rc.right-100;
     lpTrayRect->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
  hShellTrayWnd=FindWindowEx(NULL,NULL,TEXT("Shell_TrayWnd"),NULL);
  if(hShellTrayWnd)
  {
    GetWindowRect(hShellTrayWnd,lpTrayRect);
    if(lpTrayRect->right-lpTrayRect->left>DEFAULT_RECT_WIDTH)
      lpTrayRect->left=lpTrayRect->right-DEFAULT_RECT_WIDTH;
    if(lpTrayRect->bottom-lpTrayRect->top>DEFAULT_RECT_HEIGHT)
      lpTrayRect->top=lpTrayRect->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,lpTrayRect,0);
  lpTrayRect->left=lpTrayRect->right-DEFAULT_RECT_WIDTH;
  lpTrayRect->top=lpTrayRect->bottom-DEFAULT_RECT_HEIGHT;
}

good luck
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
method notes when mouse over in eclipse 5 86
lucky13 challenge 11 142
matchUp  challenge 9 105
Problem to open Excel file 15 130
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question