Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 602
  • Last Modified:

Tray Icon Changer

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
haroldxious
Asked:
haroldxious
  • 6
  • 6
1 Solution
 
mahesh1402Commented:
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
 
mahesh1402Commented:
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
 
haroldxiousAuthor Commented:
what will be the value of APPLICATION_NAME? do i need this parameter?
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
haroldxiousAuthor Commented:
and what will be the value of my hWnd?
0
 
haroldxiousAuthor Commented:
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
 
mahesh1402Commented:
just assign  m_hWnd if its MFC application

tnd.hWnd   = m_hWnd;

-MAHESH
0
 
mahesh1402Commented:
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
 
haroldxiousAuthor Commented:
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
 
haroldxiousAuthor Commented:
why is it that when i use

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

hWnd is undeclared identifier?
0
 
haroldxiousAuthor Commented:
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
 
mahesh1402Commented:
what resource-id of your icon previously ie before your modify it ?
0
 
mahesh1402Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now