注:以下内容为学习笔记,多数是从书本、资料中得来,只为加深印象,及日后参考。然而本人表达能力较差,写的不好。因非翻译、非转载,只好选原创,但多数乃摘抄,实为惭愧。但若能帮助一二访客,幸甚!

以下内容多数来自于《Windows程序设计》

1.计时器基础

Windows计时器是一种输入设备,每到一个指定的时间间隔,它都会周期性地发送WM_TIMER通知应用程序。

一些应用:多任务、保持更新进度报告(不断显式变化的信息)、实现定期自动存储、终止程序的演示版、控制运动速度、多媒体。

Windows应用程序是通过正常的消息队列来接收WM_TIMER消息,所以不用担心程序在处理其他任务时会被突然而来的WM_TIMER消息所“中断”。

WM_TIMER消息被放在正常的消息队列中,并和其他消息一起排队等候处理。因此并不能保证每隔指定时间就收到一个WM_TIMER消息。

WM_TIMER和WM_PAINT消息类似,都是低优先级的,只有当消息队列中没有其他消息时,程序才会收到它们。同样,应用程序也同时不会收到大量的WM_TIMER消息。因此不能通过WM_TIMER消息数来判定过去了多长时间。

2.使用计时器的方法

1)法1:

[cpp]  view plain copy print ?
  1. SetTimer(hwnd, 1, uiMsecInterval, NULL);
  2. KillTimer(hwnd, 1);

在 WM_TIMER消息中,wParam等于计时器的ID。

[cpp]  view plain copy print ?
  1. /*--------------------------------------
  2. BEEPER1.C -- Timer Demo Program ver1
  3. --------------------------------------*/
  4. #include <windows.h>
  5. #include <stdlib.h>
  6. #define ID_TIMER    1
  7. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  8. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  9. PSTR szCmdLine, int iCmdShow)
  10. {
  11. static TCHAR szAppName[] = TEXT("Beeper1");
  12. HWND         hwnd;
  13. MSG          msg;
  14. WNDCLASS     wndclass;
  15. wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  16. wndclass.lpfnWndProc   = WndProc;
  17. wndclass.cbClsExtra    = 0;
  18. wndclass.cbWndExtra    = 0;
  19. wndclass.hInstance     = hInstance;
  20. wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  21. wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  22. wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
  23. wndclass.lpszMenuName  = NULL;
  24. wndclass.lpszClassName = szAppName;
  25. if (!RegisterClass (&wndclass))
  26. {
  27. MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
  28. return 0 ;
  29. }
  30. hwnd = CreateWindow(szAppName, TEXT ("Beeper1 Timer Demo"),
  31. WS_OVERLAPPEDWINDOW,
  32. CW_USEDEFAULT, CW_USEDEFAULT,
  33. CW_USEDEFAULT, CW_USEDEFAULT,
  34. NULL, NULL, hInstance, NULL);
  35. ShowWindow(hwnd, iCmdShow);
  36. UpdateWindow(hwnd);
  37. while (GetMessage(&msg, NULL, 0, 0))
  38. {
  39. TranslateMessage(&msg);
  40. DispatchMessage(&msg);
  41. }
  42. return msg.wParam;
  43. }
  44. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  45. {
  46. HBRUSH          hBrush;
  47. HDC             hdc;
  48. PAINTSTRUCT     ps;
  49. RECT            rc;
  50. switch (message)
  51. {
  52. case WM_CREATE:
  53. SetTimer(hwnd, ID_TIMER, 1000, NULL);
  54. return 0 ;
  55. case WM_TIMER:
  56. MessageBeep(-1);
  57. InvalidateRect(hwnd, NULL, FALSE);
  58. return 0;
  59. case WM_PAINT:
  60. hdc = BeginPaint (hwnd, &ps) ;
  61. GetClientRect(hwnd, &rc);
  62. hBrush = CreateSolidBrush(RGB(rand()%255, rand()%255, rand()%255));
  63. FillRect(hdc, &rc, hBrush);
  64. EndPaint (hwnd, &ps) ;
  65. return 0 ;
  66. case WM_DESTROY:
  67. KillTimer(hwnd, ID_TIMER);
  68. PostQuitMessage (0) ;
  69. return 0 ;
  70. }
  71. return DefWindowProc (hwnd, message, wParam, lParam) ;
  72. }

