#include #include #include #include "session_notification.h" #include "pport.h" #include "resources.h" using namespace std; #define APP_NAME "Parallel Port Power Controller" #define WM_ICON_NOTIFY (WM_APP + 42) LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); static HINSTANCE m_hInstance; int WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { m_hInstance = hInstance; /* Load the application icon */ HICON icon = ::LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)); if (icon == 0) { cerr << "Warning: LoadIcon(): " << GetLastError() << endl; } /* Create the TrayIconClass class */ WNDCLASSEX wcex; memset(&wcex, 0, sizeof(wcex)); wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; wcex.lpfnWndProc = (WNDPROC)WindowProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = icon; wcex.hCursor = 0; wcex.hbrBackground = 0; wcex.lpszMenuName = 0; wcex.lpszClassName = "TrayIconClass"; wcex.hIconSm = 0; RegisterClassEx(&wcex); /* Create the tray icon window */ HWND hWnd = ::CreateWindow("TrayIconClass", "", WS_POPUP, CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, NULL, 0, hInstance, 0); if (hWnd == NULL) { cerr << "Warning: CreateWindow(): " << GetLastError() << endl; } /* Create the notify icon data */ NOTIFYICONDATA nid; memset(&nid, 0, sizeof(nid)); nid.cbSize = sizeof(NOTIFYICONDATA); nid.hWnd = hWnd; nid.uID = 0; nid.hIcon = icon; nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; nid.uCallbackMessage = WM_ICON_NOTIFY; strncpy(nid.szTip, APP_NAME, sizeof(nid.szTip)); BOOL bResult = Shell_NotifyIcon(NIM_ADD, &nid); if (bResult == 0) { cerr << "Warning: Shell_NotifyIcon(): " << GetLastError() << endl; } /* begin capturing session notifications */ session_notification_enable(hWnd); BOOL bRet; MSG msg; bool running = true; while(running) { bRet = GetMessage(&msg, NULL, 0, 0); if (bRet == -1 || bRet == 0) { break; } switch (msg.message) { case WM_PAINT: break; case WM_CLOSE: case WM_DESTROY: case WM_QUIT: running = false; break; case WM_WTSSESSION_CHANGE: if (msg.wParam == WTS_SESSION_LOGON || msg.wParam == WTS_SESSION_UNLOCK) { pport_write(0xFF); } if (msg.wParam == WTS_SESSION_LOGOFF || msg.wParam == WTS_SESSION_LOCK) { pport_write(0x0); } break; case WM_COMMAND: switch (LOWORD(msg.wParam)) /* menu entry index */ { case 0: /* exit */ running = false; break; } break; break; } } Shell_NotifyIcon(NIM_DELETE, &nid); /* stop listening for session notifications */ session_notification_disable(hWnd); return bRet; } LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_NCCREATE: return 1; case WM_CREATE: break; case WM_CLOSE: DestroyWindow(hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; case WM_ICON_NOTIFY: if (wParam == 0) { if (LOWORD(lParam) == WM_RBUTTONUP) { HMENU hMenu = ::CreatePopupMenu(); MENUITEMINFO mii; memset(&mii, 0, sizeof(mii)); mii.cbSize = sizeof(mii); mii.fMask = MIIM_STRING | MIIM_ID; mii.wID = 0; mii.dwTypeData = "E&xit"; mii.cch = strlen(mii.dwTypeData); if (::InsertMenuItem(hMenu, 0, TRUE, &mii) == 0) { cerr << "Warning: InsertMenuItem(): " << GetLastError() << endl; } POINT pos; GetCursorPos(&pos); ::SetForegroundWindow(hWnd); ::TrackPopupMenu(hMenu, 0, pos.x, pos.y, 0, hWnd, NULL); // BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly" ::PostMessage(hWnd, WM_NULL, 0, 0); DestroyMenu(hMenu); } } break; } return 0; }