Solved

Adding an icon to the taskbar using Shell_NotifyIcon

Posted on 2000-03-02
12
699 Views
Last Modified: 2012-05-05
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
Comment
Question by:narcotic030200
  • 9
  • 3
12 Comments
 
LVL 5

Accepted Solution

by:
Wyn earned 100 total points
ID: 2579104
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
 
LVL 5

Expert Comment

by:Wyn
ID: 2579115

0
 
LVL 5

Expert Comment

by:Wyn
ID: 2579127
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
 
LVL 5

Expert Comment

by:Wyn
ID: 2579133
->ON_NOTIFY(MYWM_NOTIFYICON, 1, OnClickDoTest)
=======
Try ON_MESSAGE Macro.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2579137
->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
 
LVL 1

Author Comment

by:narcotic030200
ID: 2579140
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:Wyn
ID: 2579146
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
 
LVL 5

Expert Comment

by:Wyn
ID: 2579148
->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
 
LVL 5

Expert Comment

by:Wyn
ID: 2579149
->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
 
LVL 1

Author Comment

by:narcotic030200
ID: 2579152
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
 
LVL 1

Author Comment

by:narcotic030200
ID: 2580482
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
 
LVL 5

Expert Comment

by:Wyn
ID: 2580723
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

920 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

12 Experts available now in Live!

Get 1:1 Help Now