2)法2:

让Windows把计时器消息发送到程序中的另一个函数。

收到计时器消息的函数被称为“回调”函数。这是程序中被Windows调用的函数。回调函数必须定义为CALLBACK类型,因为Windows是从程序的代码空间以外调用这个函数的。送到回调函数的参数和从回调函数返回的数据是由该函数要实现的功能所决定的。当回调函数与计时器同时使用时,它的参数实际上与窗口过程的参数是一样的,只不过他们的定义不同。

[cpp]  view plain copy print ?
[cpp]  view plain copy print ?
  1. VOID CALLBACK TimerProc(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
  2. {
  3. }
  4. SetTimer(hwnd, iTimerID, iMsecInterval, TimerProc);

示例:

[cpp]  view plain copy print ?
  1. /*-----------------------------------------------------------
  2. BEEPER2.cpp -- Timer Demo Program ver1
  3. ------------------------------------------------------------*/
  4. #include <windows.h>
  5. #include <stdlib.h>
  6. #define ID_TIMER    1
  7. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  8. VOID    CALLBACK TimerProc(HWND, UINT, UINT, DWORD);
  9. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  10. PSTR szCmdLine, int iCmdShow)
  11. {
  12. static TCHAR szAppName[] = TEXT("Beeper2");
  13. HWND         hwnd;
  14. MSG          msg;
  15. WNDCLASS     wndclass;
  16. wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  17. wndclass.lpfnWndProc   = WndProc;
  18. wndclass.cbClsExtra    = 0;
  19. wndclass.cbWndExtra    = 0;
  20. wndclass.hInstance     = hInstance;
  21. wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  22. wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  23. wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
  24. wndclass.lpszMenuName  = NULL;
  25. wndclass.lpszClassName = szAppName;
  26. if (!RegisterClass (&wndclass))
  27. {
  28. MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
  29. return 0 ;
  30. }
  31. hwnd = CreateWindow(szAppName, TEXT ("Beeper2 Timer Demo"),
  32. WS_OVERLAPPEDWINDOW,
  33. CW_USEDEFAULT, CW_USEDEFAULT,
  34. CW_USEDEFAULT, CW_USEDEFAULT,
  35. NULL, NULL, hInstance, NULL);
  36. ShowWindow(hwnd, iCmdShow);
  37. UpdateWindow(hwnd);
  38. while (GetMessage(&msg, NULL, 0, 0))
  39. {
  40. TranslateMessage(&msg);
  41. DispatchMessage(&msg);
  42. }
  43. return msg.wParam;
  44. }
  45. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  46. {
  47. switch (message)
  48. {
  49. case WM_CREATE:
  50. SetTimer(hwnd, ID_TIMER, 1000, TimerProc);
  51. return 0 ;
  52. case WM_DESTROY:
  53. KillTimer(hwnd, ID_TIMER);
  54. PostQuitMessage (0) ;
  55. return 0 ;
  56. }
  57. return DefWindowProc (hwnd, message, wParam, lParam) ;
  58. }
  59. VOID CALLBACK TimerProc(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
  60. {
  61. HBRUSH          hBrush;
  62. HDC             hdc;
  63. RECT            rc;
  64. MessageBeep(-1);
  65. GetClientRect(hwnd, &rc);
  66. hdc     = GetDC(hwnd);
  67. hBrush = CreateSolidBrush(RGB(rand()%255, rand()%255, rand()%255));
  68. FillRect(hdc, &rc, hBrush);
  69. ReleaseDC(hwnd, hdc);
  70. DeleteObject(hBrush);
  71. }

3)法3:

