• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 719
  • Last Modified:

Adding an icon to the taskbar using Shell_NotifyIcon

Hello,
I'm tyring to add an icon to the taskbar with a menu and all.. I'm using VC++ 6 and created a dialog based project. everything seems fine, the icon appears and stays there when i move the mouse over it, however, i don't seem to be able to track it's callback function down, i created a const
#define MYWM_NOTIFYICON 1000

and was passing that as the callback, i also mapped it:
(were the other functions were mapped)
ON_NOTIFY(MYWM_NOTIFYICON, 1, OnClickDoTest)

and declared the OnClickDoTest functions with afx_msg like so:

afx_msg void CTestDlg::OnClickDoTest(WPARAM wParam, LPARAM lParam)

in the class protected section under the rest of it's kind...

however, no i receive no messages, any help? sample code would greatly help...

and also one more thing, if this succeeds, i would like to get rid of the button that windows puts in the taskbar for my dialog, how is that achievable?!
only giving 100 points, although i think it deserves more, but sorry i'm new here and only got 200 points.
Thanx in advance
0
narcotic030200
Asked:
narcotic030200
  • 9
  • 3
1 Solution
 
WynCommented:
Code below also needed for you purpose:

hIcon = LoadIcon(IDR_MAINFRAME);
NOTIFYICONDATA pnid;
pnid.cbSize = sizeof(NOTIFYICONDATA);
pnid.hWnd = AfxGetMainWnd()->m_hWnd;
pnid.uID = IDR_MAINFRAME;
pnid.uFlags = NIF_ICON;
pnid.hIcon = hIcon;
pnid.uCallbackMessage = MYWM_NOTIFYICON; ;
::Shell_NotifyIcon(NIM_ADD,&pnid);
0
 
WynCommented:

0
 
WynCommented:
Here is all for the menu ,
LRESULT CTestDlg::OnClickDoTest(WPARAM wParam, LPARAM lParam)
{
UINT uID       = (UINT)wParam;
UINT uMouseMsg = (UINT)lParam;

if (uID != ID_TRAYICON) return 0L;

switch(uMouseMsg)
{
case WM_RBUTTONDOWN :
{
CMenu cTaskBarMenu;

if (!cTaskBarMenu.LoadMenu(IDM_TASKBARICON))
return 0L;

CMenu *pcTaskBarMenu = cTaskBar.GetSubMenu(0);

CPoint point;
GetCursorPos(&point);
SetForegroundWindow();
pcTaskBarMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON,point.x, point.y,this);
PostMessage(NULL,0,0);
cTaskBarMenu.DestroyMenu();                                        
}
break;
}
return 0L;
}

===========



In case you use plain c++,here is c++ code for the menu,sometimes You also need use SetForegroundWindow(YourMainWindowHandle) before call TrackPopupMenu and call post(null,0,0):
//suppose you have the window handle.
   int PopMenuRet;
   POINT aPoint;

   PopMenu = CreatePopupMenu();

   AppendMenu(PopMenu,MFT_STRING,IDM_OPTION1,"Option 1");
   AppendMenuPopMenu,MFT_STRING,IDM_OPTION2,"Option 2");
   AppendMenu(PopMenu,MFT_SEPARATOR,0,NULL);
   AppendMenu(PopMenu,MFT_STRING,IDM_OPTION3,"Option 3");
   AppendMenu(PopMenu,MFT_STRING,IDM_OPTION4,"Option 4");
   AppendMenu(PopMenu,MFT_STRING,IDM_OPTION5,"Option 5");
   AppendMenu(PopMenu,MFT_STRING,IDM_OPTION6,"Option 6");
   AppendMenuPopMenu,MFT_STRING,IDM_CANCELOPTION,"Cancel");

   GetCursorPos(&aPoint);

   PopMenuRet = TrackPopupMenu(PopMenu,
                               TPM_RIGHTBUTTON |
       TPM_TOPALIGN |
                               TPM_LEFTALIGN |
                               TPM_RETURNCMD,
                               aPoint.x,
                               aPoint.y,
                               0,
                               hwnd,
                               NULL);
   DestroyMenu(PopMenu);

   switch (PopMenuRet) {
   case IDM_OPTION1:
              /* Do Option 1 Stuff */
              break;
   case IDM_OPTION2:
              /* Do Option 2 Stuff */
              break;
   case IDM_OPTION3:
              /* Do Option 3 Stuff */
              break;
   case IDM_OPTION4:
              /* Do Option 4 Stuff */
              break;
   case IDM_OPTION5:
              /* Do Option 5 Stuff */
              break;
   case IDM_CANCELOPTION:
              /* Do Cancel Stuff */
              break;
   }
   return 0;
}


->if this succeeds, i would like to get rid of the button that windows puts in the taskbar for my dialog
=======================
What do you mean by getting rid of ?
If you want it disppear when you minimize the window,it's easy:

case WM_SIZE:
 if(wparam==SIZE_MINIMIZED)
 ShowWindow(hwnd,SW_HIDE);

this will hide the button in task bar.
Regards
W.Yinan
0
Technology Partners: 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!

 
WynCommented:
->ON_NOTIFY(MYWM_NOTIFYICON, 1, OnClickDoTest)
=======
Try ON_MESSAGE Macro.
0
 
WynCommented:
->ON_NOTIFY(MYWM_NOTIFYICON, 1, OnClickDoTest)
=======
Try ON_MESSAGE Macro.

For a complete sample,try:

http://www.codeguru.com/shell/systemtray.shtml

Regards
W.Yinan
0
 
narcotic030200Author Commented:
Thank you very much, i haven't gone through the whole code in detail, but i think you pointed out what i needed to know...
Thanks
0
 
WynCommented:
P.S:
FYI,the ON_MESSAGE macro should be this way:

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CNiconDlg)
        ....
//}}AFX_MSG_MAP
ON_MESSAGE(MYWM_NOTIFYICON,OnClickDoTest)
END_MESSAGE_MAP()

Final point , the button cannot be gotten rid of if window has show-style,except the tool window.

Help all above have thoroughly solve your problems...


0
 
WynCommented:
->Thank you very much, i haven't gone through the whole code in detail, but i think you pointed out what i needed to know...
Thanks
===========

if you dont care alot.I think you can simple change the ON_NOTIFY to ON_MESSAGE , the later is the correct one for user-defined message.

Regards
W.Yinan
0
 
WynCommented:
->Thank you very much, i haven't gone through the whole code in detail, but i think you pointed out what i needed to know...
Thanks
===========

if you dont care alot.I think you can simple change the ON_NOTIFY to ON_MESSAGE , the later is the correct one for user-defined message.

plz give me a feedback if you read the last two comments.
Regards
W.Yinan
0
 
narcotic030200Author Commented:
I will run the code you gave me tomorrow, it's 2:30am here now and i'm Very tired, i'll let you know what happens...
Thanks again
0
 
narcotic030200Author Commented:
Ok, all i had to do is change the ON_NOTIFY to ON_MESSAGE and it worked like a charm :)
However, the problem of having the normal button in the taskbar is still there, i don't know if you get what i meant by deleting it, what i'm aiming to do is an application that is accesed through the icon in the taskbar online and not it's button.... in VB i usually set the ShowInTaskBar value to FALSE, i can't figure out how to do it in VC++... shouldn't be too hard i guess...
0
 
WynCommented:
Again,if you wanna the button hidden if your window is minimized.Using way above and the button cannot be gotten rid of if window has show-style,except the tool window.
If I dont help you alot.I suggest you post another question(50pts enough for this) and let another expert help you.
Sincerely
W.Yinan
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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