1 #include<Windows.h> 2 #include<math.h> 3 4 #define NUM 1000 5 #define PI 3.14159 6 7 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ; 8 void Draw(HWND) ; 9 10 static int cxClient, cyClient, xOffset, yOffset; 11 POINT apt[NUM] ; 12 static int A = 100 ; 13 14 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) 15 { 16 static TCHAR szAppName[] = TEXT("谐振动合成") ; 17 HWND hwnd ; 18 MSG msg ; 19 WNDCLASS wndclass ; 20 21 wndclass.cbClsExtra = NULL ; 22 wndclass.cbWndExtra = NULL ; 23 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH) ; 24 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW) ; 25 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION) ; 26 wndclass.hInstance = hInstance ; 27 wndclass.lpfnWndProc = WndProc ; 28 wndclass.lpszClassName = szAppName ; 29 wndclass.lpszMenuName = NULL ; 30 wndclass.style = CS_HREDRAW | CS_VREDRAW ; 31 32 if(!RegisterClass(&wndclass)) 33 { 34 MessageBox(NULL, TEXT("This Program Requires Windows NT !"), szAppName, MB_ICONERROR) ; 35 return 0 ; 36 } 37 38 hwnd = CreateWindow(szAppName, TEXT("谐振动合成"), WS_OVERLAPPEDWINDOW, 39 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, 40 NULL, NULL, hInstance, NULL) ; 41 ShowWindow(hwnd, iCmdShow) ; 42 UpdateWindow(hwnd) ; 43 44 while(TRUE) 45 { 46 if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 47 { 48 if(msg.message == WM_QUIT) 49 break ; 50 TranslateMessage(&msg) ; 51 DispatchMessage(&msg) ; 52 } 53 else 54 { 55 Draw(hwnd) ; 56 } 57 } 58 return msg.wParam ; 59 } 60 61 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 62 { 63 64 switch(message) 65 { 66 case WM_SIZE : 67 cxClient = LOWORD(lParam) ; 68 cyClient = HIWORD(lParam) ; 69 xOffset = cxClient / 2 ; 70 yOffset = cyClient / 2 ; 71 A = xOffset < yOffset ? xOffset : yOffset ; 72 73 return 0 ; 74 75 case WM_DESTROY : 76 PostQuitMessage(0) ; 77 return 0 ; 78 } 79 return DefWindowProc(hwnd, message, wParam, lParam) ; 80 } 81 82 void Draw(HWND hwnd) 83 { 84 HDC hdc ; 85 PAINTSTRUCT ps ; 86 RECT rect ; 87 int t ; 88 static double w1 = 4, w2 = 5 ; // w1 : w2 = 1 : 2 89 static double g = PI / 2 ; // 初相位之差 90 91 hdc = GetDC(hwnd) ; 92 93 apt[0].x = xOffset ; 94 apt[0].y = yOffset + A * sin(g) ; 95 MoveToEx(hdc, apt[0].x, apt[0].y, NULL) ; 96 for(t = 1; t < NUM ; t++) 97 { 98 apt[t].x = xOffset + A * sin(t * 2 * w1 * w2 * PI /( NUM * w1 )); 99 apt[t].y = yOffset + A * sin(t * 2 * w1 * w2 * PI /( NUM * w2 ) + g ) ; 100 LineTo(hdc, apt[t].x, apt[t].y) ; 101 MoveToEx(hdc, apt[t].x, apt[t].y, NULL) ; 102 } 103 // Polyline(hdc, apt, NUM ) ; 104 105 rect.bottom = cyClient ; 106 rect.left = 0 ; 107 rect.right = cxClient ; 108 rect.top = 0 ; 109 110 // Sleep(1000) ; 111 // w1 ++ ; 112 // w2 ++ ; 113 // g += PI / 4 ; 114 // InvalidateRect(hwnd, &rect, TRUE) ; 115 116 ReleaseDC(hwnd, hdc) ; 117 }