?
Solved

Tray Icon Changer

Posted on 2006-07-19
12
Medium Priority
?
556 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

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 …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses

752 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