diff --git a/pppc.cc b/pppc.cc index e279fd9..33e9b97 100644 --- a/pppc.cc +++ b/pppc.cc @@ -8,6 +8,7 @@ #include "pport.h" #include "resources.h" #include "parseCmdLine.h" +#include "pppc.h" using namespace std; #define APP_NAME "Parallel Port Power Controller" @@ -16,8 +17,6 @@ using namespace std; LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); void addMenuItem(HMENU hMenu, CHAR * itemstr, WORD id); -static HINSTANCE m_hInstance; - int WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, @@ -26,64 +25,7 @@ int WinMain( { vector args = parseCmdLine(lpCmdLine); - 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); + PPPC myPPPC(hInstance); BOOL bRet; MSG msg; @@ -128,11 +70,6 @@ int WinMain( } } - Shell_NotifyIcon(NIM_DELETE, &nid); - - /* stop listening for session notifications */ - session_notification_disable(hWnd); - return bRet; } @@ -187,3 +124,72 @@ void addMenuItem(HMENU hMenu, CHAR * itemstr, WORD id) cerr << "Warning: InsertMenuItem(): " << GetLastError() << endl; } } + +PPPC::PPPC(HINSTANCE hInstance, const char * config_file) +{ + /* 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 */ + m_hWnd = ::CreateWindow("TrayIconClass", "", WS_POPUP, + CW_USEDEFAULT,CW_USEDEFAULT, + CW_USEDEFAULT,CW_USEDEFAULT, + NULL, 0, + hInstance, 0); + if (m_hWnd == NULL) + { + cerr << "Warning: CreateWindow(): " << GetLastError() << endl; + } + + /* Create the notify icon data */ + memset(&m_nid, 0, sizeof(m_nid)); + m_nid.cbSize = sizeof(NOTIFYICONDATA); + m_nid.hWnd = m_hWnd; + m_nid.uID = 0; + m_nid.hIcon = icon; + m_nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; + m_nid.uCallbackMessage = WM_ICON_NOTIFY; + strncpy(m_nid.szTip, APP_NAME, sizeof(m_nid.szTip)); + + BOOL bResult = Shell_NotifyIcon(NIM_ADD, &m_nid); + if (bResult == 0) + { + cerr << "Warning: Shell_NotifyIcon(): " << GetLastError() << endl; + } + + /* begin capturing session notifications */ + session_notification_enable(m_hWnd); + +} + +PPPC::~PPPC() +{ + /* delete the tray icon */ + Shell_NotifyIcon(NIM_DELETE, &m_nid); + + /* stop listening for session notifications */ + session_notification_disable(m_hWnd); +} diff --git a/pppc.h b/pppc.h new file mode 100644 index 0000000..4dcc9d9 --- /dev/null +++ b/pppc.h @@ -0,0 +1,16 @@ + +#ifndef PPPC_H +#define PPPC_H + +class PPPC +{ + public: + PPPC(HINSTANCE hInstance, const char * config_file = NULL); + ~PPPC(); + + protected: + HWND m_hWnd; + NOTIFYICONDATA m_nid; +}; + +#endif