Tranvick

Кашперовский

Apr 23rd, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.77 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "UP_LAB3.h"
  3. #include <windowsx.h>
  4. #include <algorithm>
  5. #include <commdlg.h>
  6. #include <cstdio>
  7. #include <commctrl.h>
  8.  
  9. #pragma comment(lib,"comctl32.lib")
  10. #pragma comment(lib,"User32.lib")
  11.  
  12. using namespace std;
  13.  
  14. struct SAVER {
  15.     COLORREF cl1, cl2, clLn, clBg;
  16.     int timer, pos, count;
  17. };
  18.  
  19. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  20. HINSTANCE hInst;
  21. INT_PTR CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
  22. INT_PTR CALLBACK DlgProc1(HWND, UINT, WPARAM, LPARAM);
  23. INT_PTR CALLBACK DlgProc2(HWND, UINT, WPARAM, LPARAM);
  24. HWND hSBar;
  25.  
  26. int APIENTRY _tWinMain(HINSTANCE hInstance,
  27.                      HINSTANCE hPrevInstance,
  28.                      LPTSTR    lpCmdLine,
  29.                      int       nCmdShow)
  30. {
  31.     hInst = hInstance;
  32.     static TCHAR szWindowClass[] = "Prog";
  33.     MSG msg;
  34.     HWND hWnd;
  35.     WNDCLASSEX wcex;
  36.  
  37.     wcex.cbSize = sizeof(WNDCLASSEX);
  38.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  39.     wcex.lpfnWndProc    = WndProc;
  40.     wcex.cbClsExtra     = 0;
  41.     wcex.cbWndExtra     = 0;
  42.     wcex.hInstance      = hInstance;
  43.     wcex.hIcon          = LoadIcon(hInstance,NULL);
  44.     wcex.hCursor        = LoadCursor(NULL, IDC_HAND);
  45.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  46.     wcex.lpszMenuName   = MAKEINTRESOURCE(IDR_FILE);
  47.     wcex.lpszClassName  = szWindowClass;
  48.     wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_UP3));
  49.     RegisterClassEx(&wcex);
  50.  
  51.     int pparts[2];
  52.     pparts[0] = 200;
  53.     pparts[1] = -1;
  54.     hWnd = CreateWindow(szWindowClass, "Труханович 5 Вариант", WS_OVERLAPPEDWINDOW,
  55.         CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  56.     hSBar = CreateWindow(STATUSCLASSNAME, "",
  57.                                  WS_CHILD | WS_VISIBLE | WS_BORDER | SBARS_SIZEGRIP | CCS_BOTTOM,
  58.                                  0, 0, 0, 0, hWnd, (HMENU)0, hInstance, NULL);
  59.     SendMessage(hSBar, SB_SETPARTS, 2, (LPARAM)&pparts);
  60.     SendMessage(hSBar, SB_SETTEXT, 0, (LPARAM)"Количество вызовов таймера: 0");
  61.     SendMessage(hSBar, SB_SETTEXT, 1, (LPARAM)"Количество строк в фигуре: 1");
  62.     ShowWindow(hWnd, nCmdShow);
  63.     UpdateWindow(hWnd);
  64.  
  65.     while (GetMessage(&msg, NULL, 0, 0))
  66.     {
  67.             TranslateMessage(&msg);
  68.             DispatchMessage(&msg);
  69.     }
  70.  
  71.     return (int) msg.wParam;
  72. }
  73.  
  74. void SaveFile(char * s, COLORREF cl1, COLORREF cl2, COLORREF clLn, COLORREF clBg, int timer, int pos, int count) {
  75.     static SAVER sav;
  76.     FILE * f = fopen(s, "wb");
  77.     sav.cl1 = cl1;
  78.     sav.cl2 = cl2;
  79.     sav.clLn = clLn;
  80.     sav.clBg = clBg;
  81.     sav.timer = timer;
  82.     sav.pos = pos;
  83.     sav.count = count;
  84.     fwrite(&sav, sizeof(SAVER), 1, f);
  85.     fclose(f);
  86. }
  87.  
  88. void OpenFile(char * s, COLORREF & cl1, COLORREF & cl2, COLORREF & clLn, COLORREF & clBg, bool & timer, int & pos, int & count) {
  89.     static SAVER sav;
  90.     FILE * f = fopen(s, "rb");
  91.     fread(&sav, sizeof(SAVER), 1, f);
  92.     cl1 = sav.cl1;
  93.     cl2 = sav.cl2;
  94.     clLn = sav.clLn;
  95.     clBg = sav.clBg;
  96.     timer = (bool)sav.timer;
  97.     pos = sav.pos;
  98.     count = sav.count;
  99.     fclose(f);
  100. }
  101.  
  102. void UpdateSBar(int x) {
  103.     char temp[42];
  104.     sprintf(temp, "Количество строк в фигуре: %d", x);
  105.     SendMessage(hSBar, SB_SETTEXT, 1, (LPARAM)&temp);
  106. }
  107.  
  108. void PrintPicture(HDC hdc, int n, int w, int h, COLORREF clRed, COLORREF clFiol, COLORREF clBg, COLORREF clLn) {
  109.     const HBRUSH hbrRed = CreateSolidBrush(clRed), hbrFiol = CreateSolidBrush(clFiol),
  110.         hbrBg = CreateSolidBrush(clBg);
  111.     const HPEN hpen = CreatePen(PS_SOLID, 1, clLn);
  112.     SelectBrush(hdc, hbrBg);
  113.     SelectPen(hdc, hpen);
  114.     Rectangle(hdc, 0, 0, w, h);
  115.     if (n > min(w, h) / 2) n = min(w, h) / 2;
  116.     for (int i = 0; i < n; ++i)
  117.         for (int j = 0; j <= i; ++j) {
  118.             if ((i + j) & 1)SelectBrush(hdc, hbrFiol);
  119.             else SelectBrush(hdc, hbrRed);
  120.             Rectangle(hdc, j * w / n, i * h / n, (j + 1) * w / n, (i + 1) * h / n);
  121.         }
  122.     DeleteBrush(hbrRed);
  123.     DeleteBrush(hbrFiol);
  124.     DeleteBrush(hbrBg);
  125.     DeletePen(hpen);
  126. }
  127.  
  128. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  129. {
  130.     static PAINTSTRUCT ps;
  131.     static HDC hdc, hdcScreen, hdcCompatible;
  132.     static HBITMAP hbmScreen;
  133.     static int iWidth, iHeight, cCount = 1, wmId, position = 1, xPos, yPos, temp, ct = 0;
  134.     static HMENU hMenu = GetSubMenu(LoadMenu(hInst, MAKEINTRESOURCE(IDR_CONTEXT)), 0);
  135.     static COLORREF color1 = RGB(255, 0, 0), color2 = RGB(155, 0, 155), colorBg = RGB(255, 255, 255), colorLn = RGB(0, 0, 0), acrCustClr[16], clt1, clt2;
  136.     static bool isTimer = true;
  137.     static CHOOSECOLOR cc;
  138.     static OPENFILENAME ofn;
  139.     static char szFile[256], szTemp[20];
  140.     switch (message)
  141.     {
  142.     case WM_CREATE:
  143.         SetTimer(hWnd, 1, 1000, NULL);
  144.         hdcScreen = GetDC(hWnd);
  145.         hdcCompatible = CreateCompatibleDC(hdcScreen);
  146.         hbmScreen = CreateCompatibleBitmap(hdcScreen, 2000, 2000);
  147.         SelectObject(hdcCompatible, hbmScreen);
  148.         cc.lStructSize = sizeof(CHOOSECOLOR);
  149.         cc.hwndOwner = hWnd;
  150.         cc.lpCustColors = acrCustClr;
  151.         cc.Flags = CC_FULLOPEN | CC_SOLIDCOLOR;
  152.         ofn.lStructSize = sizeof(OPENFILENAME);
  153.         ofn.hwndOwner = hWnd;
  154.         ofn.lpstrFile = szFile;
  155.         ofn.nMaxFile = sizeof(szFile);
  156.         break;
  157.     case WM_COMMAND:
  158.         wmId = LOWORD(wParam);
  159.         switch (wmId)
  160.         {
  161.         case IDM_FILE_EXIT:
  162.             PostQuitMessage(0);
  163.             break;
  164.         case IDM_CONTEXT_TIMER:
  165.             isTimer = !isTimer;
  166.             if (isTimer) {
  167.                 SetTimer(hWnd, 1, 1000, NULL);
  168.                 CheckMenuItem(hMenu, IDM_CONTEXT_TIMER, MF_CHECKED);
  169.             } else {
  170.                 KillTimer(hWnd, 1);
  171.                 CheckMenuItem(hMenu, IDM_CONTEXT_TIMER, MF_UNCHECKED);
  172.             }
  173.             break;
  174.         case IDM_HELP:
  175.             DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG3), NULL, DlgProc2);
  176.             break;
  177.         case IDM_CONTEXT_COLORS:
  178.             clt1 = color1;
  179.             clt2 = color2;
  180.             if (ChooseColor(&cc)) clt1 = cc.rgbResult;
  181.             if (ChooseColor(&cc)) clt2 = cc.rgbResult;
  182.             color1 = clt1;
  183.             color2 = clt2;
  184.             InvalidateRect(hWnd, NULL, true);
  185.             break;
  186.         case IDM_CONTEXT_LINES:
  187.             if (ChooseColor(&cc)) colorLn = cc.rgbResult;
  188.             InvalidateRect(hWnd, NULL, true);
  189.             break;
  190.         case IDM_CONTEXT_BKG:
  191.             if (ChooseColor(&cc)) colorBg = cc.rgbResult;
  192.             InvalidateRect(hWnd, NULL, true);
  193.             break;
  194.         case IDM_CONTEXT_POSITION:
  195.             temp = DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, DlgProc, (LPARAM)position);
  196.             if (temp != 5) {
  197.                 position = temp;
  198.                 InvalidateRect(hWnd, NULL, true);
  199.             }
  200.             break;
  201.         case IDM_CONTEXT_SIZE:
  202.             temp = DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_DIALOG2), hWnd, DlgProc1, (LPARAM)cCount);
  203.             if (temp != 0) {
  204.                 cCount = temp;
  205.                 InvalidateRect(hWnd, NULL, false);
  206.             }
  207.             UpdateSBar(cCount);
  208.             break;
  209.         case IDM_FILE_SAVE:
  210.             strcpy(szFile, "");
  211.             temp = GetSaveFileName(&ofn);
  212.             if (temp) SaveFile(ofn.lpstrFile, color1, color2, colorLn, colorBg, (int)isTimer, position, cCount);
  213.             break;
  214.         case IDM_FILE_OPEN:
  215.             strcpy(szFile, "");
  216.             temp = GetOpenFileName(&ofn);
  217.             if (temp) {
  218.                 OpenFile(ofn.lpstrFile, color1, color2, colorLn, colorBg, isTimer, position, cCount);
  219.                 InvalidateRect(hWnd, NULL, true);
  220.                 if (isTimer) {
  221.                     SetTimer(hWnd, 1, 1000, NULL);
  222.                     CheckMenuItem(hMenu, IDM_CONTEXT_TIMER, MF_CHECKED);
  223.                 } else {
  224.                     KillTimer(hWnd, 1);
  225.                     CheckMenuItem(hMenu, IDM_CONTEXT_TIMER, MF_UNCHECKED);
  226.                 }
  227.             }
  228.             UpdateSBar(cCount);
  229.             break;
  230.         }
  231.         break;
  232.     case WM_TIMER:
  233.         swap(color1, color2);
  234.         InvalidateRect(hWnd, NULL, false);
  235.         sprintf(szTemp, "Количество вызовов таймера: %d", ++ct);
  236.         SendMessage(hSBar, SB_SETTEXT, 0, (LPARAM)szTemp);     
  237.         break;
  238.     case WM_CONTEXTMENU:
  239.         TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_TOPALIGN | TPM_LEFTALIGN,
  240.                         LOWORD(lParam), HIWORD(lParam), 0, hWnd, NULL);
  241.         break;
  242.     case WM_KEYDOWN:
  243.         if (LOWORD(wParam) == VK_UP || LOWORD(wParam) == VK_DOWN) {
  244.             if (wParam == VK_UP) ++cCount;
  245.             else --cCount;
  246.             if (cCount < 1) cCount = 1;
  247.             else InvalidateRect(hWnd, NULL, true);
  248.             UpdateSBar(cCount);
  249.         }
  250.         break;     
  251.     case WM_SIZE:
  252.         iWidth = LOWORD(lParam);
  253.         iHeight = HIWORD(lParam);
  254.         SendMessage(hSBar, WM_SIZE, 0, 0);
  255.         break;
  256.     case WM_PAINT:
  257.         hdc = BeginPaint(hWnd, &ps);
  258.         if (position == 0) xPos = iWidth / 4, yPos = iHeight / 4;
  259.         if (position == 1) xPos = yPos = 0;
  260.         if (position == 2) xPos = iWidth / 2, yPos = 0;
  261.         if (position == 3) xPos = 0, yPos = iHeight / 2;
  262.         if (position == 4) xPos = iWidth / 2, yPos = iHeight / 2;
  263.         PrintPicture(hdcCompatible, cCount, iWidth / 2, iHeight / 2, color1, color2, colorBg, colorLn);
  264.         BitBlt(hdcScreen, xPos, yPos, iWidth / 2, iHeight / 2, hdcCompatible, 0, 0, SRCCOPY);
  265.         EndPaint(hWnd, &ps);
  266.         break;
  267.     case WM_DESTROY:
  268.         PostQuitMessage(0);
  269.         break;
  270.     default:
  271.         return DefWindowProc(hWnd, message, wParam, lParam);
  272.     }
  273.     return 0;
  274. }
  275.  
  276. INT_PTR CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  277. {
  278.     int g;
  279.     static int sel;
  280.     switch (message)
  281.     {
  282.     case WM_CLOSE:
  283.         EndDialog(hDlg, 5);
  284.         break;
  285.     case WM_INITDIALOG:
  286.         g = (int) lParam;
  287.         if (g == 0) CheckRadioButton(hDlg, IDC_RAD_LU, IDC_RAD_RD, IDC_RAD_CC);
  288.         if (g == 1) CheckRadioButton(hDlg, IDC_RAD_LU, IDC_RAD_RD, IDC_RAD_LU);
  289.         if (g == 2) CheckRadioButton(hDlg, IDC_RAD_LU, IDC_RAD_RD, IDC_RAD_RU);
  290.         if (g == 3) CheckRadioButton(hDlg, IDC_RAD_LU, IDC_RAD_RD, IDC_RAD_LD);
  291.         if (g == 4) CheckRadioButton(hDlg, IDC_RAD_LU, IDC_RAD_RD, IDC_RAD_RD);
  292.         sel = g;
  293.         break;
  294.     case WM_COMMAND:
  295.         g = LOWORD(wParam);
  296.         if (g == IDC_OK) EndDialog(hDlg, sel);
  297.         if (g == IDC_CANC) EndDialog(hDlg, 5);
  298.         if (g == IDC_RAD_LU) sel = 1;
  299.         if (g == IDC_RAD_RU) sel = 2;
  300.         if (g == IDC_RAD_LD) sel = 3;
  301.         if (g == IDC_RAD_RD) sel = 4;
  302.         if (g == IDC_RAD_CC) sel = 0;
  303.         break;
  304.     }
  305.     return (INT_PTR)FALSE;
  306. }
  307.  
  308. INT_PTR CALLBACK DlgProc1(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  309. {
  310.     int g;
  311.     static int sel;
  312.     HWND edit;
  313.     switch (message)
  314.     {
  315.     case WM_CLOSE:
  316.         EndDialog(hDlg, 0);
  317.         break;
  318.     case WM_INITDIALOG:
  319.         g = (int) lParam;
  320.         edit = GetDlgItem(hDlg, IDC_EDIT2);
  321.         SetDlgItemInt(hDlg, IDC_EDIT2, g, true);
  322.         break;
  323.     case WM_COMMAND:
  324.         g = LOWORD(wParam);
  325.         if (g == IDOK) EndDialog(hDlg, GetDlgItemInt(hDlg, IDC_EDIT2, NULL, FALSE));
  326.         if (g == IDCANCEL) EndDialog(hDlg, 0);
  327.         break;
  328.     }
  329.     return (INT_PTR)FALSE;
  330. }
  331.  
  332. INT_PTR CALLBACK DlgProc2(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  333. {
  334.     int g;
  335.     static int sel;
  336.     static CHOOSEFONT chf;
  337.     static HFONT hFont;
  338.     static LOGFONT lf;
  339.     switch (message)
  340.     {
  341.     case WM_INITDIALOG:
  342.         SetWindowFont(GetDlgItem(hDlg, IDC_STATIC), hFont, true);
  343.         chf.lStructSize = sizeof(CHOOSEFONT);
  344.         chf.hwndOwner = hDlg;
  345.         chf.lpLogFont = &lf;
  346.         chf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
  347.         break;
  348.     case WM_CLOSE:
  349.         EndDialog(hDlg, 0);
  350.         break;
  351.     case WM_COMMAND:
  352.         g = LOWORD(wParam);
  353.         if (g == IDOK) EndDialog(hDlg, 0);
  354.         if (g == IDC_FONT && ChooseFont(&chf)) {
  355.                 hFont = CreateFontIndirect(chf.lpLogFont);
  356.                 SetWindowFont(GetDlgItem(hDlg, IDC_STATIC), hFont, false);
  357.                 InvalidateRect(GetDlgItem(hDlg, IDC_STATIC), NULL, true);
  358.         }
  359.         break;
  360.     }
  361.     return (INT_PTR)FALSE;
  362. }
Advertisement
Add Comment
Please, Sign In to add comment