Solved

Adding an icon to the taskbar using Shell_NotifyIcon

Posted on 2000-03-02
12
706 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
[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
  • 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
Independent Software Vendors: 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 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
 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

730 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