[C] 画几朵花 →→→→→进入此内容的聊天室

来自 , 2020-08-24, 写在 C, 查看 101 次.
URL http://www.code666.cn/view/3e9e39fe
  1. #include <windows.h>
  2. #include <math.h>
  3.  
  4. #define PI      3.14159265
  5.  
  6. LRESULT CALLBACK WindowProcedure ( HWND, UINT, WPARAM, LPARAM );
  7. void OnPaint ( HDC );
  8.  
  9. void DrawLine ( HDC, int, int, int, int );
  10. void DrawFlower ( HDC, int, int, COLORREF );
  11. void DrawTie ( HDC, int, int, COLORREF );
  12.  
  13.  
  14. int WINAPI WinMain ( HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil )
  15. {
  16.         char szClassName[] = TEXT ( "DrawFlower" );
  17.         HWND hwnd;
  18.         MSG msg;
  19.         WNDCLASSEX wc = { 0 };
  20.  
  21.         wc.hInstance = hThisInstance;
  22.         wc.lpszClassName = szClassName;
  23.         wc.lpfnWndProc = WindowProcedure;
  24.         wc.style = CS_DBLCLKS;
  25.         wc.cbSize = sizeof ( WNDCLASSEX );
  26.  
  27.         wc.hIcon = LoadIcon ( NULL, IDI_APPLICATION );
  28.         wc.hIconSm = LoadIcon ( NULL, IDI_APPLICATION );
  29.         wc.hCursor = LoadCursor ( NULL, IDC_ARROW );
  30.         wc.hbrBackground = ( HBRUSH ) ( COLOR_WINDOW+1 );
  31.  
  32.         if ( !RegisterClassEx ( &wc ) ) return 0;
  33.  
  34.         hwnd = CreateWindowEx ( 0, szClassName, szClassName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, HWND_DESKTOP, NULL, hThisInstance, NULL );
  35.         ShowWindow ( hwnd, nFunsterStil );
  36.  
  37.         while ( GetMessage ( &msg, NULL, 0, 0 ) )
  38.         {
  39.                 TranslateMessage ( &msg );
  40.                 DispatchMessage ( &msg );
  41.         }
  42.  
  43.         return msg.wParam;
  44. }
  45.  
  46. LRESULT CALLBACK WindowProcedure ( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
  47. {
  48.         PAINTSTRUCT ps;
  49.         HDC hdc;
  50.  
  51.         switch ( msg )
  52.         {
  53.         case WM_PAINT:
  54.                 hdc = BeginPaint ( hwnd, &ps );
  55.                 OnPaint ( hdc );
  56.                 EndPaint ( hwnd, &ps );
  57.                 break;
  58.  
  59.         case WM_DESTROY:
  60.                 PostQuitMessage ( 0 );
  61.                 break;
  62.  
  63.         default:
  64.                 return DefWindowProc ( hwnd, msg, wParam, lParam );
  65.         }
  66.  
  67.         return 0;
  68. }
  69.  
  70. void OnPaint ( HDC hdc )
  71. {
  72.         HPEN h;
  73.  
  74.         // 画蝴蝶结
  75.         DrawTie ( hdc, 195, 354, RGB ( 255,0,255 ) );
  76.  
  77.         // 画枝干
  78.         h = CreatePen ( PS_SOLID, 1, RGB ( 0,255,0 ) );
  79.         SelectObject ( hdc, h );
  80.  
  81.         DrawLine ( hdc, 189, 372, 180, 400 );
  82.         DrawLine ( hdc, 310, 160, 325,  68 );
  83.         DrawLine ( hdc, 310, 160, 187, 374 );
  84.         DrawLine ( hdc, 150, 140, 189, 374 );
  85.         DrawLine ( hdc, 430, 176, 190, 374 );
  86.         DrawLine ( hdc, 370, 110, 187, 374 );
  87.         DrawLine ( hdc, 250,  72, 189, 372 );
  88.         DrawLine ( hdc, 253, 192, 190, 374 );
  89.         DrawLine ( hdc, 189, 372, 187, 400 );
  90.         DrawLine ( hdc, 189, 372, 182, 400 );
  91.         DrawLine ( hdc, 189, 372, 200, 120 );
  92.  
  93.         DeleteObject ( h );
  94.  
  95.         // 画花朵
  96.         DrawFlower ( hdc, 320, 160, RGB ( 255,0,0 ) );
  97.         DrawFlower ( hdc, 200, 120, RGB ( 0,255,0 ) );
  98.         DrawFlower ( hdc, 150, 140, RGB ( 0,0,255 ) );
  99.         DrawFlower ( hdc, 430, 176, RGB ( 255, 127, 0 ) );
  100.         DrawFlower ( hdc, 370, 110, RGB ( 239, 179, 52 ) );
  101.         DrawFlower ( hdc, 250,  72, RGB ( 235, 95, 186 ) );
  102.         DrawFlower ( hdc, 325,  68, RGB ( 228, 119, 98 ) );
  103.         DrawFlower ( hdc, 253, 190, RGB ( 247, 169, 117 ) );
  104. }
  105.  
  106. // 画花朵
  107. void DrawFlower ( HDC hdc, int x, int y, COLORREF c )
  108. {
  109.         int x1, y1, x2, y2;
  110.         const int d = 30;
  111.         double a, e;
  112.         HPEN h;
  113.  
  114.         h = CreatePen ( PS_SOLID, 1, c );
  115.         SelectObject ( hdc, h );
  116.  
  117.         for ( a = 0; a < 2 * PI; a += PI / 360 )
  118.         {
  119.                 e = d * ( 1 + sin ( a * 5 ) );
  120.                 x1 = ( int ) ( x + e * cos ( a ) );
  121.                 y1 = ( int ) ( y + e * sin ( a ) );
  122.                 x2 = ( int ) ( x + e * cos ( a + PI / 5 ) );
  123.                 y2 = ( int ) ( y + e * sin ( a + PI / 5 ) );
  124.                 DrawLine ( hdc, x1, y1, x2, y2 );
  125.         }
  126.  
  127.         DeleteObject ( h );
  128. }
  129.  
  130. // 画蝴蝶结
  131. void DrawTie ( HDC hdc, int x, int y, COLORREF c )
  132. {
  133.         int x1, y1, x2, y2;
  134.         const int d = 100;
  135.         double a, e;
  136.         HPEN h;
  137.  
  138.         h = CreatePen ( PS_SOLID, 1, c );
  139.         SelectObject ( hdc, h );
  140.  
  141.         for ( a = 0; a < 2 * PI; a += PI / 360 )
  142.         {
  143.                 e = d * ( 1 + sin ( a * 4 ) );
  144.                 x1 = ( int ) ( x + e * cos ( a ) );
  145.                 y1 = ( int ) ( y + e * sin ( a ) / 2 );
  146.                 x2 = ( int ) ( x + e * cos ( a + PI / 9 ) );
  147.                 y2 = ( int ) ( y + e * sin ( a + PI / 9 ) / 4.5 );
  148.                 DrawLine ( hdc, x1, y1, x2, y2 );
  149.         }
  150.  
  151.         DeleteObject ( h );
  152. }
  153.  
  154. //画线
  155. void DrawLine ( HDC hdc, int x1, int y1, int x2, int y2 )
  156. {
  157.         MoveToEx ( hdc, x1, y1, NULL );
  158.         LineTo ( hdc, x2, y2 );
  159. }

回复 "画几朵花"

这儿你可以回复上面这条便签

captcha