需要多次调用SetTimer,又不想记录哪些计时器ID已经被使用过,可以:

[cpp]  view plain copy print ?
  1. iTimerID = SetTimer(NULL, 0, wMsecInterval, TimerProc);
  2. KillTimer(NULL, iTimerID);

3.使用计时器作为时钟

1)数字时钟

模拟LED的七段显示,显示当前时间:
[cpp]  view plain copy print ?
  1. /*-----------------------------------------------------------
  2. digClock.cpp -- Digital Clock
  3. ------------------------------------------------------------*/
  4. #include <windows.h>
  5. #define ID_TIMER    1
  6. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  7. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  8. PSTR szCmdLine, int iCmdShow)
  9. {
  10. static TCHAR szAppName[] = TEXT("DigClock");
  11. HWND         hwnd;
  12. MSG          msg;
  13. WNDCLASS     wndclass;
  14. wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  15. wndclass.lpfnWndProc   = WndProc;
  16. wndclass.cbClsExtra    = 0;
  17. wndclass.cbWndExtra    = 0;
  18. wndclass.hInstance     = hInstance;
  19. wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  20. wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  21. wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
  22. wndclass.lpszMenuName  = NULL;
  23. wndclass.lpszClassName = szAppName;
  24. if (!RegisterClass (&wndclass))
  25. {
  26. MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
  27. return 0 ;
  28. }
  29. hwnd = CreateWindow(szAppName, TEXT ("Digital Clock"),
  30. WS_OVERLAPPEDWINDOW,
  31. CW_USEDEFAULT, CW_USEDEFAULT,
  32. CW_USEDEFAULT, CW_USEDEFAULT,
  33. NULL, NULL, hInstance, NULL);
  34. ShowWindow(hwnd, iCmdShow);
  35. UpdateWindow(hwnd);
  36. while (GetMessage(&msg, NULL, 0, 0))
  37. {
  38. TranslateMessage(&msg);
  39. DispatchMessage(&msg);
  40. }
  41. return msg.wParam;
  42. }
  43. void DisplayDigit(HDC hdc, int iNumber)
  44. {
  45. static BOOL fSevenSegment[10][7] = {
  46. 1, 1, 1, 0, 1, 1, 1,     // 0
  47. 0, 0, 1, 0, 0, 1, 0,     // 1
  48. 1, 0, 1, 1, 1, 0, 1,     // 2
  49. 1, 0, 1, 1, 0, 1, 1,     // 3
  50. 0, 1, 1, 1, 0, 1, 0,     // 4
  51. 1, 1, 0, 1, 0, 1, 1,     // 5
  52. 1, 1, 0, 1, 1, 1, 1,     // 6
  53. 1, 0, 1, 0, 0, 1, 0,     // 7
  54. 1, 1, 1, 1, 1, 1, 1,     // 8
  55. 1, 1, 1, 1, 0, 1, 1      // 9
  56. };
  57. static POINT ptSegment [7][6] = {
  58. 7,  6,  11,  2,  31,  2,  35,  6,  31, 10,  11, 10,
  59. 6,  7,  10, 11,  10, 31,   6, 35,   2, 31,   2, 11,
  60. 36,  7,  40, 11,  40, 31,  36, 35,  32, 31,  32, 11,
  61. 7, 36,  11, 32,  31, 32,  35, 36,  31, 40,  11, 40,
  62. 6, 37,  10, 41,  10, 61,   6, 65,   2, 61,   2, 41,
  63. 36, 37,  40, 41,  40, 61,  36, 65,  32, 61,  32, 41,
  64. 7, 66,  11, 62,  31, 62,  35, 66,  31, 70,  11, 70
  65. };
  66. int          iSeg;
  67. for (iSeg = 0; iSeg < 7; iSeg++)
  68. if (fSevenSegment[iNumber][iSeg])
  69. Polygon(hdc, ptSegment[iSeg], 6);
  70. }
  71. void DisplayTwoDigits(HDC hdc, int iNumber, BOOL fSuppress)
  72. {
  73. //if (!fSuppress || (iNumber / 10 != 0))
  74. DisplayDigit(hdc, iNumber / 10);
  75. OffsetWindowOrgEx(hdc, -42, 0, NULL);
  76. DisplayDigit(hdc, iNumber % 10);
  77. OffsetWindowOrgEx(hdc, -42, 0, NULL);
  78. }
  79. void DisplayColon(HDC hdc)
  80. {
  81. POINT ptColon[2][4] = { 2,  21,  6,  17,  10, 21,  6, 25,
  82. 2,  51,  6,  47,  10, 51,  6, 55 };
  83. Polygon(hdc, ptColon[0], 4);
  84. Polygon(hdc, ptColon[1], 4);
  85. OffsetWindowOrgEx(hdc, -12, 0, NULL);
  86. }
  87. void DisplayTime(HDC hdc, BOOL f24Hour, BOOL fSuppress)
  88. {
  89. SYSTEMTIME st;
  90. GetLocalTime(&st);
  91. if (f24Hour)
  92. DisplayTwoDigits(hdc, st.wHour, fSuppress);
  93. else
  94. DisplayTwoDigits(hdc, (st.wHour %= 12) ? st.wHour : 12, fSuppress);
  95. DisplayColon(hdc);
  96. DisplayTwoDigits(hdc, st.wMinute, fSuppress);
  97. DisplayColon(hdc);
  98. DisplayTwoDigits(hdc, st.wSecond, fSuppress);
  99. }
  100. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  101. {
  102. static BOOL     f24Hour, fSuppress;
  103. static HBRUSH   hBrushRed;
  104. static int      cxClient, cyClient;
  105. HDC             hdc;
  106. PAINTSTRUCT     ps;
  107. TCHAR           szBuffer[2];
  108. switch (message)
  109. {
  110. case WM_CREATE:
  111. hBrushRed = CreateSolidBrush(RGB(255, 0, 0));
  112. SetTimer(hwnd, ID_TIMER, 1000, NULL);
  113. // fall through
  114. case WM_SETTINGCHANGE:
  115. GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ITIME, szBuffer, 2);
  116. f24Hour = (szBuffer[0] == '1');
  117. GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ITLZERO, szBuffer, 2);
  118. fSuppress = (szBuffer[0] == '0');
  119. InvalidateRect(hwnd, NULL, TRUE);
  120. return 0;
  121. case WM_SIZE:
  122. cxClient = LOWORD(lParam);
  123. cyClient = HIWORD(lParam);
  124. return 0;
  125. case WM_TIMER:
  126. InvalidateRect(hwnd, NULL, TRUE);
  127. return 0;
  128. case WM_PAINT:
  129. hdc = BeginPaint(hwnd, &ps);
  130. SetMapMode(hdc, MM_ISOTROPIC);
  131. SetWindowExtEx(hdc, 276, 72, NULL);
  132. SetViewportExtEx(hdc, cxClient, cyClient, NULL);
  133. SetWindowExtEx(hdc, 138, 36, NULL);
  134. SetViewportExtEx(hdc, cxClient/2, cyClient/2, NULL);
  135. SelectObject(hdc, GetStockObject(NULL_PEN));
  136. SelectObject(hdc, hBrushRed);
  137. DisplayTime(hdc, f24Hour, fSuppress);
  138. EndPaint(hwnd, &ps);
  139. return 0;
  140. case WM_DESTROY:
  141. KillTimer(hwnd, ID_TIMER);
  142. DeleteObject(hBrushRed);
  143. PostQuitMessage (0) ;
  144. return 0 ;
  145. }
  146. return DefWindowProc (hwnd, message, wParam, lParam) ;
  147. }

