#include "ShutDown.h"
#include "resource.h"
#define ID_TIMER 0x0101
static SHUTDOWN_SETTING sds;
void Initialize(HWND);
void SaveData2Registry(void);
void UnInstall(void);
static BOOL flHasRegData = FALSE;
extern SHUTDOWN_TIME sdt;//ShutDownLib.c中已声明了。
#define ADDCOMBOTEXT(n, val) SendMessage(GetDlgItem(hwnd, n), CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)(val))
#define GETCOMBOTEXT(val) if (HIWORD(wParam) == LBN_SELCHANGE) \
val = SendMessage((HWND)lParam, CB_GETCURSEL, 0, 0);
#define SETCOMBOSEL(n, val) (int)SendMessage(GetDlgItem(hwnd, n), CB_SETCURSEL, (WPARAM)(int)(val), 0L);
void AddComboBoxText(HWND hwnd)
{
TCHAR szTimeNum[4];
int count, i;
for (count = 0; count < 60; count ++)
{
if (count < 10)
{
wsprintf(szTimeNum, TEXT("0%d"), count);
for (i = 0; i < 3; i++)
{
ADDCOMBOTEXT(IDC_COMBO1 + i, szTimeNum);
}
}
else
{
wsprintf(szTimeNum, TEXT("%d"), count);
if (count < 24)
{
for (i = 0; i < 3; i++)
{
ADDCOMBOTEXT(IDC_COMBO1 + i, szTimeNum);
}
}
else
{
for (i = 1; i < 3; i++)
{
ADDCOMBOTEXT(IDC_COMBO1 + i, szTimeNum);
}
}
}
}
}
void SetNowTime(HWND hwnd)
{
SYSTEMTIME st;
TCHAR time[20];
GetLocalTime(&st);
GetTimeFormat(LOCALE_USER_DEFAULT, TIME_FORCE24HOURFORMAT, &st, TEXT("HH':'mm':'ss'"), time, sizeof(time));
SetWindowText(GetDlgItem(hwnd, IDC_EDIT1), time);
}
LRESULT CALLBACK DlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
typedef BOOL (WINAPI *SLWAFUNC)(HWND, COLORREF, BYTE, DWORD);
SLWAFUNC slwaFunc = NULL;
static SYSTEMTIME st;
TCHAR szCountDownSecs[20];
static UINT uWndId = 0;
switch (message)
{
case WM_INITDIALOG:
//设置对话框为稍微透明...纯粹是好玩...:)
SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) ^ 0x80000);
slwaFunc = (SLWAFUNC)GetProcAddress(GetModuleHandle(L"User32.dll"), "SetLayeredWindowAttributes");
if (slwaFunc) slwaFunc(hwnd, 0, 200, 2);
sdt = QueryShutTime();
SetTimer(hwnd, ID_TIMER, 1000, NULL);
Initialize(hwnd);
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_COMBO1:
GETCOMBOTEXT(sdt.wHour);
break;
case IDC_COMBO2:
GETCOMBOTEXT(sdt.wMinute);
break;
case IDC_COMBO3:
GETCOMBOTEXT(sdt.wSecond)
break;
case IDC_CHECK1:
sds.bAutoRun = IsDlgButtonChecked(hwnd, IDC_CHECK1);
break;
case IDC_CHECK2:
sds.bCantCancel = IsDlgButtonChecked(hwnd, IDC_CHECK2);
break;
case IDC_EDIT2:
SendMessage(GetDlgItem(hwnd, IDC_EDIT2), WM_GETTEXT,
sizeof(szCountDownSecs) / sizeof(TCHAR), (LPARAM)szCountDownSecs);
sds.uCountDownSecs = (DWORD)ConvertASCII2Int(szCountDownSecs);
break;
case IDC_BUTTON1:
UnInstall();//删除注册表中相关信息。
EnableWindow(GetDlgItem(hwnd, IDC_BUTTON1), FALSE);
break;
case IDC_BUTTON2:
PostQuitMessage(0);
break;
case IDOK:
SaveData2Registry();//保存到注册表
EnableWindow(GetDlgItem(hwnd, IDC_BUTTON1), flHasRegData);
//并同CANCEL一样关闭对话框...
case IDCANCEL:
PostQuitMessage(0);
break;
}
return TRUE;
case WM_TIMER:
SetNowTime(hwnd);
return TRUE;
// case WM_KEYDOWN:
// switch (wParam)
// {
// case VK_TAB:
// SetFocus(GetDlgItem(hwnd, IDC_COMBO1 + uWndId++));
// break;
// }
// return TRUE;
case WM_SETFOCUS:
SetFocus(GetDlgItem(hwnd, IDC_COMBO1 + uWndId++));
break;
case WM_CLOSE:
KillTimer(hwnd, ID_TIMER);
DestroyWindow(hwnd);
return TRUE;
default:
break;
}
return FALSE;
}
void SaveData2Registry()
{
DWORD dwShutTime;
TCHAR szBuf[MAX_PATH + 20];
TCHAR szFileName[20];
dwShutTime = (sdt.wHour << 12) | (sdt.wMinute << 6) | sdt.wSecond;
flHasRegData |= SetRegistryValue(szShutReg, TEXT(""), REG_DWORD, dwShutTime);
flHasRegData |= SetRegistryValue(szShutReg, TEXT("AutoRun"), REG_SZ, sds.bAutoRun);
GetCurrentProcessName(szFileName);
if (sds.bAutoRun)
{
GetModuleFileName(NULL, szBuf, sizeof(szBuf));
_tcscat(szBuf, TEXT(" /background"));
flHasRegData |= SetRegistryValueEx(szAutoRun, szFileName, szBuf);
}
else
{
DelRegistryValue(szAutoRun, szFileName);
}
flHasRegData |= SetRegistryValue(szShutReg, TEXT("CantCancel"), REG_SZ, sds.bCantCancel);
flHasRegData |= SetRegistryValue(szShutReg, TEXT("CountDownSecs"), REG_DWORD, sds.uCountDownSecs);
}
void UnInstall(void)
{
TCHAR szFileName[20];
DeleteASubRegKey(HKEY_LOCAL_MACHINE, szShutReg);
GetCurrentProcessName(szFileName);
DelRegistryValue(szAutoRun, szFileName);
}
void Initialize(HWND hwnd)
{
TCHAR szBuffer[MAX_REGDATA_SIZE];
HICON hIcon;
hIcon = LoadIcon(NULL, IDI_INFORMATION);
if (hIcon)
{
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
}
AddComboBoxText(hwnd);
SetNowTime(hwnd);
SETCOMBOSEL(IDC_COMBO1, sdt.wHour);
SETCOMBOSEL(IDC_COMBO2, sdt.wMinute);
SETCOMBOSEL(IDC_COMBO3, sdt.wSecond);
ZeroMemory(&sds, sizeof(sds));
wsprintf(szBuffer, TEXT("%c"), QueryRegistryKey(szShutReg, TEXT("AutoRun"), REG_SZ));
if (GetLastError() == ERROR_SUCCESS)
{
flHasRegData = TRUE;
}
sds.bAutoRun = ConvertASCII2Int(szBuffer);
SendMessage(GetDlgItem(hwnd, IDC_CHECK1), BM_SETCHECK, sds.bAutoRun ? BST_CHECKED : BST_UNCHECKED, 0);
wsprintf(szBuffer, TEXT("%c"), QueryRegistryKey(szShutReg, TEXT("CantCancel"), REG_SZ));
if (GetLastError() == ERROR_SUCCESS)
{
flHasRegData = TRUE;
}
sds.bCantCancel = ConvertASCII2Int(szBuffer);
SendMessage(GetDlgItem(hwnd, IDC_CHECK2), BM_SETCHECK, sds.bCantCancel ? BST_CHECKED : BST_UNCHECKED, 0);
sds.uCountDownSecs = QueryRegistryKey(szShutReg, TEXT("CountDownSecs"), REG_DWORD);
if (GetLastError() == ERROR_SUCCESS)
{
flHasRegData = TRUE;
}
else
{
sds.uCountDownSecs = DEFAULT_COUNTDOWN_SECS;
}
FormatEditText(GetDlgItem(hwnd, IDC_EDIT2), TEXT("%d"), sds.uCountDownSecs);
EnableWindow(GetDlgItem(hwnd, IDC_BUTTON1), flHasRegData);
}
LRESULT CALLBACK MsgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
TCHAR szStrBuf[100];
LoadString(NULL, IDS_STRING101, szStrBuf, sizeof(szStrBuf) / sizeof(TCHAR));
switch (message)
{
case WM_INITDIALOG:
if (sds.uCountDownSecs == 0)
{
PostQuitMessage(0);
ExitShutDown(0);
}
FormatEditText(GetDlgItem(hwnd, IDC_COUNTSEC), szStrBuf, sds.uCountDownSecs);
if (sds.bCantCancel)
{
EnableWindow(GetDlgItem(hwnd, IDCANCEL), FALSE);
}
SetTimer(hwnd, ID_TIMER, 1000, NULL);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
ExitShutDown(0);
case IDCANCEL:
PostQuitMessage(0);
return 0;
}
break;
case WM_TIMER:
FormatEditText(GetDlgItem(hwnd, IDC_COUNTSEC), szStrBuf, --sds.uCountDownSecs);
if (sds.uCountDownSecs == 0)
{
PostQuitMessage(0);
ExitShutDown(0);
}
break;
case WM_CLOSE:
KillTimer(GetParent(hwnd), 0x0100);
DestroyWindow(hwnd);
break;
default:
break;
}
return FALSE;
}