Solved

Adding icon to tray... again

Posted on 1998-09-04
9
265 Views
Last Modified: 2010-04-02
Hi everyone... I tried and failed. I have made a simple dialog based MFC application. I added a control to the dialog to place an icon in the tray. If the icon is clicked, I want the dialog to reappear and the icon in the tray to disappear. What is wrong with this short cut...? How do I fix it and allow myself to click it closed and have a caption when the mouse passes over it? Thanks

if (Tray == 1)
{
ShowWindow(SW_HIDE);
m_NotifyIconData.cbSize = sizeof(m_NotifyIconData);
m_NotifyIconData.uFlags = NIF_ICON;
m_NotifyIconData.hIcon = m_hIcon;
uCallbackMessage;
m_NotifyIconData.szTip, "Dialog Application";
Shell_NotifyIcon(NIM_ADD,&m_NotifyIconData);
do
{
}
while (LOWORD(lParam) == WM_LBUTTONDBLCLK);
m_NotifyIconData.uFlags = 0;
Shell_NotifyIcon(NIM_DELETE,&m_NotifyIconData);
ShowWindow(SW_SHOW);
}
0
Comment
Question by:rose337
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1172029
What are you doing with
do
{
}
while (LOWORD(lParam) == WM_LBUTTONDBLCLK);
?
0
 

Author Comment

by:rose337
ID: 1172030
I am doing nothing with it... it mst just loop until left button is hit...
0
 
LVL 6

Accepted Solution

by:
WxW earned 70 total points
ID: 1172031
This is the correct method to "wait" for a message caused by an icon in the system tray

#define MESSAGE_CALLBACK (WM_USER + 4)
NOTIFYICONDATA n;

      nn.cbSize = sizeof(nn);
      nn.hWnd = HWindow; // The handle of the window to post the message
      nn.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
      nn.hIcon = hIconMain; // The handle of the icon
      nn.uCallbackMessage = MESSAGE_CALLBACK;
   lstrcpy(nn.szTip,"Your Tip Here");
      Shell_NotifyIcon(NIM_ADD,&nn);

Then your WindowProc should catch the MESSAGE_CALLBACK as follows

      switch(message)
            {
            case MESSAGE_CALLBACK:
                  {
                  if (lParam == WM_LBUTTONDBLCLK)
                        {
                        Shell_NotifyIcon(NIM_DELETE,&nn);
                        }
       return 0;
                  }

For example I did a NIM_DELETE , if a Double Click occurs . You can specify any other mouse message || action .
0
 
LVL 6

Expert Comment

by:WxW
ID: 1172032
Sorry it is NOTIFYICONDATA nn; and not n
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:alexo
ID: 1172033
It is better to use WM_APP than WM_USER.
0
 
LVL 6

Expert Comment

by:WxW
ID: 1172034
Yeah another mistake of mine...
0
 
LVL 11

Expert Comment

by:alexo
ID: 1172035
>> Yeah another mistake of mine...
Stop worrying.  You're doing fine.
0
 

Author Comment

by:rose337
ID: 1172036
Thanks WxW... I have tried it and I am lost with an error...
firstly, what message should I place in switch to make sure that it will accept my mouse click and does not run through this segment of code and never return to accept the click...
secondly, the complier does not like the line
case MESSAGE_CALLBACK:
it gives errors of no closing bracket or semicolons, etc.
and then when I work around that, an icon appears but does not accept the click to disappear.
I now have seven of the same icons in my tray with no way to close them ;-)
By the way, the way the program is structured is that I have a dialog, then the user hits a property sheet button and the option for a tray icon is there. If he selects it, and closes the property sheet, the dialog disapears and the icon should be in the tray. All icon manipulation (placing and removing it) occurs in the OnProperties segment that is executed when the property sheet is closed.
Thanks for any help that you can give...
0
 
LVL 6

Expert Comment

by:WxW
ID: 1172037
you need to check for example WM_LBUTTONDOWN for a left mouse click .  I used DLBCLICK to check for a double click .

You have possibly defined MESSAGE_CALLBACK in that way :

#define MESSAGE_CALLBACK WM_APP + 4;

remove the ; from the end

You can create the icon tray then by using some property like ON CLOSE or OR DESTROY . If you use plain C++ , just handle the WM_CLOSE message .

The handling of MESSAGE_CALLBACK is your work . If you need just to remove the icon tray , use the Shell_NotifyIcon to remove it . You may want also to reshow the dialog box ... in that case you need to handle WM_CLOSE and just hide ( but not destroy ) the dialog . Then withing the MESSAGE_CALLBACK handle you just reshow the dialog with ShowWindow() .



0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 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

18 Experts available now in Live!

Get 1:1 Help Now