📄 subject_28256.htm
字号:
<p>
序号:28256 发表者:icemen 发表日期:2003-01-19 23:59:56
<br>主题:请教定时函数的用法
<br>内容:VOID CALLBACK TimerProc(<BR> HWND hwnd, // handle of window for timer messages<BR> UINT uMsg, // WM_TIMER message<BR> UINT idEvent, // timer identifier<BR> DWORD dwTime // current system time<BR>);<BR><BR>UINT SetTimer(<BR> HWND hWnd, // handle of window for timer messages<BR> UINT nIDEvent, // timer identifier<BR> UINT uElapse, // time-out value<BR> TIMERPROC lpTimerFunc // address of timer procedure<BR>);<BR><BR>请教上面两个函数如何结合来用,我的定时程序用Win32 Application 产生,不能用到ontimer 事件,请问如可用SetTimer和TomerProc ? 能否有源程序例子?<BR>请教! 急,谢谢!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:upstream 回复日期:2003-01-20 08:34:14
<br>内容:SetTimer和TomerProc的用法可参考下面的例子,是一个时钟,希望对你能有所帮助。<BR><BR><BR>#include <windows.h><BR>#include <string.h><BR>#include <time.h><BR>#include <math.h><BR><BR>#define ID_TIMER 1<BR>#define TWOPI (2 * 3.14159)<BR><BR>LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;<BR><BR>int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,<BR> PSTR szCmdLine, int iCmdShow)<BR> {<BR> static char szAppName[] = "AnaClock" ;<BR> HWND hwnd;<BR> MSG msg;<BR> WNDCLASSEX wndclass ;<BR><BR> wndclass.cbSize = sizeof (wndclass) ;<BR> wndclass.style = CS_HREDRAW | CS_VREDRAW ;<BR> wndclass.lpfnWndProc = WndProc ;<BR> wndclass.cbClsExtra = 0 ;<BR> wndclass.cbWndExtra = 0 ;<BR> wndclass.hInstance = hInstance ;<BR> wndclass.hIcon = NULL ;<BR> wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;<BR> wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;<BR> wndclass.lpszMenuName = NULL ;<BR> wndclass.lpszClassName = szAppName ;<BR> wndclass.hIconSm = NULL ;<BR><BR> RegisterClassEx (&wndclass) ;<BR><BR> hwnd = CreateWindow (szAppName, "Analog Clock",<BR> WS_OVERLAPPEDWINDOW,<BR> CW_USEDEFAULT, CW_USEDEFAULT,<BR> CW_USEDEFAULT, CW_USEDEFAULT,<BR> NULL, NULL, hInstance, NULL) ;<BR><BR> if (!SetTimer (hwnd, ID_TIMER, 1000, NULL))<BR> {<BR> MessageBox (hwnd, "Too many clocks or timers!", szAppName,<BR> MB_ICONEXCLAMATION | MB_OK) ;<BR> return FALSE ;<BR> }<BR><BR> ShowWindow (hwnd, iCmdShow) ;<BR> UpdateWindow (hwnd) ;<BR><BR> while (GetMessage (&msg, NULL, 0, 0))<BR> {<BR> TranslateMessage (&msg) ;<BR> DispatchMessage (&msg) ;<BR> }<BR> return msg.wParam ;<BR> }<BR><BR>void SetIsotropic (HDC hdc, int cxClient, int cyClient)<BR> {<BR> SetMapMode (hdc, MM_ISOTROPIC) ;<BR> SetWindowExtEx (hdc, 1000, 1000, NULL) ;<BR> SetViewportExtEx (hdc, cxClient / 2, -cyClient / 2, NULL) ;<BR> SetViewportOrgEx (hdc, cxClient / 2, cyClient / 2, NULL) ;<BR> }<BR><BR>void RotatePoint (POINT pt[], int iNum, int iAngle)<BR> {<BR> int i ;<BR> POINT ptTemp ;<BR><BR> for (i = 0 ; i < iNum ; i++)<BR> {<BR> ptTemp.x = (int) (pt[i].x * cos (TWOPI * iAngle / 360) +<BR> pt[i].y * sin (TWOPI * iAngle / 360)) ;<BR><BR> ptTemp.y = (int) (pt[i].y * cos (TWOPI * iAngle / 360) -<BR> pt[i].x * sin (TWOPI * iAngle / 360)) ;<BR><BR> pt[i] = ptTemp ;<BR> }<BR> }<BR><BR>void DrawClock (HDC hdc)<BR> {<BR> int iAngle ;<BR> POINT pt[3] ;<BR><BR> for (iAngle = 0 ; iAngle < 360 ; iAngle += 6)<BR> {<BR> pt[0].x = 0 ;<BR> pt[0].y = 900 ;<BR><BR> RotatePoint (pt, 1, iAngle) ;<BR><BR> pt[2].x = pt[2].y = iAngle % 5 ? 33 : 100 ;<BR><BR> pt[0].x -= pt[2].x / 2 ;<BR> pt[0].y -= pt[2].y / 2 ;<BR><BR> pt[1].x = pt[0].x + pt[2].x ;<BR> pt[1].y = pt[0].y + pt[2].y ;<BR><BR> SelectObject (hdc, GetStockObject (BLACK_BRUSH)) ;<BR><BR> Ellipse (hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y) ;<BR> }<BR> }<BR><BR>void DrawHands (HDC hdc, struct tm *datetime, BOOL bChange)<BR> {<BR> static POINT pt[3][5] = { 0, -150, 100, 0, 0, 600, -100, 0, 0, -150,<BR> 0, -200, 50, 0, 0, 800, -50, 0, 0, -200,<BR> 0, 0, 0, 0, 0, 0, 0, 0, 0, 800 } ;<BR> int i, iAngle[3] ;<BR> POINT ptTemp[3][5] ;<BR><BR> iAngle[0] = (datetime->tm_hour * 30) % 360 + datetime->tm_min / 2 ;<BR> iAngle[1] = datetime->tm_min * 6 ;<BR> iAngle[2] = datetime->tm_sec * 6 ;<BR><BR> memcpy (ptTemp, pt, sizeof (pt)) ;<BR><BR> for (i = bChange ? 0 : 2 ; i < 3 ; i++)<BR> {<BR> RotatePoint (ptTemp[i], 5, iAngle[i]) ;<BR><BR> Polyline (hdc, ptTemp[i], 5) ;<BR> }<BR> }<BR><BR>LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)<BR> {<BR> static int cxClient, cyClient ;<BR> static struct tm dtPrevious ;<BR> BOOL bChange ;<BR> HDC hdc ;<BR> PAINTSTRUCT ps ;<BR> time_t lTime ;<BR> struct tm *datetime ;<BR><BR> switch (iMsg)<BR> {<BR> case WM_CREATE :<BR> time (&lTime) ;<BR> datetime = localtime (&lTime) ;<BR><BR> dtPrevious = * datetime ;<BR> return 0 ;<BR><BR> case WM_SIZE :<BR> cxClient = LOWORD (lParam) ;<BR> cyClient = HIWORD (lParam) ;<BR> return 0 ;<BR><BR> case WM_TIMER :<BR> time (&lTime) ;<BR> datetime = localtime (&lTime) ;<BR><BR> bChange = datetime->tm_hour != dtPrevious.tm_hour ||<BR> datetime->tm_min != dtPrevious.tm_min ;<BR><BR> hdc = GetDC (hwnd) ;<BR><BR> SetIsotropic (hdc, cxClient, cyClient) ;<BR><BR> SelectObject (hdc, GetStockObject (WHITE_PEN)) ;<BR> DrawHands (hdc, &dtPrevious, bChange) ;<BR><BR> SelectObject (hdc, GetStockObject (BLACK_PEN)) ;<BR> DrawHands (hdc, datetime, TRUE) ;<BR><BR> ReleaseDC (hwnd, hdc) ;<BR><BR> dtPrevious = *datetime ;<BR> return 0 ;<BR><BR> case WM_PAINT :<BR> hdc = BeginPaint (hwnd, &ps) ;<BR><BR> SetIsotropic (hdc, cxClient, cyClient) ;<BR> DrawClock (hdc) ;<BR> DrawHands (hdc, &dtPrevious, TRUE) ;<BR><BR> EndPaint (hwnd, &ps) ;<BR> return 0 ;<BR><BR> case WM_DESTROY :<BR> KillTimer (hwnd, ID_TIMER) ;<BR> PostQuitMessage (0) ;<BR> return 0 ;<BR> }<BR> return DefWindowProc (hwnd, iMsg, wParam, lParam) ;<BR> }<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:在水一方 回复日期:2003-01-20 09:54:01
<br>内容: UINT SetTimer(<BR> HWND hWnd, // 将要接受WM_TIMER消息的窗口句柄<BR> UINT nIDEvent, // 计时器的标识号ID(由用户自定义)<BR> UINT uElapse, // 计时器的间隔时间(以毫秒为单位)<BR> TIMERPROC lpTimerFunc // CALLBACK函数地址<BR> );<BR> 此函数的返回值用来标识这个计时器,并且将被KillTimer API使用;失败时返回0.<BR><BR>说明:<BR> 当计时器的间隔时间到时,如果lpTimerFunc不为NULL,则系统调用lpTimerFunc,<BR>否则系统向窗口hWnd发送WM_TIMER消息。通常的做法是:将lpTimerFunc设为NULL,<BR>只需处理系统发过来的WM_TIMER消息即可。当不再需要计时器时,可以用API KillTimer<BR>将其Kill掉;Kill掉以后,窗口hWnd不会再收到WM_TIMER消息。<BR> BOOL KillTimer(<BR> HWND hWnd, // 安装计时器的窗口句柄<BR> UINT uID // SetTimer的返回值<BR> );<BR><BR><BR>例如:<BR>1> SDK处理方式:<BR><BR>#define ID_MY_TIMER 100<BR><BR>LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)<BR>{<BR> switch (message)<BR> {<BR> case WM_CREATE:<BR> if (SetTimer(hWnd, ID_MY_TIMER, 1000, NULL) == 0)<BR> {<BR> MessageBox(hWnd, "Fail to set the timer", "infor", MB_OK);<BR> }<BR> return 0;<BR> <BR> case WM_TIMER:<BR> if (wParam == ID_MY_TIMER)<BR> {<BR> //处理计时器操作<BR> }<BR> return 0;<BR> }<BR> <BR> return DefWindowProc(hWnd, message, wParam, lParam);<BR>}<BR><BR>2> MFC处理方式:<BR><BR>#define ID_MY_TIMER 100<BR><BR>int CMyWnd::OnCreate( LPCREATESTRUCT lpCreateStruct )<BR>{<BR> if (SetTimer(ID_MY_TIMER, 1000, NULL) == 0)<BR> {<BR> MessageBox("Fail to set the timer");<BR> return -1;<BR> }<BR> return 0;<BR>}<BR><BR>void CMyWnd::OnTimer(UINT nID)<BR>{<BR> if (wParam == ID_MY_TIMER)<BR> {<BR> //处理计时器操作<BR> } <BR>}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -