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("You
rTrayCallb
ackMessage
");
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(N
ULL), 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_BRUS
H);
// 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(N
ULL), MAKEINTRESOURCE(MYICON));
strcpy(m_NotifyIconData.sz
Tip, "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_MESSA
GE;
m_NotifyIconData.uCallback
Message = 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_DELET
E, &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;
}