Solved

Tray Icon Changer

Posted on 2006-07-19
12
509 Views
Last Modified: 2013-11-20
im having a problem in changing the icon in the system tray, i have my code i dont know whats wrong. can you help me debug my code.

void CDownloader::DownloadFromSettings()
{
      pTheApp->m_bIconChanging = true;
      CWinThread *thrIconChanger = AfxBeginThread(SystrayDwnlding, NULL);
....
...
...
      pTheApp->m_bIconChanging = false;
}

UINT CDownloader::SystrayDwnlding(LPVOID pParam)
{
      //AfxMessageBox("Entering CDownloader::SystrayDwnlding");
      CSampleApp *pTheApp = (CSampleApp*)AfxGetApp();
      CSampleDlg *pMainDlg = (CSampleDlg*)pTheApp->GetMainWnd();
      bool bDefaultIcon = true;
      pTheApp->m_bIcnChngerRunning = true;
      while(pTheApp->m_bIconChanging == true){
            if(bDefaultIcon){
                  if(pTheApp->GetEndFlag() == false){
                        pMainDlg->TrayMessage(NIM_MODIFY, IDI_TRAYICON21);
                        bDefaultIcon = false;
                  }
            }
            else{
                  if(pTheApp->GetEndFlag() == false){
                        pMainDlg->TrayMessage(NIM_MODIFY, IDI_TRAYICON1);
                        bDefaultIcon = true;
                  }
            }
            Sleep(500);
      }// end of while
      if(pTheApp->GetEndFlag() == false){
            pMainDlg->TrayMessage(NIM_MODIFY, IDI_TRAYICON1);
      }
      pTheApp->m_bIcnChngerRunning = false;
      //AfxMessageBox("Exiting CDownloader::SystrayDwnlding");
      return 0;
}

BOOL CSampleDlg::TrayMessage(DWORD dwMessage, int iResId)
{
    NOTIFYICONDATA tnd;
      ZeroMemory(&tnd, sizeof(NOTIFYICONDATA));
    tnd.cbSize = sizeof(NOTIFYICONDATA);
    tnd.hWnd = this->GetSafeHwnd();
    tnd.uID = iResId;
    tnd.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
    tnd.uCallbackMessage = MYWM_NOTIFYICON;
    tnd.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
      VERIFY( tnd.hIcon = LoadIcon(AfxGetInstanceHandle(),
            MAKEINTRESOURCE(iResId)));
    lstrcpyn(tnd.szTip, (LPCTSTR)sTip, sizeof(tnd.szTip));

    return Shell_NotifyIcon(dwMessage, &tnd);
}
0
Comment
Question by:haroldxious
  • 6
  • 6
12 Comments
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17136572
I think you should not change the resource-id while modifying tray icon while assigning to NOTIFYICONDATA struct..keep the id same and try to change ICON.

ie. try to remove 'tnd.uID = iResId;' from TrayMessage function and just let it load icon from iRedID usign LoadIcon()

-MAHESH
0
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 100 total points
ID: 17136612
you need to assign same resource id to tnd.uID while modifying tray icon..so while NIF_MODIFY assign previous res-id to uID and in tnd.hIcon load whichever icon you need to load using LoadIcon..

You may use function like :

BOOL TrayMessage(HWND hWnd, DWORD dwMessage, UINT uID, HICON hIcon, PSTR pszTip)
{
 BOOL res;

   NOTIFYICONDATA tnd;

   tnd.cbSize = sizeof(NOTIFYICONDATA);
   tnd.hWnd   = hWnd;
   tnd.uID    = uID;

  tnd.uFlags           = NIF_MESSAGE | NIF_ICON | NIF_TIP;
   tnd.uCallbackMessage = WM_NOTIFYTRAYICON;
   tnd.hIcon            = hIcon;

   if (pszTip)
    lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
   else
    tnd.szTip[0] = '\0';

   res = Shell_NotifyIcon(dwMessage, &tnd);

   if (hIcon)
    DestroyIcon(hIcon);
 
  return res;
}

