/* The original version of this program can be found at http://damb.dk */
#define WINVER  0x0500
#define _WIN32_IE 0x0500
#include <windows.h>
#include <string.h>
#include <stdlib.h>

#define TRAY_MESSAGE (WM_APP + 123)
#define IDC_BUTTON1 1001
#define IDM_TIMER   1002

HINSTANCE InstanceHandle;
void Minimize(HWND hwndDlg);
void UnMinimize(HWND hwndDlg);
void SetBaloonTip(HWND hwndDlg);
HICON HIcon;

LRESULT CALLBACK DialogProc(HWND hwndDlg,
                            UINT msg,
                            WPARAM wParam,
                            LPARAM lParam)
{
   switch(msg)
   {
   case WM_CREATE:
      CreateWindow("BUTTON",
                   "Push Me",
                   WS_CHILD | WS_VISIBLE,
                   5, 5, 100, 24,
                   hwndDlg,
                   (HMENU)(IDC_BUTTON1),
                   InstanceHandle,
                   0);
      break;
   case TRAY_MESSAGE:
      if(lParam == WM_LBUTTONDOWN)
      {
         UnMinimize(hwndDlg);
      }
      break;
   case WM_TIMER:
      KillTimer(hwndDlg, IDM_TIMER);
      SetBaloonTip(hwndDlg);
      break;
   case WM_COMMAND:
      if(HIWORD(wParam) == BN_CLICKED &&
         LOWORD(wParam) == IDC_BUTTON1)
      {
         Minimize(hwndDlg);
      }
      break;
   }
   return DefWindowProc(hwndDlg, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
{
   InstanceHandle = hInstance;
   HIcon = LoadIcon(0, IDI_WINLOGO);
   // HIcon = (HICON )LoadImage(0, "myicon.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
   WNDCLASS wc;
   memset(&wc, 0, sizeof(WNDCLASS));
   wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
   wc.lpfnWndProc = DialogProc;
   wc.hInstance = InstanceHandle;
   wc.hbrBackground = (HBRUSH )(COLOR_BTNFACE + 1);
   wc.lpszClassName = "WhateverClass";
   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
   if(!RegisterClass(&wc))
      return FALSE;
   HWND WindowHandle = CreateWindow("WhateverClass",
                                    "Whatever", // Caption text
                                    WS_VISIBLE | WS_CLIPSIBLINGS |
                                    WS_CLIPCHILDREN | WS_CAPTION |
                                    WS_BORDER | WS_SYSMENU,
                                    100, 100, 110, 70,  // Position
                                    0,
                                    0,
                                    InstanceHandle,
                                    0);
   MSG Msg;
   while(GetMessage(&Msg, WindowHandle, 0, 0xFFFF) > 0)
   {
      if(!IsDialogMessage(WindowHandle, &Msg))
      {
         TranslateMessage(&Msg);
         DispatchMessage(&Msg);
      }
   }
   return 0;
}

void Minimize(HWND hwndDlg)
{
   NOTIFYICONDATA NotifyIconData;
   ShowWindow(hwndDlg, SW_HIDE);
   memset(&NotifyIconData, 0, sizeof(NotifyIconData));
   NotifyIconData.cbSize = sizeof(NotifyIconData);
   NotifyIconData.uID = 123;
   NotifyIconData.hWnd = hwndDlg;
   NotifyIconData.hIcon = HIcon;
   NotifyIconData.uFlags = NIF_ICON | NIF_MESSAGE;
   NotifyIconData.uCallbackMessage = WM_APP + 123;
   strcpy(NotifyIconData.szTip, "TrayTip");
   Shell_NotifyIcon(NIM_ADD, &NotifyIconData);
   SetTimer(hwndDlg, IDM_TIMER, 2000, 0);
}

void UnMinimize(HWND hwndDlg)
{
   NOTIFYICONDATA NotifyIconData;
   ShowWindow(hwndDlg, SW_SHOW);
   memset(&NotifyIconData, 0, sizeof(NotifyIconData));
   NotifyIconData.cbSize = sizeof(NotifyIconData);
   NotifyIconData.uID = 123;
   NotifyIconData.hWnd = hwndDlg;
   NotifyIconData.hIcon = HIcon;
   NotifyIconData.uFlags = NIF_ICON | NIF_MESSAGE;
   NotifyIconData.uCallbackMessage = TRAY_MESSAGE;
   strcpy(NotifyIconData.szTip, "TrayTip");
   Shell_NotifyIcon(NIM_DELETE, &NotifyIconData);
}

void SetBaloonTip(HWND hwndDlg)
{
   NOTIFYICONDATA  NotifyIconData;
   memset(&NotifyIconData, 0, sizeof(NOTIFYICONDATA));
   NotifyIconData.cbSize = sizeof(NOTIFYICONDATA);
   NotifyIconData.uID = 123;
   NotifyIconData.hWnd = hwndDlg;
   NotifyIconData.hIcon = HIcon;
   NotifyIconData.uFlags = 0x10;
   NotifyIconData.uTimeout = 15000;
   NotifyIconData.uCallbackMessage = TRAY_MESSAGE;
   strcpy(NotifyIconData.szInfoTitle, "BaloonTitle");
   strcpy(NotifyIconData.szInfo, "BalloonInfo");
   strcpy(NotifyIconData.szTip, "TrayTip");
   Shell_NotifyIcon(NIM_MODIFY, (NOTIFYICONDATA *)&NotifyIconData);
}
//--- EOF