由于本人记性不太好,晚上经常忘记关电脑,白白浪费了很多的电。在网上也找了一些定时关机的软件,想到最近自己也在学习VC++,就想自己写一个定时关机的小程序,正好也可以检验自己所学的东西。
程序源码下载地址:点击下载
先来看看效果图:
写的比较简陋,但是只要能实现功能就好了。
写Win32应用程序首先要有WinMain函数:
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
g_hInstance = hInstance;
DialogBox(hInstance, (LPCTSTR)IDD_MAIN, NULL, DlgProc);
return 0;
}
在WinMain中调用DailogBox()弹出一个对话框,下面就是对消息的处理。
INT_PTR CALLBACK DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int wmId;
switch(uMsg)
{
case WM_INITDIALOG:
g_hWnd = hWnd;
InitDialog();
break;
case WM_CLOSE:
Shell_NotifyIcon(NIM_DELETE, &g_nidTray);
EndDialog(hWnd, 0);
break;
case WM_TIMER:
ProcessTimer();
break;
case WM_SIZE:
if(wParam == SIZE_MINIMIZED)
{
ShowWindow(g_hWnd, SW_HIDE);
}
break;
case NW_SHELLNOTIFY:
switch(lParam)
{
case WM_LBUTTONDBLCLK:
ShowWindow(g_hWnd, SW_SHOWDEFAULT);
SetForegroundWindow(g_hWnd);
break;
case WM_RBUTTONUP:
{
POINT ptPosition;
GetCursorPos(&ptPosition);
SetForegroundWindow(g_hWnd);
TrackPopupMenu(GetSubMenu(g_hMenu, 0), TPM_LEFTALIGN | TPM_LEFTBUTTON, ptPosition.x, ptPosition.y, 0, g_hWnd, NULL);
}
break;
}
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
switch(wmId)
{
case IDBTN_SHUTDOWN:
DateTime_GetSystemtime(GetDlgItem(g_hWnd, IDDT_TIME), &g_stShutdown);
SetTimer(hWnd, ID_TIMER, 1000, NULL);
ChangeEnable(FALSE);
break;
case IDBTN_CANCEL:
KillTimer(hWnd, ID_TIMER);
UpdateRemainTime(NULL);
ChangeEnable(TRUE);
break;
case IDM_SHOW:
ShowWindow(g_hWnd, SW_SHOWDEFAULT);
SetForegroundWindow(g_hWnd);
break;
case IDM_EXIT:
SendMessage(g_hWnd, WM_CLOSE, 0, 0L);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
break;
}
return 0;
}
主体部分就是上面的内容,想要完整的程序源码可以去下载。
程序源码下载地址:点击下载
用Win32写的定时关机程序