Code:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
#include <shellapi.h> //<-this is needed for a systray app
#include <string>
using namespace std;
#define ID_TASKBARICON 100 //<-systray app
#define WM_ICONNOTIFY (WM_USER + 101) //<-systray app
HWND hWnd=NULL;
HINSTANCE hInst=NULL;
HANDLE hFile=NULL;
string sFiles[4], sLocalPath, sLocalFile, sServerPath, sServerFile;
bool bBackup;
FILETIME ftC, ftA, ftW; //created, accessed and written file times
SYSTEMTIME stCur, stFile; //current and file system times
DWORD dwLast;
int i;
NOTIFYICONDATA nid; //<-systray app
HWND hBtnMin=NULL, hBtnBkup=NULL, hBtnExit=NULL;
void InitVars();
void MidnightRollover();
bool BackupFiles();
void MainLoop();
LRESULT CALLBACK MsgHndlr(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam)
{
switch(umsg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
}break;
case WM_ICONNOTIFY: //<-systray app msg
{
switch(lparam)
{
case WM_LBUTTONDBLCLK:
{
ShowWindow(hWnd,SW_RESTORE);
}break;
}
return(0);
}break;
case WM_LBUTTONDOWN:
{
BackupFiles();
bBackup=false;
return(0);
}break;
case WM_COMMAND:
{
if((HWND)lparam==hBtnMin)
{
ShowWindow(hWnd,SW_HIDE);
return(0);
}
if((HWND)lparam==hBtnExit)
{
DestroyWindow(hWnd);
return(0);
}
}break;
case WM_CREATE:
{
hBtnMin=CreateWindowEx(0,"Button","Minimize",WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,150,5,100,30,hwnd,NULL,hInst,NULL);
hBtnExit=CreateWindowEx(0,"Button","Exit",WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,260,5,50,30,hwnd,NULL,hInst,NULL);
return(0);
}break;
}
return(DefWindowProc(hwnd,umsg,wparam,lparam));
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hInst=hInstance;
WNDCLASSEX wcx;
wcx.cbSize=sizeof(WNDCLASSEX);
wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcx.lpfnWndProc=MsgHndlr;
wcx.cbClsExtra=0;
wcx.cbWndExtra=0;
wcx.hInstance=hInst;
wcx.hIcon=LoadIcon(NULL, IDI_APPLICATION);
wcx.hCursor=LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wcx.lpszMenuName=NULL;
wcx.lpszClassName="WindowClass";
wcx.hIconSm=NULL;
if(!RegisterClassEx(&wcx)) return(0);
hWnd=CreateWindowEx(0,"WindowClass","Tidewater Billing Backup",WS_CAPTION,
GetSystemMetrics(SM_CXSCREEN)/2-210,GetSystemMetrics(SM_CYSCREEN)/2-40,420,80,
NULL,NULL,hInst,NULL);
if(!hWnd) return(0);
InitVars();
MSG msg;
for(;;)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message==WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
MainLoop();
}
Shell_NotifyIcon(NIM_DELETE,&nid); //<-systray app
return(msg.wParam);
}
void InitVars()
{
sLocalPath="C:\\IMATEBKUP\\";
sServerPath="V:\\IMATEBKUP\\";
sFiles[0]="IMATEBKUP1100";
sFiles[1]="IMATEBKUP1200";
sFiles[2]="IMATEBKUP3100";
sFiles[3]="IMATEBKUP3800";
dwLast=0;
bBackup=false;
GetLocalTime(&stFile);
//systray app ->
nid.cbSize=sizeof(NOTIFYICONDATA);
nid.hWnd=hWnd;
nid.uID=ID_TASKBARICON;
nid.uFlags=NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage=WM_ICONNOTIFY;
nid.hIcon=LoadIcon(NULL,IDI_APPLICATION);
strcpy(nid.szTip,"tooltip");
Shell_NotifyIcon(NIM_ADD,&nid);
//<- systray app
}
void MidnightRollover()
{
GetLocalTime(&stCur);
if((stCur.wHour==0)&&(stCur.wMinute>=30))
{
bBackup=false;
}
}
void MainLoop()
{
if((timeGetTime()-dwLast)>=900000)
{
dwLast=timeGetTime();
MidnightRollover();
if(!bBackup)
{
GetLocalTime(&stCur);
if((stCur.wHour==20)&&(stCur.wMinute>=5))
{
if(BackupFiles()) bBackup=true;
}
}
}
}
bool BackupFiles()
{
for(i=0;i<4;i++)
{
sLocalFile=sLocalPath;
sLocalFile+=sFiles[i];
sServerFile=sServerPath;
sServerFile+=sFiles[i];
CopyFile(sLocalFile.c_str(),sServerFile.c_str(),0);
}
return(true);
}