added a PPPC class to abstract some functionality

git-svn-id: http://apu.dw.local/svnusers/JoshHoltrop/pppc/trunk@48 8131a0b2-b21c-1c47-bd6a-f003126495bd
This commit is contained in:
joshholtrop 2009-08-19 15:25:53 +00:00
parent cfb6dd739b
commit 05ff66a313
2 changed files with 87 additions and 65 deletions

136
pppc.cc
View File

@ -8,6 +8,7 @@
#include "pport.h" #include "pport.h"
#include "resources.h" #include "resources.h"
#include "parseCmdLine.h" #include "parseCmdLine.h"
#include "pppc.h"
using namespace std; using namespace std;
#define APP_NAME "Parallel Port Power Controller" #define APP_NAME "Parallel Port Power Controller"
@ -16,8 +17,6 @@ using namespace std;
LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void addMenuItem(HMENU hMenu, CHAR * itemstr, WORD id); void addMenuItem(HMENU hMenu, CHAR * itemstr, WORD id);
static HINSTANCE m_hInstance;
int WinMain( int WinMain(
HINSTANCE hInstance, HINSTANCE hInstance,
HINSTANCE hPrevInstance, HINSTANCE hPrevInstance,
@ -26,64 +25,7 @@ int WinMain(
{ {
vector<string> args = parseCmdLine(lpCmdLine); vector<string> args = parseCmdLine(lpCmdLine);
m_hInstance = hInstance; PPPC myPPPC(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; BOOL bRet;
MSG msg; MSG msg;
@ -128,11 +70,6 @@ int WinMain(
} }
} }
Shell_NotifyIcon(NIM_DELETE, &nid);
/* stop listening for session notifications */
session_notification_disable(hWnd);
return bRet; return bRet;
} }
@ -187,3 +124,72 @@ void addMenuItem(HMENU hMenu, CHAR * itemstr, WORD id)
cerr << "Warning: InsertMenuItem(): " << GetLastError() << endl; 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);
}

16
pppc.h Normal file
View File

@ -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