2)模拟时钟

[cpp]  view plain copy print ?
  1. /*-----------------------------------------------------------
  2. digClock.cpp -- Digital Clock
  3. ------------------------------------------------------------*/
  4. #include <windows.h>
  5. #include <math.h>
  6. #define ID_TIMER    1
  7. #define TWOPI       (2*3.14159)
  8. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  9. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  10. PSTR szCmdLine, int iCmdShow)
  11. {
  12. static TCHAR szAppName[] = TEXT("DigClock");
  13. HWND         hwnd;
  14. MSG          msg;
  15. WNDCLASS     wndclass;
  16. wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  17. wndclass.lpfnWndProc   = WndProc;
  18. wndclass.cbClsExtra    = 0;
  19. wndclass.cbWndExtra    = 0;
  20. wndclass.hInstance     = hInstance;
  21. wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  22. wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  23. wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
  24. wndclass.lpszMenuName  = NULL;
  25. wndclass.lpszClassName = szAppName;
  26. if (!RegisterClass (&wndclass))
  27. {
  28. MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
  29. return 0 ;
  30. }
  31. hwnd = CreateWindow(szAppName, TEXT ("Digital Clock"),
  32. WS_OVERLAPPEDWINDOW,
  33. CW_USEDEFAULT, CW_USEDEFAULT,
  34. CW_USEDEFAULT, CW_USEDEFAULT,
  35. NULL, NULL, hInstance, NULL);
  36. ShowWindow(hwnd, iCmdShow);
  37. UpdateWindow(hwnd);
  38. while (GetMessage(&msg, NULL, 0, 0))
  39. {
  40. TranslateMessage(&msg);
  41. DispatchMessage(&msg);
  42. }
  43. return msg.wParam;
  44. }
  45. void SetIsotropic(HDC hdc, int cxClient, int cyClient)
  46. {
  47. SetMapMode(hdc, MM_ISOTROPIC);
  48. SetWindowExtEx(hdc, 1000, 1000, NULL);
  49. SetViewportExtEx(hdc, cxClient/2, -cyClient/2, NULL);
  50. SetViewportOrgEx(hdc, cxClient/2,  cyClient/2, NULL);
  51. }
  52. void RotatePoint(POINT pt[], int iNum, int iAngle)
  53. {
  54. int         i;
  55. POINT       ptTemp;
  56. for (i = 0; i < iNum; i++)
  57. {
  58. ptTemp.x = (int)( pt[i].x * cos(TWOPI*iAngle/360) + pt[i].y * sin(TWOPI*iAngle/360) );
  59. ptTemp.y = (int)( pt[i].y * cos(TWOPI*iAngle/360) + pt[i].x * sin(TWOPI*iAngle/360) );
  60. pt[i] = ptTemp;
  61. }
  62. }
  63. void DrawClock(HDC hdc)
  64. {
  65. int     iAngle;
  66. POINT   pt[3];
  67. for (iAngle = 0; iAngle < 360; iAngle += 6)
  68. {
  69. pt[0].x = 0;
  70. pt[0].y = 900;
  71. RotatePoint(pt, 1, iAngle);
  72. pt[2].x = pt[2].y = iAngle%5 ? 33 : 100;
  73. pt[0].x -= pt[2].x / 2;
  74. pt[0].y -= pt[2].y / 2;
  75. pt[1].x = pt[0].x + pt[2].x;
  76. pt[1].y = pt[0].y + pt[2].y;
  77. SelectObject(hdc, GetStockObject(BLACK_BRUSH));
  78. Ellipse(hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
  79. }
  80. }
  81. void DrawHands(HDC hdc, SYSTEMTIME* pst, BOOL fChange)
  82. {
  83. static POINT pt[3][5] = {
  84. 0, -150, 100, 0, 0, 600, -100, 0, 0, -150,
  85. 0, -200,  50, 0, 0, 800,  -50, 0, 0, -200,
  86. 0,    0,   0, 0, 0,   0,    0, 0, 0,  800
  87. };
  88. int         i, iAngle[3];
  89. POINT       ptTemp[3][5];
  90. iAngle[0] = (pst->wHour * 30) % 360 + pst->wMinute/2;
  91. iAngle[1] = pst->wMinute * 6;
  92. iAngle[2] = pst->wSecond * 6;
  93. memcpy(ptTemp, pt, sizeof(pt));
  94. for (i = fChange ? 0 : 2; i < 3; i++)
  95. {
  96. RotatePoint(ptTemp[i], 5, iAngle[i]);
  97. Polyline(hdc, ptTemp[i], 5);
  98. }
  99. }
  100. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  101. {
  102. static SYSTEMTIME   stPrevious;
  103. static int          cxClient, cyClient;
  104. BOOL                fChange;
  105. HDC                 hdc;
  106. PAINTSTRUCT         ps;
  107. SYSTEMTIME          st;
  108. switch (message)
  109. {
  110. case WM_CREATE:
  111. SetTimer(hwnd, ID_TIMER, 1000, NULL);
  112. GetLocalTime(&st);
  113. stPrevious = st;
  114. return 0;
  115. case WM_SIZE:
  116. cxClient = LOWORD(lParam);
  117. cyClient = HIWORD(lParam);
  118. return 0;
  119. case WM_TIMER:
  120. GetLocalTime(&st);
  121. fChange = st.wHour != stPrevious.wHour ||
  122. st.wMinute  != stPrevious.wHour;
  123. hdc = GetDC(hwnd);
  124. SetIsotropic(hdc, cxClient, cyClient);
  125. SelectObject(hdc, GetStockObject(WHITE_PEN));
  126. DrawHands(hdc, &stPrevious, fChange);
  127. SelectObject(hdc, GetStockObject(BLACK_PEN));
  128. DrawHands(hdc, &st, TRUE);
  129. ReleaseDC(hwnd, hdc);
  130. stPrevious = st;
  131. return 0;
  132. case WM_PAINT:
  133. hdc = BeginPaint(hwnd, &ps);
  134. SetIsotropic(hdc, cxClient, cyClient);
  135. DrawClock(hdc);
  136. DrawHands(hdc, &stPrevious, TRUE);
  137. EndPaint(hwnd, &ps);
  138. return 0;
  139. case WM_DESTROY:
  140. KillTimer(hwnd, ID_TIMER);
  141. PostQuitMessage (0) ;
  142. return 0 ;
  143. }
  144. return DefWindowProc (hwnd, message, wParam, lParam) ;
  145. }

4.在状态报告中使用计时器

[cpp]  view plain copy print ?
  1. /*-----------------------------------------------------------
  2. whatclr.cpp -- Displays Color Under Cursor
  3. ------------------------------------------------------------*/
  4. #include <windows.h>
  5. #define ID_TIMER    1
  6. void FindWindowSize(int* , int* );
  7. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  8. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  9. PSTR szCmdLine, int iCmdShow)
  10. {
  11. static TCHAR szAppName[] = TEXT("WhatClr");
  12. HWND         hwnd;
  13. MSG          msg;
  14. WNDCLASS     wndclass;
  15. wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  16. wndclass.lpfnWndProc   = WndProc;
  17. wndclass.cbClsExtra    = 0;
  18. wndclass.cbWndExtra    = 0;
  19. wndclass.hInstance     = hInstance;
  20. wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  21. wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  22. wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
  23. wndclass.lpszMenuName  = NULL;
  24. wndclass.lpszClassName = szAppName;
  25. if (!RegisterClass (&wndclass))
  26. {
  27. MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
  28. return 0 ;
  29. }
  30. hwnd = CreateWindow(szAppName, TEXT ("What Color"),
  31. WS_OVERLAPPEDWINDOW,
  32. CW_USEDEFAULT, CW_USEDEFAULT,
  33. CW_USEDEFAULT, CW_USEDEFAULT,
  34. NULL, NULL, hInstance, NULL);
  35. ShowWindow(hwnd, iCmdShow);
  36. UpdateWindow(hwnd);
  37. while (GetMessage(&msg, NULL, 0, 0))
  38. {
  39. TranslateMessage(&msg);
  40. DispatchMessage(&msg);
  41. }
  42. return msg.wParam;
  43. }
  44. void FindWindowSize(int* pcxWindow, int* pcyWindow)
  45. {
  46. HDC         hdcScreen;
  47. TEXTMETRIC  tm;
  48. hdcScreen = CreateIC(TEXT("DISPLAY"), NULL, NULL, NULL);
  49. GetTextMetrics(hdcScreen, &tm);
  50. DeleteDC(hdcScreen);
  51. *pcxWindow = 2 * GetSystemMetrics(SM_CXBORDER) + 12 * tm.tmAveCharWidth;
  52. *pcyWindow = 2 * GetSystemMetrics(SM_CYBORDER) + GetSystemMetrics(SM_CYCAPTION) + 2 * tm.tmHeight;
  53. }
  54. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  55. {
  56. static COLORREF     cr, crLast;
  57. static HDC          hdcScreen;
  58. HDC                 hdc;
  59. PAINTSTRUCT         ps;
  60. POINT               pt;
  61. RECT                rc;
  62. TCHAR               szBuffer[16];
  63. switch (message)
  64. {
  65. case WM_CREATE:
  66. hdcScreen = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
  67. SetTimer(hwnd, ID_TIMER, 100, NULL);
  68. return 0;
  69. case WM_TIMER:
  70. GetCursorPos(&pt);
  71. cr = GetPixel(hdcScreen, pt.x, pt.y);
  72. SetPixel(hdcScreen, pt.x, pt.y, 0);
  73. if (cr != crLast)
  74. {
  75. crLast = cr;
  76. InvalidateRect(hwnd, NULL, FALSE);
  77. }
  78. return 0;
  79. case WM_PAINT:
  80. hdc = BeginPaint(hwnd, &ps);
  81. GetClientRect(hwnd, &rc);
  82. wsprintf(szBuffer, TEXT("   %02X %02X %02X   "), GetRValue(cr), GetGValue(cr), GetBValue(cr));
  83. DrawText(hdc, szBuffer, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  84. EndPaint(hwnd, &ps);
  85. return 0;
  86. case WM_DESTROY:
  87. DeleteDC(hdcScreen);
  88. KillTimer(hwnd, ID_TIMER);
  89. PostQuitMessage (0);
  90. return 0;
  91. }
  92. return DefWindowProc (hwnd, message, wParam, lParam);
  93. }

Win32 计时器消息 - WM_TIMER,SetTimer相关推荐

  1. win32 ——定时器消息 小程序:打印时间

    //定时器消息 //设置了定时器之后,操作系统每隔一段相同的时间就产生WM_TIMER消息//原型 UINT_PTR SetTimer(HWND hWnd, //窗口句柄UINT_PTR nIDEve ...

  2. win32 api 消息解释

    //wMsg参数常量值:         //WM_KEYDOWN 按下一个键         public static int WM_KEYDOWN = 0x0100;         //释放一 ...

  3. Win32 API消息函数:GetMessagePos

    函数功能:该函数返回表示屏幕坐标下光标位置的长整数值.此位置表示当上一消息由GetMessage取得时鼠标占用的点. 函数原型:DWORD GetMessagePos(VOID) 参数:无. 返回值: ...

  4. Win32 鼠标消息 - 客户区鼠标消息、非客户区鼠标消息、击中测试、鼠标滚轮

    注:以下内容为学习笔记,多数是从书本.资料中得来,只为加深印象,及日后参考.然而本人表达能力较差,写的不好.因非翻译.非转载,只好选原创,但多数乃摘抄,实为惭愧.但若能帮助一二访客,幸甚! 注:以下内 ...

  5. MFC 消息中( WPARAM wParam,LPARAM lParam)包含信息

    windows的消息具有以下两个参数: (1)字参数(wParam) (2)长参数(lParam) 字参数和长参数都是32位整数,用于提供消息的附带消息,是消息传递过程中参数的载体.附加信息的消息号取 ...

  6. wParam和lParam消息

    1 WM_PAINT消息,LOWORD(lParam)是客户区的宽,HIWORD(lParam)是客户区的高       2 滚动条WM_VSCROLL或WM_HSCROLL消息,LOWORD(wPa ...

  7. windows编程中wParam和lParam消息

    windows编程中wParam和lParam消息 1.WM_PAINT消息,LOWORD(lParam)是客户区的宽,HIWORD(lParam)是客户区的高. 2 滚动条WM_VSCROLL或WM ...

  8. 一些WM消息与其相应WPARAM,LPARAM的定义

    以下内容首发在我的百度空间:http://hi.baidu.com/legend_sss 1. WM_PAINT,WM_SIZE: LOWORD(lParam)是客户区的宽 HIWORD(lParam ...

  9. 常用的各种消息下wParam及lParam值的含义

    01.WM_PAINT消息 LOWORD(lParam)是客户区的宽,HIWORD(lParam)是客户区的高 02.滚动条WM_VSCROLL或WM_HSCROLL消息 LOWORD(wParam) ...

最新文章

  1. 关键词热度分析工具_干货分享丨关键词热度分析工具
  2. java解非线性方程组_Scipy - 非线性方程组的所有解
  3. JAX-RS开发 hello world
  4. Nodejs实现WebSocket通信demo
  5. 【洛谷P1632】点的移动
  6. discuz php源码,Discuz7 php源码,该如何解决
  7. shell 命令自动识别系统升级内核、系统索引更新
  8. linux调用堆栈函数,使用 backtrace 获得 Linux 函数调用栈
  9. Jquery第二章常用方法,一二级菜单淡入淡出,event事件,复选框的全选反选第一节
  10. excel筛选排序从小到大_excel筛选怎么用教程 重复数据多个条件筛选功能教学
  11. JaxWsProxyFactoryBean调用WebService实例
  12. 如何将照片中的文字转变成可编辑的文章(如何将书上的文字转变为可编辑的文字)
  13. oracle怎么看日志文件,Oracle日志文件的管理与查看
  14. 数学----向量点积公式推导
  15. 自同步如果服务器删掉文件,linux服务器上ftp删掉的文件能找回
  16. 大型超市计算机管理系统论文,大型超市进销存管理系统的设计与实现
  17. 金仓数据库 KingbaseES插件参考手册 F
  18. 莫纳什大学计算机专业研究生在哪个校区,2020年莫纳什大学信息技术学院具体地址及在哪个校区...
  19. 一起学习如何使用Photoshop绘制像素图片
  20. Thinkphp 6.0请求对象和信息

热门文章

  1. 2022施工员-市政方向-通用基础(施工员)考题及答案
  2. flash cs5 书写代码的好习惯
  3. JetBrains旗下软件通用激活方法
  4. VALSE2023-内容总结(正在更新)
  5. Android实战开发--小慕笔记UI设计(Fragment布局的使用)
  6. IPOPT工具解决非线性规划最优化问题使用案例
  7. 基于vue的高仿网易云音乐网站,实现大多数功能
  8. 浏览器兼容性测试问题
  9. MySQL(四)完整性约束
  10. Cadence PCB仿真使用Allegro PCB SI生成反射仿真报告及报告导读图文教程