📄 demo3_4.c
字号:
1 /****************************************************************/
2 /* Demo3_4 --- The test of ISOTROPIC MapMode */
3 /****************************************************************/
4
5 #include <windows.h>
6 #include <math.h>
7 #include "demo3_4.h"
8
9 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
10 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
11
12 void DrawGraph(HDC, int, int, POINT);
13 void DrawResult(HDC, int, int, int, POINT);
14
15
16 /****************************************************************/
17 /* WinMain() */
18 /****************************************************************/
19
20 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
21 LPSTR lpszCmdLine, int nCmdShow)
22 {
23 WNDCLASS wclass;
24 MSG msg;
25 HWND hWnd;
26 char szName[] = "Demo3_4";
27
28 if (!hPrevInstance)
29 {
30 wclass.style = CS_HREDRAW | CS_VREDRAW;
31 wclass.lpfnWndProc = MainWndProc;
32 wclass.cbClsExtra = 0;
33 wclass.cbWndExtra = 0;
34 wclass.hInstance = hInstance;
35 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
36 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
37 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
38 wclass.lpszMenuName = szName;
39 wclass.lpszClassName = szName;
40
41 if (!RegisterClass (&wclass))
42 return (FALSE);
43 }
44
45 hWnd = CreateWindow(
46 szName,
47 "Curve Operation" ,
48 WS_OVERLAPPEDWINDOW,
49 CW_USEDEFAULT,
50 CW_USEDEFAULT,
51 CW_USEDEFAULT,
52 CW_USEDEFAULT,
53 NULL,
54 NULL,
55 hInstance,
56 NULL );
57
58 if (!hWnd)
59 return (FALSE);
60
61 ShowWindow(hWnd, nCmdShow);
62 UpdateWindow(hWnd);
63
64 while (GetMessage(&msg, NULL, NULL,NULL))
65 {
66 TranslateMessage(&msg);
67 DispatchMessage(&msg);
68 }
69 return (msg.wParam);
70 }
71
72
73
74 /****************************************************************/
75 /* MainWndProc() */
76 /****************************************************************/
77
78 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
79 WORD wParam, LONG lParam)
80 {
81 HDC hDC;
82 HMENU hMenu;
83 PAINTSTRUCT ps;
84 static int GraphID_1 = IDM_SIN1;
85 static int GraphID_2 = IDM_COS2;
86 static int OpID = IDM_ADD;
87 RECT Rect;
88 static RECT Rect1, Rect2, Rect3;
89 static POINT Client;
90 BOOL GraphChange = FALSE;
91 BOOL ResultChange = FALSE;
92
93 switch (message)
94 {
95 case WM_CREATE :
96 hMenu = GetMenu(hWnd);
97 CheckMenuItem(hMenu, IDM_SIN1, MF_CHECKED);
98 CheckMenuItem(hMenu, IDM_COS2, MF_CHECKED);
99 CheckMenuItem(hMenu, IDM_ADD, MF_CHECKED);
100 return (0);
101
102 case WM_COMMAND :
103 hMenu = GetMenu(hWnd);
104 switch (wParam)
105 {
106 case IDM_SIN1 :
107 case IDM_COS1 :
108
109 if (GraphID_1 == wParam)
110 return (0);
111
112 CheckMenuItem(hMenu, GraphID_1,
113 MF_UNCHECKED);
114 GraphID_1 = wParam;
115 CheckMenuItem(hMenu, GraphID_1,
116 MF_CHECKED);
117
118 InvalidateRect(hWnd, &Rect1, TRUE);
119 break;
120
121 case IDM_SIN2 :
122 case IDM_COS2 :
123
124 if (GraphID_2 == wParam)
125 return (0);
126
127 CheckMenuItem(hMenu, GraphID_2,
128 MF_UNCHECKED);
129 GraphID_2 = wParam;
130 CheckMenuItem(hMenu, GraphID_2,
131 MF_CHECKED);
132
133 InvalidateRect(hWnd, &Rect2, TRUE);
134 break;
135
136 case IDM_ADD :
137 case IDM_SUB1 :
138 case IDM_SUB2 :
139
140 if (OpID == wParam)
141 return (0);
142
143 CheckMenuItem(hMenu, OpID,
144 MF_UNCHECKED);
145 OpID = wParam;
146 CheckMenuItem(hMenu, OpID,
147 MF_CHECKED);
148
149 InvalidateRect(hWnd, &Rect3, TRUE);
150 break;
151 }
152 return (0);
153
154 case WM_SIZE :
155
156 Client.x = LOWORD(lParam);
157 Client.y = HIWORD(lParam);
158
159 Rect1.left = 0;
160 Rect1.top = 0;
161 Rect1.right = Client.x/2;
162 Rect1.bottom = Client.y/2;
163
164 Rect2.left = 0;
165 Rect2.top = Client.y/2;
166 Rect2.right = Client.x/2;
167 Rect2.bottom = Client.y;
168
169 Rect3.left = Client.x/2;
170 Rect3.top = 0;
171 Rect3.right = Client.x;
172 Rect3.bottom = Client.y;
173
174 return (0);
175
176 case WM_PAINT :
177 hDC = BeginPaint(hWnd, &ps);
178
179 SetMapMode(hDC, MM_ISOTROPIC);
180
181 SetWindowExt(hDC, 628, 628);
182 SetViewportExt(hDC, Client.x, -Client.y);
183
184 /* Draw the Graph 1 */
185 IntersectRect(&Rect, &Rect1, &ps.rcPaint);
186 if (! IsRectEmpty(&Rect))
187 {
188 DrawGraph(hDC, 1, GraphID_1, Client);
189 GraphChange = TRUE;
190 }
191
192 /* Draw the Graph 2 */
193 IntersectRect(&Rect, &Rect2, &ps.rcPaint);
194 if (! IsRectEmpty(&Rect))
195 {
196 DrawGraph(hDC, 2, GraphID_2, Client);
197 GraphChange = TRUE;
198 }
199
200 /* Draw the result of */
201 /* Graph1 op Graph2 */
202 IntersectRect(&Rect, &Rect3, &ps.rcPaint);
203 if (! IsRectEmpty(&Rect))
204 { DrawResult(hDC, GraphID_1, GraphID_2,
205 OpID, Client);
206 ResultChange = TRUE;
207 }
208
209 EndPaint(hWnd, &ps);
210 if (GraphChange && !ResultChange)
211 InvalidateRect(hWnd, &Rect3, TRUE);
212 return (0);
213
214 case WM_DESTROY :
215 PostQuitMessage(0);
216 return (0);
217
218 default :
219 return(DefWindowProc(hWnd, message, wParam, lParam));
220 }
221 }
222
223
224
225 void DrawGraph(HDC hDC, int Index, int GraphID, POINT Client)
226 {
227 int i;
228 DWORD rgbColor;
229 double Rad;
230 int Val;
231 int Width;
232
233 SaveDC(hDC);
234
235 if (Client.x < Client.y)
236 Width = Client.x;
237 else
238 Width = Client.y;
239
240 if (Index == 1)
241 SetViewportOrg(hDC, Client.x/2-Width/4, Client.y/2-Width/4);
242 else
243 SetViewportOrg(hDC, Client.x/2-Width/4, Client.y/2+Width/4);
244
245 MoveTo(hDC, -628/6, 0);
246 LineTo(hDC, 628/6, 0);
247 MoveTo(hDC, 0, -628/6);
248 LineTo(hDC, 0, 628/6);
249
250 rgbColor = RGB(0, 0, 0);
251 if (GraphID == IDM_SIN1 || GraphID == IDM_SIN2)
252 {
253 for (i=-628; i<=628; i++)
254 {
255 Rad = (double) i/100;
256 Val = sin(Rad)*100;
257 SetPixel(hDC, i/6, Val, rgbColor);
258 }
259 }
260 else
261 {
262 for (i=-628; i<=628; i++)
263 {
264 Rad = (double) i/100;
265 Val = cos(Rad)*100;
266 SetPixel(hDC, i/6, Val, rgbColor);
267 }
268 }
269
270 RestoreDC(hDC, -1);
271 }
272
273
274
275 void DrawResult(HDC hDC, int GraphID_1, int GraphID_2,
276 int OpID, POINT Client)
277 {
278 int i;
279 DWORD rgbColor;
280 double Rad;
281 int Val;
282 double (*Func1)(double), (*Func2)(double);
283 int Index1, Index2;
284 int Width;
285
286 SaveDC(hDC);
287
288 if (Client.x < Client.y)
289 Width = Client.x;
290 else
291 Width = Client.y;
292
293 SetViewportOrg(hDC, Client.x/2+Width/4, Client.y/2);
294
295 MoveTo(hDC, -628/6, 0);
296 LineTo(hDC, 628/6, 0);
297 MoveTo(hDC, 0, -628/3);
298 LineTo(hDC, 0, 628/3);
299
300 if (GraphID_1 == IDM_SIN1)
301 Func1 = sin;
302 else
303 Func1 = cos;
304
305 if (GraphID_2 == IDM_SIN2)
306 Func2 = sin;
307 else
308 Func2 = cos;
309
310 rgbColor = RGB(0, 0, 0);
311
312 switch (OpID)
313 {
314 case IDM_ADD :
315 for (i=-628; i<=628; i++)
316 {
317 Rad = (double) i/100;
318 Val = (Func1(Rad) + Func2(Rad)) * 100;
319 SetPixel(hDC, i/6, Val, rgbColor);
320 }
321 break;
322
323 case IDM_SUB1 :
324 for (i=-628; i<=628; i++)
325 {
326 Rad = (double) i/100;
327 Val = (Func1(Rad) - Func2(Rad)) * 100;
328 SetPixel(hDC, i/6, Val, rgbColor);
329 }
330 break;
331
332 case IDM_SUB2 :
333 for (i=-628; i<=628; i++)
334 {
335 Rad = (double) i/100;
336 Val = (Func2(Rad) - Func1(Rad)) * 100;
337 SetPixel(hDC, i/6, Val, rgbColor);
338 }
339 break;
340 }
341
342 RestoreDC(hDC, -1);
343 }
344
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -