Advertisement
Guest User

Untitled

a guest
May 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.80 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. const char szClassName[] = "WinDZ";
  7. const char szWindowName[] = "Windows DZ";
  8.  
  9. LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam);
  10.  
  11. bool isVisible = false;
  12. HWND buttonHwnd;
  13. void DrawGraph(HWND hWnd, HDC hDC)
  14. {
  15.     RECT rect;
  16.     GetClientRect(hWnd, &rect);
  17.     const int xVE=rect.right-rect.left;
  18.     const int yVE=rect.bottom-rect.top;
  19.    
  20.     SetBkMode(hDC, TRANSPARENT);
  21.  
  22.     SetMapMode(hDC, MM_ISOTROPIC);
  23.     SetWindowExtEx(hDC, xVE, yVE, NULL);
  24.     SetViewportExtEx(hDC, xVE, -yVE, NULL);
  25.     SetViewportOrgEx(hDC, xVE/2, yVE/2, NULL);
  26.    
  27.  
  28.     HPEN hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
  29.     HPEN hOldPen = (HPEN)SelectObject (hDC, hPen);
  30.    
  31.     HFONT hOldFont, hFont;
  32.     hFont = CreateFont(15,0,0,0,FW_NORMAL,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,"Times New Roman Cyr");
  33.     hOldFont=(HFONT)SelectObject(hDC,hFont);
  34.    
  35.     TextOut(hDC, 5, -5, "0", 1);
  36.  
  37.     MoveToEx(hDC, 0, -yVE/2, NULL);
  38.     LineTo(hDC, 0, yVE/2);
  39.     MoveToEx(hDC, -xVE/2, 0, NULL);
  40.     LineTo(hDC, xVE/2, 0);
  41.  
  42.     double maxY = -999999.0;
  43.     double ax[21], ay[21];
  44.     int x;
  45.     double xVal = -1.0;
  46.     int pointX, pointY;
  47.  
  48.     for (x = 0; x <= 20; x++)
  49.     {
  50.         ax[x] = xVal;
  51.         ay[x] = sin(xVal);
  52.        
  53.         if (ay[x] > maxY)
  54.             maxY = ay[x];
  55.                
  56.         xVal += 0.1;
  57.     }
  58.     double mx = xVE/2;
  59.     double my = yVE/2;
  60.    
  61.     char fStr[100];
  62.     int len;
  63.     MoveToEx(hDC, ax[0]*mx, ay[0]*my, NULL);
  64.     for (x = 0; x <= 20; x++)
  65.     {
  66.  
  67.         pointX = ax[x]*mx;
  68.         pointY = ay[x]*my;
  69.        
  70.         if (!(x % 2) && x != 10)
  71.         {
  72.             len = sprintf(fStr, "%.1f", ax[x]);
  73.             TextOut(hDC, x < 10 ? pointX : pointX - 20, 0, fStr, len);
  74.  
  75.             len = sprintf(fStr, "%.1f", ay[x]);
  76.             TextOut(hDC, 5, pointY, fStr, len);
  77.         }
  78.  
  79.         LineTo(hDC, pointX, pointY);
  80.     }
  81.  
  82.     SelectObject(hDC, hOldPen);
  83.     SelectObject(hDC, hOldFont);
  84.     DeleteObject (hPen);
  85. }
  86.  
  87. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine,
  88.            int nCmdShow)
  89. {
  90.    
  91.     HWND hWnd;
  92.  
  93.    
  94.     WNDCLASS wndclass;
  95.     wndclass.style         = (CS_HREDRAW | CS_VREDRAW);
  96.     wndclass.lpfnWndProc   = WndProc;
  97.     wndclass.hInstance     = hInstance;
  98.     wndclass.cbClsExtra = 0;
  99.     wndclass.cbWndExtra = 0;
  100.     wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  101.     wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  102.     wndclass.hbrBackground = (HBRUSH)GetStockObject(0);
  103.     wndclass.lpszMenuName  = NULL;
  104.     wndclass.lpszClassName = szClassName;
  105.  
  106.     if (!RegisterClass(&wndclass))
  107.             exit(FALSE);
  108.  
  109.    
  110.     hWnd = CreateWindow(szClassName,
  111.         szWindowName,
  112.         WS_OVERLAPPEDWINDOW,
  113.         250,
  114.         250,
  115.         800,
  116.         600,
  117.         NULL,
  118.         0,
  119.         hInstance,
  120.         NULL);
  121.  
  122.     if (!hWnd)
  123.         exit(FALSE);
  124.  
  125.     buttonHwnd = CreateWindow("button", "Показать/Скрыть", WS_CHILD|WS_VISIBLE, 0, 0, 150, 30, hWnd, (HMENU)333, hInstance, NULL);
  126.  
  127.     ShowWindow(hWnd, nCmdShow);
  128.     UpdateWindow(hWnd);  
  129.  
  130.        
  131.     MSG msg;
  132.     while(GetMessage(&msg, NULL, 0, 0))
  133.     {
  134.         TranslateMessage(&msg);
  135.         DispatchMessage(&msg);
  136.     }
  137.     return (int)msg.wParam;
  138. }
  139.  
  140.  
  141. LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
  142. {
  143.     HDC hDC;
  144.     PAINTSTRUCT ps;
  145.     RECT rct;
  146.     switch (iMessage)
  147.     {
  148.         case WM_DESTROY:
  149.             PostQuitMessage(0);
  150.             break;
  151.         case WM_PAINT:
  152.             if (isVisible)
  153.             {
  154.                 hDC=BeginPaint(hWnd, &ps);
  155.                 DrawGraph(hWnd, hDC);
  156.                 EndPaint(hWnd, &ps);
  157.             }
  158.            
  159.         case WM_COMMAND:
  160.             if (wParam == 333)
  161.             {
  162.                 if (!(isVisible = !isVisible))
  163.                 {
  164.                     GetClientRect(hWnd, &rct);
  165.                     InvalidateRect(hWnd, &rct, true);
  166.                     UpdateWindow(buttonHwnd);
  167.                 }              
  168.             }
  169.             break;
  170.            
  171.         default:
  172.             return DefWindowProc(hWnd, iMessage, wParam, lParam);
  173.     }
  174.     return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement