#include <windows.h>
#include "resource.h"
#include <commctrl.h>
//消息定义
#define WM_SHELLNOTIFY WM_USER+1
//Download by http://www.codefans.net
//资源定义
#define IDM_RESTORE 100
#define IDM_EXIT 101
#define IDI_NID 0
#define IDC_EDIT_SHUTDOWNTIME 1000
#define IDC_SPIN_SHUTDOWNTIME 1001
#define IDC_BTN_APPLY 1002
#define IDC_BTN_CANCELAPPLY 1003
#define IDC_BTN_LOGOFF 1004
#define IDC_BTN_REBOOT 1005
#define IDC_BTN_SHUTDOWN 1006
#define SCREEN_WIDTH GetSystemMetrics(SM_CXSCREEN)
#define SCREEN_HEIGHT GetSystemMetrics(SM_CYSCREEN)
#define WINDOW_WIDTH 280
#define WINDOW_HEIGHT 300
#define EDIT_SHUTDOWN_WIDTH 60
#define EDIT_SHUTDOWN_HEIGHT 20
#define SPIN_SHUTDOWN_WIDTH 30
#define SPIN_SHUTDOWN_HEIGHT EDIT_SHUTDOWN_HEIGHT
#define BUTTON_WIDTH 64
#define BUTTON_HEIGHT 25
#define STATIC_WIDTH 155
#define STATIC_HEIGHT 20
#define BTN_TEXT_APPLY TEXT("开始定时")
#define BTN_TEXT_CANCELAPPLY TEXT("取消定时")
#define BTN_TEXT_LOGOFF TEXT("注销")
#define BTN_TEXT_REBOOT TEXT("重启")
#define BTN_TEXT_SHUTDOWN TEXT("关机")
/*
全局变量
*/
const TCHAR szClassName[] = L"Timer";
const TCHAR szTitle[] = L"定时关机";
const TCHAR g_szTip[] = L"Timer";
const TCHAR g_szShutDownTime[] = L"关机时间:";
const TCHAR g_szAfterMinute[] = L"分钟后";
TCHAR g_szTime[MAX_PATH];
HWND g_hWnd = NULL;
HWND g_hStaticWnd = NULL;
HWND g_hEditWnd = NULL;
HWND g_hSpinWnd = NULL;
HWND g_hBtnApply = NULL;
HWND g_hBtnCancelApply = NULL;
HDC g_hdc = NULL;
UINT g_nSDTime = -1; //记录关机的时间
UINT g_nCounter = 0; //记录秒
BOOL g_bIsApply = FALSE; //记录是否开始计时
HINSTANCE g_hInstance;
HMENU g_hPopupMenu = NULL;
NOTIFYICONDATA note; //最小化到托盘时的结构
/*
函数声明
*/
void OnCreate(); //WM_CREATE时调用
void OnTimer(UINT nIDEvent); //WM_TIMER
void ShutDown(); //关机
void LogOff(); //注销
void ReBoot(); //重启
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE , LPSTR lpCmdLine, int nShowCmd)
{
HWND hWnd;
WNDCLASSEX wcex;
wcex.cbClsExtra = 0;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbWndExtra = 0;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(NULL, IDI_SHIELD);
wcex.hIconSm = NULL;
wcex.hInstance = hInstance;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.lpszClassName = szClassName;
wcex.lpszMenuName = NULL;
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
if(!RegisterClassEx(&wcex))
{
MessageBox(NULL, L"注册窗口失败!", L"出错啦", MB_OK | MB_ICONERROR);
return -1;
}
hWnd = CreateWindow(
szClassName,
szTitle,
WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX,
(SCREEN_WIDTH-WINDOW_WIDTH)/2,
(SCREEN_HEIGHT-WINDOW_HEIGHT)/2,
WINDOW_WIDTH,
WINDOW_HEIGHT,
HWND_DESKTOP,
NULL,
hInstance,
NULL
);
if(!hWnd)
{
MessageBox(NULL,L"创建窗口失败!", L"出错啦", MB_OK|MB_ICONERROR);
return -1;
}
g_hWnd = hWnd;
g_hInstance = hInstance;
UpdateWindow(hWnd);
ShowWindow(hWnd,nShowCmd);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
SetTimer(hWnd,1,1000,NULL);
g_hWnd = hWnd;
g_hdc = GetDC(g_hWnd);
OnCreate();
return 0;
case WM_TIMER:
OnTimer(1);
return 0;
case WM_PAINT:
{
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(g_hWnd, &ps);
TCHAR szFormat[] = L"现在时间:%d:%d:%d%d";
SYSTEMTIME st;
GetLocalTime(&st);
wsprintf(g_szTime, szFormat, st.wHour, st.wMinute, st.wSecond/10, st.wSecond%10);
TextOut(hdc, 65, 20, g_szTime, lstrlen(g_szTime));
//显示----关机时间:XXXX分钟后
TextOut(hdc, 30, 60, g_szShutDownTime, lstrlen(g_szShutDownTime));
TextOut(hdc, 100+EDIT_SHUTDOWN_WIDTH+SPIN_SHUTDOWN_WIDTH, 60, g_szAfterMinute, lstrlen(g_szAfterMinute));
TextOut(hdc, 80, 200, L"作者:tszhao", lstrlen(L"作者:tszhao"));
TextOut(hdc, 40, 220, L"MSN:tszhao10@hotmail.com", lstrlen(L"MSN:tszhao10@hotmail.com"));
EndPaint(g_hWnd, &ps);
}
return 0;
case WM_SIZE:
if(wParam == SIZE_MINIMIZED)
{
note.cbSize = sizeof(NOTIFYICONDATA);
note.hWnd = g_hWnd;
note.uID = IDI_NID;
note.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
note.uCallbackMessage = WM_SHELLNOTIFY;
note.hIcon = LoadIcon(g_hInstance,MAKEINTRESOURCE(IDI_ICON1));
lstrcpy(note.szTip,g_szTip);
ShowWindow(g_hWnd, SW_HIDE);
Shell_NotifyIcon(NIM_ADD, ¬e);
}
return 0;
case WM_COMMAND:
///接收notify
if(lParam == 0)
{
Shell_NotifyIcon(NIM_DELETE, ¬e);
if(LOWORD(wParam) == IDM_RESTORE)
{
ShowWindow(g_hWnd, SW_RESTORE);
}
else
{
DestroyWindow(g_hWnd);
}
}
//响应编辑框事件
if(LOWORD(wParam) == IDC_EDIT_SHUTDOWNTIME)
{
switch(HIWORD(wParam))
{
case EN_CHANGE:
TCHAR szText[MAX_PATH];
GetWindowText(g_hEditWnd, szText, MAX_PATH);
SendMessage(g_hSpinWnd, UDM_SETPOS, 0, (LPARAM)MAKELONG(_wtoi(szText), 0));
break;
case EN_SETFOCUS:
SendMessage(g_hEditWnd, EM_SETSEL, 0, SendMessage(g_hEditWnd, EM_LINELENGTH, 1, 0));
break;
}
}
////点击应用定时按钮
if(wParam == IDC_BTN_APPLY)
{
TCHAR szText[MAX_PATH];
GetWindowText(g_hEditWnd, szText, MAX_PATH);
g_nSDTime = _wtoi(szText);
if(g_nSDTime == 0)
{
MessageBox(NULL, L"请输入一个大于0的数值!", L"出错啦", MB_OK|MB_ICONERROR);
return 0;
}
g_bIsApply = TRUE;
EnableWindow(g_hEditWnd, FALSE);
EnableWindow(g_hSpinWnd, FALSE);
EnableWindow(g_hBtnApply, FALSE);
EnableWindow(g_hBtnCancelApply, TRUE);
}
//响应取消定时
if(wParam == IDC_BTN_CANCELAPPLY)
{
g_bIsApply = FALSE;
g_nSDTime = -1;
EnableWindow(g_hEditWnd, TRUE);
EnableWindow(g_hSpinWnd, TRUE);
EnableWindow(g_hBtnApply, TRUE);
EnableWindow(g_hBtnCancelApply, FALSE);
}
//响应关机
if(wParam == IDC_BTN_SHUTDOWN)
{
ShutDown();
}
//响应注销
if(wParam == IDC_BTN_LOGOFF)
{
LogOff();
}
//响应重启
if(wParam == IDC_BTN_REBOOT)
{
ReBoot();
}
return 0;
case WM_VSCROLL:
{
int nPos = SendMessage(g_hSpinWnd, UDM_GETPOS32, 0, 0);
TCHAR szMinute[MAX_PATH];
wsprintf(szMinute, L"%d", nPos);
SetWindowText(g_hEditWnd, szMinute);
}
return 0;
case WM_SHELLNOTIFY:
if(wParam == IDI_NID)
{
if(lParam == WM_LBUTTONDBLCLK)
{
ShowWindow(g_hWnd, SW_RESTORE);
Shell_NotifyIcon(NIM_DELETE, ¬e);
SetForegroundWindow(g_hWnd);
}
if(lParam == WM_RBUTTONDOWN)
{
POINT pt;
GetCursorPos(&pt);
TrackPopupMenu(g_hPopupMenu, TPM_RIGHTALIGN, pt.x, pt.y, 0, g_hWnd, NULL);
}
}
return 0;
case WM_DESTROY:
KillTimer(hWnd,1);
PostQuitMessage(1);
return 0;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
}
//Download by http://www.codefans.net
void OnCreate()
{
//创建右击时的菜单
g_hPopupMenu = CreatePopupMenu();
AppendMenu(g_hPopupMenu, MF_STRING, IDM_RESTORE, L"&Restore");
AppendMenu(g_hPopupMenu, MF_STRING, IDM_EXIT, L"Exit");
g_hEditWnd = CreateWindow(L"EDIT", L"", WS_VISIBLE|WS_CHILD|ES_LEFT|ES_NUMBER|WS_BORDER, 100, 58, EDIT_SHUTDOWN_WIDTH, EDIT_SHUTDOWN_HEIGHT, g_hWnd, (HMENU)IDC_EDIT_SHUTDOWNTIME, NULL, NULL);
g_hSpinWnd = CreateWindow(UPDOWN_CLASS, L"", WS_CHILD|WS_VISIBLE|UDS_AUTOBUDDY, 160, 58, SPIN_SHUTDOWN_WIDTH, SPIN_SHUTDOWN_HEIGHT, g_hWnd, (HMENU)IDC_SPIN_SHUTDOWNTIME, NULL, NULL);
g_hStaticWnd = CreateWindow(L"STATIC", L"", WS_VISIBLE|WS_CHILD|SS_CENTER, 60, 100, STATIC_WIDTH, STATIC_HEIGHT, g_hWnd, NULL, NULL, NULL);
//创建按钮
g_hBtnApply = CreateWindow(L"BUTTON", BTN_TEXT_APPLY, WS_VISIBLE|W