欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

第五章 绘图基础(SINEWAVE)

程序员文章站 2022-06-26 20:42:58
1 //SINEWAVE.C -- Sine Wave Using Polyline (c) Charles Petzold, 1998 2 #include 3 #include 4 5 #define NUM 1000 6 #define TWOPI ( ......
第五章 绘图基础(SINEWAVE)
 1 //SINEWAVE.C -- Sine Wave Using Polyline (c) Charles Petzold, 1998
 2 #include <Windows.h>
 3 #include <math.h>
 4 
 5 #define NUM        1000
 6 #define TWOPI    (2 * 3.14159)
 7 
 8 LRESULT CALLBACK WndPorc(HWND, UINT, WPARAM, LPARAM);
 9 
10 int WINAPI WinMain( __in HINSTANCE hInstance
11                     , __in_opt HINSTANCE hPrevInstance
12                     , __in LPSTR lpCmdLine
13                     , __in int nShowCmd )
14 {
15     static TCHAR szAppName[] = TEXT("SineWave");
16     HWND hwnd;
17     MSG msg;
18     WNDCLASS wndclass;
19 
20     wndclass.style = CS_HREDRAW | CS_VREDRAW;
21     wndclass.lpfnWndProc = WndPorc;
22     wndclass.cbClsExtra = 0;
23     wndclass.cbWndExtra = 0;
24     wndclass.hInstance = hInstance;
25     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
26     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
27     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
28     wndclass.lpszMenuName = NULL;
29     wndclass.lpszClassName = szAppName;
30 
31     if (!RegisterClass(&wndclass))
32     {
33         MessageBox(NULL, TEXT("Program requires Windows NT!")
34             , szAppName, MB_ICONERROR);
35         return 0;
36     }
37 
38     hwnd = CreateWindow(szAppName, TEXT("Sine Wave Using Polyline")
39         , WS_OVERLAPPEDWINDOW
40         , CW_USEDEFAULT, CW_USEDEFAULT
41         , CW_USEDEFAULT, CW_USEDEFAULT
42         , NULL, NULL, hInstance, NULL);
43 
44     ShowWindow(hwnd, nShowCmd);
45     UpdateWindow(hwnd);
46 
47     while (GetMessage(&msg, NULL, 0, 0))
48     {
49         TranslateMessage(&msg);
50         DispatchMessage(&msg);
51     }
52 
53     return msg.wParam;
54 }
55 
56 LRESULT CALLBACK WndPorc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
57 {
58     static int cxClient, cyClient;
59     HDC hdc;
60     int i;
61     PAINTSTRUCT ps;
62     POINT apt[NUM];
63 
64     switch (message)
65     {
66     case WM_SIZE:
67         cxClient = LOWORD(lParam);
68         cyClient = HIWORD(lParam);
69         return 0;
70 
71     case WM_PAINT:
72         hdc = BeginPaint(hwnd, &ps);
73         
74         MoveToEx(hdc, 0, cyClient / 2, NULL);
75         LineTo(hdc, cxClient, cyClient / 2);
76         
77         for (i = 0; i != NUM; ++i)
78         {
79             apt[i].x = i * cxClient / NUM;
80             apt[i].y = (int)(cyClient / 2 * (1 - sin(TWOPI * i / NUM)));
81         }
82 
83         Polyline(hdc, apt, NUM);
84         EndPaint(hwnd, &ps);
85         return 0;
86 
87     case WM_DESTROY:
88         PostQuitMessage(0);
89         return 0;
90     }
91 
92     return DefWindowProc(hwnd, message, wParam, lParam);
93 }
SINEWAVE.C

SINEWAVE显示结果

第五章 绘图基础(SINEWAVE)