Code:
#include <windows.h>
#include <fstream.h>
#include "controls.h"
#define WIN32_LEAN_AND_MEAN
const char ClassName[] = "WndShutDown";
HINSTANCE hInstance;
ofstream fin("errors.txt");
typedef struct _wh {
int Width;
int Height;
}WH;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
WH Main;
Main.Height = 100;
Main.Width = 300;
wc.cbClsExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wc.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = ClassName;
wc.lpszMenuName = NULL;
wc.style = 0;
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Error Registering Window Class", "Error!", MB_OK);
return 0;
}
hwnd = CreateWindowEx( 0,
ClassName,
"ShutDown Time!",
WS_OVERLAPPED | WS_VISIBLE | WS_SYSMENU,
CW_USEDEFAULT,
CW_USEDEFAULT,
Main.Width,
Main.Height,
NULL,
NULL,
hInstance,
NULL);
if(!hwnd)
{
MessageBox(NULL, "Error Creating Window", "Error!", MB_OK);
return 0;
}
while(GetMessage(&msg, hwnd, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
int time;
switch(msg)
{
case WM_CREATE:
hTime = CreateWindowEx(0, TEXT("EDIT"), TEXT("Time (In Seconds)"), WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER | ES_CENTER, 10, 10, 180, 15, hwnd, (HMENU)IDC_TIME, hInstance, NULL);
hStart = CreateWindowEx(0, TEXT("BUTTON"), TEXT("Start"), WS_CHILD | WS_VISIBLE | BS_FLAT, 190, 10, 100, 15, hwnd, (HMENU)IDC_START, hInstance, NULL);
hLogoff = CreateWindowEx(0, TEXT("BUTTON"), TEXT("Logoff"), WS_CHILD | WS_VISIBLE | BS_FLAT | BS_AUTOCHECKBOX, 10, 30, 80, 15, hwnd, (HMENU)IDC_LOGOFF, hInstance, NULL);
hShutdown = CreateWindowEx(0, TEXT("BUTTON"), TEXT("Shutdown"), WS_CHILD | WS_VISIBLE | BS_FLAT | BS_AUTOCHECKBOX, 10, 50, 85, 15, hwnd, (HMENU)IDC_SHUTDOWN, hInstance, NULL);
hRestart = CreateWindowEx(0, TEXT("BUTTON"), TEXT("Restart"), WS_CHILD | WS_VISIBLE | BS_FLAT | BS_AUTOCHECKBOX, 100, 30, 80, 15, hwnd, (HMENU)IDC_RESTART, hInstance, NULL);
CheckDlgButton(hwnd, IDC_LOGOFF, BST_CHECKED);
bLogoff = true;
break;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDC_START:
{
//time = GetDlgItemInt(hwnd, IDC_TIME, NULL, false);
//SendMessage(hTime, WM_GETTEXT, 0, (INT)time);
GetWindowText(hTime, (LPTSTR)time, NULL);
fin << time;
fin.close();
EnableWindow(hTime, false);
EnableWindow(hLogoff, false);
EnableWindow(hShutdown, false);
EnableWindow(hRestart, false);
EnableWindow(hStart, false);
SetTimer(hwnd, IDC_TIMER, 1000, NULL);
}
break;
case IDC_LOGOFF:
{
cLogoff = IsDlgButtonChecked(hwnd, IDC_LOGOFF);
if(cLogoff & BST_CHECKED)
{
bLogoff = true;
bShutdown = false;
bRestart = false;
CheckDlgButton(hwnd, IDC_SHUTDOWN, BST_UNCHECKED);
CheckDlgButton(hwnd, IDC_RESTART, BST_UNCHECKED);
}
else //if(cLogoff & BST_UNCHECKED)
CheckDlgButton(hwnd, IDC_LOGOFF, BST_CHECKED);
}
break;
case IDC_SHUTDOWN:
{
cShutdown = IsDlgButtonChecked(hwnd, IDC_SHUTDOWN);
if(cShutdown & BST_CHECKED)
{
bLogoff = false;
bShutdown = true;
bRestart = false;
CheckDlgButton(hwnd, IDC_LOGOFF, BST_UNCHECKED);
CheckDlgButton(hwnd, IDC_RESTART, BST_UNCHECKED);
}
else //if(cShutdown & BST_UNCHECKED)
CheckDlgButton(hwnd, IDC_SHUTDOWN, BST_CHECKED);
}
break;
case IDC_RESTART:
{
cRestart = IsDlgButtonChecked(hwnd, IDC_RESTART);
if(cRestart & BST_CHECKED)
{
bLogoff = false;
bShutdown = false;
bRestart = true;
CheckDlgButton(hwnd, IDC_LOGOFF, BST_UNCHECKED);
CheckDlgButton(hwnd, IDC_SHUTDOWN, BST_UNCHECKED);
}
else //if(cShutdown & BST_UNCHECKED)
CheckDlgButton(hwnd, IDC_RESTART, BST_CHECKED);
}
break;
}
}
break;
case WM_TIMER:
{
switch(wParam)
{
case IDC_TIMER:
{
if(time > 0)
{
time--;
SetWindowText(hTime, (LPTSTR)time);
}
if(time == 0)
{
EnableWindow(hTime, true);
EnableWindow(hLogoff, true);
EnableWindow(hShutdown, true);
EnableWindow(hRestart, true);
EnableWindow(hStart, true);
KillTimer(hwnd, IDC_TIMER);
}
}
break;
}
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}