Now
if you want to ADD ICON use like:
TrayMessage(hwnd, NIM_ADD, IDC_TRAYICON, LoadIcon(hInst, MAKEINTRESOURCE(IDI_ADDICON)), APPLICATION_NAME);

if you want to MODIFY ICON use like:
TrayMessage(hwnd, NIM_MODIFY, IDC_TRAYICON, LoadIcon(hInst, MAKEINTRESOURCE(IDI_MODIFYICON)), APPLICATION_NAME);


-MAHESH
0
 
LVL 1

Author Comment

by:haroldxious
ID: 17138489
what will be the value of APPLICATION_NAME? do i need this parameter?
0
 
LVL 1

Author Comment

by:haroldxious
ID: 17138543
and what will be the value of my hWnd?
0
 
LVL 1

Author Comment

by:haroldxious
ID: 17138720
im just now having a problem on how to get the hWnd of my application. i remove the APPLICATION_NAME i guess that is only for the tool tip.

   NOTIFYICONDATA tnd;

   tnd.cbSize = sizeof(NOTIFYICONDATA);
   tnd.hWnd   = hWnd; -> will i specify a value here?
   tnd.uID    = uID; -> will i specify a value here?

  tnd.uFlags           = NIF_MESSAGE | NIF_ICON | NIF_TIP;
   tnd.uCallbackMessage = WM_NOTIFYTRAYICON;
   tnd.hIcon            = hIcon; -> will i specify a value here?


 
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17138895
just assign  m_hWnd if its MFC application

tnd.hWnd   = m_hWnd;

-MAHESH
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:mahesh1402
ID: 17138938
and APPLICATION_NAME is string you need to display for your system tray as tooltip it may be application name or whatever you wish to set as tooltip

-MAHESH
0
 
LVL 1

Author Comment

by:haroldxious
ID: 17143202
i cant still animate the icon.

i havnt use the hWnd beacuse im having many errors ive used this..

BOOL CSampleDlg::TrayMessage(DWORD dwMessage, int iResId, HICON hIcon)
{
    NOTIFYICONDATA tnd;
      ZeroMemory(&tnd, sizeof(NOTIFYICONDATA));
    tnd.cbSize = sizeof(NOTIFYICONDATA);
    tnd.hWnd = this->GetSafeHwnd();
    tnd.uID = iResId;
    tnd.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
    tnd.uCallbackMessage = MYWM_NOTIFYICON;
    tnd.hIcon = hIcon;

    lstrcpyn(tnd.szTip, (LPCTSTR)sTip, sizeof(tnd.szTip));
    return Shell_NotifyIcon(dwMessage, &tnd);
}

TrayMessage(NIM_MODIFY, IDI_TRAYICON1, LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_TRAYICON1)));

still the icon is not changeing....
my application is a download manager, i want that if my application is downloading it will interchange icons from icon1 to icon2 er until the download ends.

Im sorry if i cant understand some of your tips i guess i just need more help.
thank you very much.
0
 
LVL 1

Author Comment

by:haroldxious
ID: 17143269
why is it that when i use

TrayMessage(hWnd, NIM_MODIFY, IDI_TRAYICON1, LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_TRAYICON1)));

hWnd is undeclared identifier?
0
 
LVL 1

Author Comment

by:haroldxious
ID: 17143347
mahesh ive tried it already ive added the HWND but still no ican change occur

     CSampleApp *pTheApp = (CSampleApp*)AfxGetApp();
     CSampleDlg *pMainDlg = (CSampleDlg*)pTheApp->GetMainWnd();

pMainDlg->TrayMessage(pMainDlg->m_hWnd, NIM_MODIFY, IDI_TRAYICON1, LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_TRAYICON1)));

is there anything wrong here?
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17143606
what resource-id of your icon previously ie before your modify it ?
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17143809
As I said Previously while modifying tray icon just load icon dont change resource-id of icon..pass it whatever it was previously while adding to tray...

pMainDlg->TrayMessage(pMainDlg->m_hWnd, NIM_MODIFY, IDI_ADDICON, LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_TRAYICON1)));

-MAHESH
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Process filename extension 3 172
sumNumber challenge 16 110
matchUp  challenge 6 53
how to split multiple lines delimiter : 8 79
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now