Advertisement

10.22.2002 at 11:43AM PDT, ID: 20379355
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

How to minimize a window to the system tray?

Asked by rustycp in C++ Programming Language

Tags: , ,

I am trying to build a simple window which, when minimized, will become an icon on the system tray, then can be activated again by clicking on that icon.  So far, using examples I have found, I have built a window that has an icon on the system tray, but the icon just disappears when I mouse over it.  I've made no progress on minimizing or activating. I am getting pretty stuck and not getting far :(   Any help is greatly appreciated..!

Here is the code for this simple window that I've been able to put together so far:


#include <windows.h>

#define MYICON 500


/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";

// FOR ADDING THE ICON TO THE STATUS BAR
NOTIFYICONDATA m_NotifyIconData;

// FOR NOTIFYING WINDOW FROM ICON
static UINT NEAR WM_YOURTRAYCALLBACKMESSAGE = RegisterWindowMessage("YourTrayCallbackMessage");


int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)

{
    HWND hwnd;
    MSG messages;
    WNDCLASSEX wincl;

    // THE WINDOW
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof(WNDCLASSEX);
    // USE MY OWN ICON
    wincl.hIcon  = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(MYICON));
    wincl.hIconSm = NULL;

    wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL; /* No menu */
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);

      // THIS ADDS THE ICON TO THE STATUS BAR
      m_NotifyIconData.cbSize = sizeof m_NotifyIconData;
      m_NotifyIconData.hWnd = hwnd;
      m_NotifyIconData.uID = 1;
      m_NotifyIconData.uFlags = NIF_ICON|NIF_TIP;
      m_NotifyIconData.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(MYICON));
      strcpy(m_NotifyIconData.szTip, "My Program");
      Shell_NotifyIcon(NIM_ADD, &m_NotifyIconData);

      // THIS IS SUPPOSED TO LET THE ICON OPEN THE WINDOW BACK UP
      m_NotifyIconData.uFlags = NIF_ICON|NIF_TIP|NIF_MESSAGE;
      m_NotifyIconData.uCallbackMessage = WM_YOURTRAYCALLBACKMESSAGE;

    // REGISTER WINDOW CLASS
    if(!RegisterClassEx(&wincl)) return 0;

    // CREATE PROGRAM
    hwnd = CreateWindowEx(
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Rustys Program",         /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow(hwnd, nFunsterStil);


    /* Run the message loop. It will run until GetMessage( ) returns 0 */
    while(GetMessage(&messages, NULL, 0, 0))
    {
           /* Translate virtual-key messages into character messages */
           TranslateMessage(&messages);

           /* Send message to WindowProcedure */
           DispatchMessage(&messages);
    }

    return messages.wParam;
}

/* This function is called by the Windows function DispatchMessage( ) */
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
      case WM_DESTROY:

            // this is supposed to take the icon out of the system tray when the window is closed
            m_NotifyIconData.uFlags = 0;
            Shell_NotifyIcon(NIM_DELETE, &m_NotifyIconData);

            PostQuitMessage(0);        /* send a WM_QUIT to the message queue */
            break;
      default:                   /* for messages that we don't deal with */
            return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}


Start Free Trial
 
Loading Advertisement...
 
[+][-]10.22.2002 at 12:51PM PDT, ID: 7357647

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C++ Programming Language
Tags: minimize, shell_notifyicon, bar
Sign Up Now!
Solution Provided By: job_s
Participating Experts: 2
Solution Grade: A
 
 
[+][-]10.22.2002 at 12:59PM PDT, ID: 7357679

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.23.2002 at 07:51AM PDT, ID: 7360813

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.23.2002 at 07:56AM PDT, ID: 7360844

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-44