1 /***************************
2 *
3 * 程序名称 : 模拟正弦波
4 * 作 者 : doodle777
5 * 版 本 : 1.1
6 * 日 期 : 2012-10-19
7 * 说 明 : 1.1--解决了闪屏问题
8 *
9 ***************************/
10
11 #include<Windows.h>
12 #include<math.h>
13
14 #define NUM 1000
15 #define PI 3.14159
16
17 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ;
18 void Draw(HWND, float, float) ;
19
20 static int cxClient, cyClient, xOffset, yOffset;
21 POINT apt[NUM] ;
22 static int A = 1000 ;
23 static float w = 20, g = 0 ;
24
25 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
26 {
27 static TCHAR szAppName[] = TEXT("模拟正弦波") ;
28 HWND hwnd ;
29 MSG msg ;
30 WNDCLASS wndclass ;
31
32 wndclass.cbClsExtra = NULL ;
33 wndclass.cbWndExtra = NULL ;
34 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH) ;
35 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW) ;
36 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION) ;
37 wndclass.hInstance = hInstance ;
38 wndclass.lpfnWndProc = WndProc ;
39 wndclass.lpszClassName = szAppName ;
40 wndclass.lpszMenuName = NULL ;
41 wndclass.style = CS_HREDRAW | CS_VREDRAW ;
42
43 if(!RegisterClass(&wndclass))
44 {
45 MessageBox(NULL, TEXT("This Program Requires Windows NT !"), szAppName, MB_ICONERROR) ;
46 return 0 ;
47 }
48
49 hwnd = CreateWindow(szAppName, TEXT("模拟正弦波(上下左右控制)"), WS_OVERLAPPEDWINDOW,
50 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,
51 NULL, NULL, hInstance, NULL) ;
52 ShowWindow(hwnd, iCmdShow) ;
53 UpdateWindow(hwnd) ;
54
55 while(TRUE)
56 {
57 if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
58 {
59 if(msg.message == WM_QUIT)
60 break ;
61 TranslateMessage(&msg) ;
62 DispatchMessage(&msg) ;
63 }
64 else
65 {
66 Draw(hwnd, w, g) ;
67 // w += 1 ;
68 // g += PI / 6 ;
69
70 }
71 }
72 return msg.wParam ;
73 }
74
75 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
76 {
77 RECT rect ;
78
79 rect.bottom = cyClient ;
80 rect.left = 0 ;
81 rect.right = cxClient ;
82 rect.top = 0 ;
83
84 switch(message)
85 {
86 case WM_SIZE :
87 cxClient = LOWORD(lParam) ;
88 cyClient = HIWORD(lParam) ;
89 xOffset = cxClient / 2 ;
90 yOffset = cyClient / 2 ;
91 A = cyClient / 2 ;
92
93 return 0 ;
94
95 case WM_KEYDOWN :
96 InvalidateRect(hwnd, &rect, TRUE) ;
97 switch(wParam)
98 {
99 case VK_UP :
100 g += PI / 6 ;
101 break ;
102 case VK_DOWN :
103 g -= PI / 6 ;
104 break ;
105 case VK_LEFT :
106 w -= 1 ;
107 break ;
108 case VK_RIGHT :
109 w += 1 ;
110 break ;
111 }
112 return 0 ;
113
114
115 case WM_DESTROY :
116 PostQuitMessage(0) ;
117 return 0 ;
118 }
119 return DefWindowProc(hwnd, message, wParam, lParam) ;
120 }
121
122 void Draw(HWND hwnd, float w, float g)
123 {
124 HDC hdc ;
125 PAINTSTRUCT ps ;
126 int t ;
127
128 hdc = GetDC(hwnd) ;
129
130 MoveToEx(hdc, 0, yOffset, NULL) ;
131 LineTo(hdc, cxClient, yOffset) ;
132 MoveToEx(hdc, xOffset, 0, NULL) ;
133 LineTo(hdc, xOffset, cyClient) ;
134
135 apt[0].x = 0 ;
136 apt[0].y = yOffset + A * sin(g) ;
137 MoveToEx(hdc, apt[0].x, apt[0].y, NULL) ;
138 for(t = 1; t < NUM ; t++)
139 {
140 apt[t].x = cxClient * t / NUM ;
141 apt[t].y = yOffset + A * sin(w * t / NUM + g ) ;
142 LineTo(hdc, apt[t].x, apt[t].y) ;
143 MoveToEx(hdc, apt[t].x, apt[t].y, NULL) ;
144 }
145
146 ReleaseDC(hwnd, hdc) ;
147 }