📄 sinewave.cpp
字号:
#include <windows.h>
#include <math.h>
#define SEGMENTS 500 // 取的点数(在一个周期内取500个点)
#define PI 3.1415926 // 圆周率
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc;
MSG msg;
HWND hWnd;
// Register application main window class
wc.style = CS_HREDRAW | CS_VREDRAW; // Window style
wc.lpfnWndProc = MainWndProc; // Callback function
wc.cbClsExtra = 0; // Extra class data
wc.cbWndExtra = 0; // Extra window data
wc.hInstance = hInstance; // Owner handle
wc.hIcon = NULL, // Application icon
wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = TEXT("SineWave"); // Window class name
if (RegisterClass (&wc) == 0) return -1;
// Create main window.
hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE, // Ex style flags
TEXT("SineWave"), // Window class
TEXT("SineWave"), // Window title
// Style flags
WS_VISIBLE | WS_CAPTION | WS_SYSMENU,
CW_USEDEFAULT, // x position
CW_USEDEFAULT, // y position
CW_USEDEFAULT, // Initial width
CW_USEDEFAULT, // Initial height
NULL, // Parent
NULL, // Menu, must be null
hInstance, // Application instance
NULL); // Pointer to create
// parameters
if (!IsWindow (hWnd)) return -2; // Fail code if not created.
// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// Instance cleanup
return 1;
}
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
RECT rt;
HDC hdc;
int cxClient, cyClient;
POINT pt[SEGMENTS];
int i;
switch(wMsg)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt);
cxClient = rt.right - rt.left;
cyClient = rt.bottom - rt.top;
// 画横坐标轴
MoveToEx(hdc, 0, cyClient/2, NULL);
LineTo(hdc, cxClient, cyClient/2);
// 找出500个点的坐标
for(i=0; i<SEGMENTS; i++)
{
pt[i].x = cxClient*i/SEGMENTS;
pt[i].y = (int)((cyClient/2)*(1 - sin(2*PI*i/SEGMENTS)));
}
// 将各点连在一起
Polyline(hdc, pt, SEGMENTS);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, wMsg, wParam, lParam);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -