请问c++怎么做一个定时程序
我想让它每隔两小时弹出一个窗口提示:休息一下,休息一下。要一个控制台程序,最好不要mfc的控制台程序,也别用系统的计划任务,感觉用哪个干脆就别编程了
我想让它每隔两小时弹出一个窗口提示:休息一下,休息一下。要一个控制台程序,最好不要mfc的控制台程序,也别用系统的计划任务,感觉用哪个干脆就别编程了
答案://GetTickCount方法效率太低//Sleep会使程序卡住且并且要关闭对话框才能继续即时
//用Timer消息才是最完美的
// consoleTimer.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include<windows.h>
HINSTANCE hInst;
TCHAR szTitle[128]="控制台提醒程序^_^";
TCHAR szWindowClass[128]="HuazaiWindow";ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
#pragma comment(lib,"user32.lib")
int main()
{
// TODO: Place code here.
MSG msg;
hInst=GetModuleHandle(NULL);
ATOM atom=MyRegisterClass(hInst);// Perform application initialization:
if (!InitInstance (hInst, SW_HIDE))
{
return FALSE;
}while (GetMessage(&msg, NULL, 0, 0))
{TranslateMessage(&msg);
DispatchMessage(&msg);}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
WNDCLASSEX wcex;
ATOM MyRegisterClass(HINSTANCE hInstance)
{
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
//wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_WIN32);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
//wcex.lpszMenuName = (LPCTSTR)IDC_WIN32;
wcex.lpszClassName = szWindowClass;
//wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);return RegisterClassEx(&wcex);
}//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, 300, 200, NULL, NULL, hInstance, NULL);if (!hWnd)
{
return FALSE;
}
return TRUE;
}//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
HWND hwndNotepad=NULL;
RECT rect;
char* str;switch (message)
{
case WM_CREATE:
SetTimer(hWnd,18829777,1000,NULL);
break;case WM_TIMER:
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
break;case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
//case IDM_EXIT:
// KillTimer(hWnd,18829777);
// //DestroyWindow(hWnd);
// break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd,&rect);
str="您该休息下了^_^";
DrawText(hdc,str,strlen(str),&rect,DT_CENTER);
//TextOut(
EndPaint(hWnd, &ps);
break;
case WM_CLOSE:
ShowWindow(hWnd,SW_HIDE);
case WM_DESTROY:
//PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}GetTickCount可以取时间计数值,判断两次取的差值,可以满足实现2小时间隔要求,但整体不知是否合适,仅供参考。Sleep(3600*2*1000);timer
上一个:C++怎么样改指定内存地址的值
下一个:那位高手给调以下C++程序