Solved

Tray Icon Changer

Posted on 2006-07-19
12
546 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

729 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