Solved

Tray Icon Changer

Posted on 2006-07-19
12
496 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Handling string inputs in C/Linux 23 167
xyBalance chalenge 58 88
Window placement 17 65
Use of condition with 'serial' in ansible 2 36
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

706 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

16 Experts available now in Live!

Get 1:1 Help Now