搜索
查看: 248|回复: 0
打印 上一主题 下一主题

我想知道我哪里弄错了

[复制链接]
跳转到指定楼层
楼主
 楼主| 发表于 2020-12-20 14:09:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
5啊哈币
  1. #include<windows.h>

  2. #include<math.h>

  3. #define ID_TIMER 1

  4. #define TWOPI (2*3.14159)

  5. LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

  6. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int iCmdShow)

  7. {

  8. static TCHAR szAppName[]=TEXT("Clock");

  9. HWND hwnd;

  10. MSG msg;

  11. WNDCLASS wndclass;

  12. wndclass.cbClsExtra=0;

  13. wndclass.cbWndExtra=0;

  14. wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

  15. wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);

  16. wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);

  17. wndclass.hInstance=hInstance;

  18. wndclass.lpfnWndProc=WndProc;

  19. wndclass.lpszClassName=szAppName;

  20. wndclass.lpszMenuName=NULL;

  21. wndclass.style=CS_HREDRAW|CS_VREDRAW;

  22. if(!RegisterClass(&wndclass))

  23. {

  24. MessageBox(NULL,TEXT("This program requires Windows T"),szAppName,MB_ICONERROR);

  25. return 0;

  26. }

  27. hwnd=CreateWindow(szAppName,TEXT("Analog Clock"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

  28. ShowWindow(hwnd,iCmdShow);

  29. UpdateWindow(hwnd);

  30. while(GetMessage(&msg,NULL,0,0))

  31. {

  32. TranslateMessage(&msg);

  33. DispatchMessage(&msg);

  34. }

  35. return msg.wParam;

  36. }

  37. void Setsotropic(HDC hdc,int cxClient,int cyClient)

  38. {

  39. SetMapMode(hdc,MM_ISOTROPIC);

  40. SetWindowExtEx(hdc,1000,1000,NULL);

  41. SetViewportExtEx(hdc,cxClient/2,-cyClient/2,NULL);

  42. SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);

  43. }

  44. void RotatePoint(POINT pt[],int iNum,int iAngle)

  45. {

  46. int i;

  47. POINT ptTemp;

  48. for(i=0;i<iNum;i++)

  49. {

  50. ptTemp.x=(int)(pt[i].x*cos(TWOPI*iAngle/360)+pt[i].y*sin(TWOPI*iAngle/360));

  51. ptTemp.y=(int)(pt[i].y*cos(TWOPI*iAngle/360)+pt[i].x*sin(TWOPI*iAngle/360));

  52. pt[i]=ptTemp;

  53. }

  54. }

  55. void DrawClock(HDC hdc)

  56. {

  57. int iAngle;

  58. POINT pt[3];

  59. for(iAngle=0;iAngle<360;iAngle+=6)

  60. {

  61. pt[0].x=0;

  62. pt[0].y=900;

  63. RotatePoint(pt,1,iAngle);

  64. pt[2].x=pt[2].y=iAngle%5?33:100;

  65. pt[0].x-=pt[2].x/2;

  66. pt[0].y-=pt[2].y/2;

  67. pt[1].x=pt[0].x+pt[2].x;

  68. pt[1].y=pt[0].y+pt[2].y;

  69. SelectObject(hdc,GetStockObject(BLACK_BRUSH));

  70. Ellipse(hdc,pt[0].x,pt[0].y,pt[1].x,pt[1].y );

  71. }

  72. }

  73. void DrawHands(HDC hdc,SYSTEMTIME *pst,BOOL fChange)

  74. {

  75. static POINT pt[3][5]={0,-150,100,0,0,600,-100,0,0,-150, 0,-200,50,0,0,800,-50,0,0,-200, 0,0,0,0,0,0,0,0,0,800 };

  76. int i,iAngle[3];

  77. POINT ptTemp[3][5];

  78. iAngle[0]=(pst->wHour*30)%360+pst->wMinute/2;

  79. iAngle[1]=pst->wMinute*6;

  80. iAngle[2]=pst->wSecond*6;

  81. memcpy(ptTemp,pt,sizeof(pt));

  82. for(i=fChange?0:2;i<3;i++)

  83. {

  84. RotatePoint(ptTemp[i],5,iAngle[i]);

  85. Polyline(hdc,ptTemp[i],5);

  86. }

  87. }

  88. LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)

  89. {

  90. static int cxClient,cyClient;

  91. static SYSTEMTIME stPrevious;

  92. BOOL fChange;

  93. HDC hdc;

  94. PAINTSTRUCT ps;

  95. SYSTEMTIME st;

  96. switch(message)

  97. {

  98. case WM_CREATE:

  99. SetTimer(hwnd,ID_TIMER,1000,NULL);

  100. GetLocalTime(&st);

  101. stPrevious=st;

  102. return 0;

  103. case WM_SIZE:

  104. cxClient=LOWORD(lParam);

  105. cyClient=HIWORD(lParam);

  106. return 0;

  107. case WM_TIMER:

  108. GetLocalTime(&st);

  109. fChange=st.wHour!=stPrevious.wHour||st.wMinute!=stPrevious.wMinute;

  110. hdc=GetDC(hwnd);

  111. Setsotropic(hdc,cxClient,cyClient);

  112. SelectObject(hdc,GetStockObject(WHITE_PEN));

  113. DrawHands(hdc,&stPrevious,fChange);

  114. SelectObject(hdc,GetStockObject(BLACK_PEN));

  115. DrawHands(hdc,&st,TRUE);

  116. stPrevious=st;

  117. return 0;

  118. case WM_PAINT:

  119. hdc=BeginPaint(hwnd,&ps);

  120. Setsotropic(hdc,cxClient,cyClient);

  121. DrawClock(hdc);

  122. DrawHands(hdc,&stPrevious,TRUE);

  123. EndPaint(hwnd,&ps);

  124. return 0;

  125. case WM_DESTROY:

  126. KillTimer(hwnd,ID_TIMER);

  127. PostQuitMessage(0);

  128. return 0;

  129. }
  130. return DefWindowProc(hwnd,message,wParam,lParam);
  131. }
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表