#include <windows.h>
#include <tchar.h>
#define IDM_TEST 		101
#define IDM_EXIT 		102
#define IDI_SYSTRAYDEMO	103
#define	WM_SHELLICON 	WM_USER + 1

NOTIFYICONDATA nidApp;
HMENU hPopMenu;
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine, int       nCmdShow)
{
    TCHAR szWindowClass[] = _T("Hello-World");

    WNDCLASSEX wcex = {0};

    wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(NULL, IDI_APPLICATION);
	RegisterClassEx(&wcex);

    HWND hWnd = CreateWindow(szWindowClass, szWindowClass, WS_OVERLAPPEDWINDOW,
                             CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
    UpdateWindow(hWnd);
    ShowWindow(hWnd, SW_SHOWNORMAL);
    
    hPopMenu = CreatePopupMenu();
    InsertMenu(hPopMenu, 0xFFFFFFFF, MF_BYPOSITION | MF_STRING, IDM_TEST, _T("Test"));
    InsertMenu(hPopMenu, 0xFFFFFFFF, MF_BYPOSITION | MF_STRING, IDM_EXIT, _T("Exit"));

    nidApp.cbSize = sizeof(NOTIFYICONDATA);
    nidApp.hWnd = (HWND) hWnd;
    nidApp.uID = IDI_SYSTRAYDEMO;
    nidApp.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
    nidApp.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
    nidApp.uCallbackMessage = WM_SHELLICON; 
    strcpy(nidApp.szTip, _T("Just a tip"));
    Shell_NotifyIcon(NIM_ADD, &nidApp); 

	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
	}
    DestroyMenu(hPopMenu);
	return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
	int wmId, wmEvent;
    POINT lpClickPoint;
    RECT rect;
	PAINTSTRUCT ps;
	switch (message) {

	case WM_SHELLICON: 
		switch(LOWORD(lParam)) {   
        case WM_RBUTTONDOWN: 
            SetForegroundWindow(hWnd);
            GetCursorPos(&lpClickPoint);
            TrackPopupMenu(hPopMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_BOTTOMALIGN,
                           lpClickPoint.x, lpClickPoint.y, 0, hWnd, NULL);
            return TRUE; 
		}
		break;
	case WM_COMMAND:
		wmId    = LOWORD(wParam); // wmEvent = HIWORD(wParam);
		switch (wmId) {
        case IDM_TEST:
            MessageBox(NULL,_T("Just a test."),_T("Test"),MB_OK);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        GetClientRect(hWnd, &rect);
        DrawText(hdc, _T("Hello, Wrold."), -1, &rect, DT_CENTER);
        EndPaint(hWnd, &ps);
        break;
	case WM_DESTROY:
        Shell_NotifyIcon(NIM_DELETE,&nidApp);
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
