Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "UP_LAB3.h"
- #include <windowsx.h>
- #include <algorithm>
- #include <commdlg.h>
- #include <cstdio>
- #include <commctrl.h>
- #pragma comment(lib,"comctl32.lib")
- #pragma comment(lib,"User32.lib")
- using namespace std;
- struct SAVER {
- COLORREF cl1, cl2, clLn, clBg;
- int timer, pos, count;
- };
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- HINSTANCE hInst;
- INT_PTR CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
- INT_PTR CALLBACK DlgProc1(HWND, UINT, WPARAM, LPARAM);
- INT_PTR CALLBACK DlgProc2(HWND, UINT, WPARAM, LPARAM);
- HWND hSBar;
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- hInst = hInstance;
- static TCHAR szWindowClass[] = "Prog";
- MSG msg;
- HWND hWnd;
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(hInstance,NULL);
- wcex.hCursor = LoadCursor(NULL, IDC_HAND);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = MAKEINTRESOURCE(IDR_FILE);
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_UP3));
- RegisterClassEx(&wcex);
- int pparts[2];
- pparts[0] = 200;
- pparts[1] = -1;
- hWnd = CreateWindow(szWindowClass, "Труханович 5 Вариант", WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
- hSBar = CreateWindow(STATUSCLASSNAME, "",
- WS_CHILD | WS_VISIBLE | WS_BORDER | SBARS_SIZEGRIP | CCS_BOTTOM,
- 0, 0, 0, 0, hWnd, (HMENU)0, hInstance, NULL);
- SendMessage(hSBar, SB_SETPARTS, 2, (LPARAM)&pparts);
- SendMessage(hSBar, SB_SETTEXT, 0, (LPARAM)"Количество вызовов таймера: 0");
- SendMessage(hSBar, SB_SETTEXT, 1, (LPARAM)"Количество строк в фигуре: 1");
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (int) msg.wParam;
- }
- void SaveFile(char * s, COLORREF cl1, COLORREF cl2, COLORREF clLn, COLORREF clBg, int timer, int pos, int count) {
- static SAVER sav;
- FILE * f = fopen(s, "wb");
- sav.cl1 = cl1;
- sav.cl2 = cl2;
- sav.clLn = clLn;
- sav.clBg = clBg;
- sav.timer = timer;
- sav.pos = pos;
- sav.count = count;
- fwrite(&sav, sizeof(SAVER), 1, f);
- fclose(f);
- }
- void OpenFile(char * s, COLORREF & cl1, COLORREF & cl2, COLORREF & clLn, COLORREF & clBg, bool & timer, int & pos, int & count) {
- static SAVER sav;
- FILE * f = fopen(s, "rb");
- fread(&sav, sizeof(SAVER), 1, f);
- cl1 = sav.cl1;
- cl2 = sav.cl2;
- clLn = sav.clLn;
- clBg = sav.clBg;
- timer = (bool)sav.timer;
- pos = sav.pos;
- count = sav.count;
- fclose(f);
- }
- void UpdateSBar(int x) {
- char temp[42];
- sprintf(temp, "Количество строк в фигуре: %d", x);
- SendMessage(hSBar, SB_SETTEXT, 1, (LPARAM)&temp);
- }
- void PrintPicture(HDC hdc, int n, int w, int h, COLORREF clRed, COLORREF clFiol, COLORREF clBg, COLORREF clLn) {
- const HBRUSH hbrRed = CreateSolidBrush(clRed), hbrFiol = CreateSolidBrush(clFiol),
- hbrBg = CreateSolidBrush(clBg);
- const HPEN hpen = CreatePen(PS_SOLID, 1, clLn);
- SelectBrush(hdc, hbrBg);
- SelectPen(hdc, hpen);
- Rectangle(hdc, 0, 0, w, h);
- if (n > min(w, h) / 2) n = min(w, h) / 2;
- for (int i = 0; i < n; ++i)
- for (int j = 0; j <= i; ++j) {
- if ((i + j) & 1)SelectBrush(hdc, hbrFiol);
- else SelectBrush(hdc, hbrRed);
- Rectangle(hdc, j * w / n, i * h / n, (j + 1) * w / n, (i + 1) * h / n);
- }
- DeleteBrush(hbrRed);
- DeleteBrush(hbrFiol);
- DeleteBrush(hbrBg);
- DeletePen(hpen);
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- static PAINTSTRUCT ps;
- static HDC hdc, hdcScreen, hdcCompatible;
- static HBITMAP hbmScreen;
- static int iWidth, iHeight, cCount = 1, wmId, position = 1, xPos, yPos, temp, ct = 0;
- static HMENU hMenu = GetSubMenu(LoadMenu(hInst, MAKEINTRESOURCE(IDR_CONTEXT)), 0);
- 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;
- static bool isTimer = true;
- static CHOOSECOLOR cc;
- static OPENFILENAME ofn;
- static char szFile[256], szTemp[20];
- switch (message)
- {
- case WM_CREATE:
- SetTimer(hWnd, 1, 1000, NULL);
- hdcScreen = GetDC(hWnd);
- hdcCompatible = CreateCompatibleDC(hdcScreen);
- hbmScreen = CreateCompatibleBitmap(hdcScreen, 2000, 2000);
- SelectObject(hdcCompatible, hbmScreen);
- cc.lStructSize = sizeof(CHOOSECOLOR);
- cc.hwndOwner = hWnd;
- cc.lpCustColors = acrCustClr;
- cc.Flags = CC_FULLOPEN | CC_SOLIDCOLOR;
- ofn.lStructSize = sizeof(OPENFILENAME);
- ofn.hwndOwner = hWnd;
- ofn.lpstrFile = szFile;
- ofn.nMaxFile = sizeof(szFile);
- break;
- case WM_COMMAND:
- wmId = LOWORD(wParam);
- switch (wmId)
- {
- case IDM_FILE_EXIT:
- PostQuitMessage(0);
- break;
- case IDM_CONTEXT_TIMER:
- isTimer = !isTimer;
- if (isTimer) {
- SetTimer(hWnd, 1, 1000, NULL);
- CheckMenuItem(hMenu, IDM_CONTEXT_TIMER, MF_CHECKED);
- } else {
- KillTimer(hWnd, 1);
- CheckMenuItem(hMenu, IDM_CONTEXT_TIMER, MF_UNCHECKED);
- }
- break;
- case IDM_HELP:
- DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG3), NULL, DlgProc2);
- break;
- case IDM_CONTEXT_COLORS:
- clt1 = color1;
- clt2 = color2;
- if (ChooseColor(&cc)) clt1 = cc.rgbResult;
- if (ChooseColor(&cc)) clt2 = cc.rgbResult;
- color1 = clt1;
- color2 = clt2;
- InvalidateRect(hWnd, NULL, true);
- break;
- case IDM_CONTEXT_LINES:
- if (ChooseColor(&cc)) colorLn = cc.rgbResult;
- InvalidateRect(hWnd, NULL, true);
- break;
- case IDM_CONTEXT_BKG:
- if (ChooseColor(&cc)) colorBg = cc.rgbResult;
- InvalidateRect(hWnd, NULL, true);
- break;
- case IDM_CONTEXT_POSITION:
- temp = DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, DlgProc, (LPARAM)position);
- if (temp != 5) {
- position = temp;
- InvalidateRect(hWnd, NULL, true);
- }
- break;
- case IDM_CONTEXT_SIZE:
- temp = DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_DIALOG2), hWnd, DlgProc1, (LPARAM)cCount);
- if (temp != 0) {
- cCount = temp;
- InvalidateRect(hWnd, NULL, false);
- }
- UpdateSBar(cCount);
- break;
- case IDM_FILE_SAVE:
- strcpy(szFile, "");
- temp = GetSaveFileName(&ofn);
- if (temp) SaveFile(ofn.lpstrFile, color1, color2, colorLn, colorBg, (int)isTimer, position, cCount);
- break;
- case IDM_FILE_OPEN:
- strcpy(szFile, "");
- temp = GetOpenFileName(&ofn);
- if (temp) {
- OpenFile(ofn.lpstrFile, color1, color2, colorLn, colorBg, isTimer, position, cCount);
- InvalidateRect(hWnd, NULL, true);
- if (isTimer) {
- SetTimer(hWnd, 1, 1000, NULL);
- CheckMenuItem(hMenu, IDM_CONTEXT_TIMER, MF_CHECKED);
- } else {
- KillTimer(hWnd, 1);
- CheckMenuItem(hMenu, IDM_CONTEXT_TIMER, MF_UNCHECKED);
- }
- }
- UpdateSBar(cCount);
- break;
- }
- break;
- case WM_TIMER:
- swap(color1, color2);
- InvalidateRect(hWnd, NULL, false);
- sprintf(szTemp, "Количество вызовов таймера: %d", ++ct);
- SendMessage(hSBar, SB_SETTEXT, 0, (LPARAM)szTemp);
- break;
- case WM_CONTEXTMENU:
- TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_TOPALIGN | TPM_LEFTALIGN,
- LOWORD(lParam), HIWORD(lParam), 0, hWnd, NULL);
- break;
- case WM_KEYDOWN:
- if (LOWORD(wParam) == VK_UP || LOWORD(wParam) == VK_DOWN) {
- if (wParam == VK_UP) ++cCount;
- else --cCount;
- if (cCount < 1) cCount = 1;
- else InvalidateRect(hWnd, NULL, true);
- UpdateSBar(cCount);
- }
- break;
- case WM_SIZE:
- iWidth = LOWORD(lParam);
- iHeight = HIWORD(lParam);
- SendMessage(hSBar, WM_SIZE, 0, 0);
- break;
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- if (position == 0) xPos = iWidth / 4, yPos = iHeight / 4;
- if (position == 1) xPos = yPos = 0;
- if (position == 2) xPos = iWidth / 2, yPos = 0;
- if (position == 3) xPos = 0, yPos = iHeight / 2;
- if (position == 4) xPos = iWidth / 2, yPos = iHeight / 2;
- PrintPicture(hdcCompatible, cCount, iWidth / 2, iHeight / 2, color1, color2, colorBg, colorLn);
- BitBlt(hdcScreen, xPos, yPos, iWidth / 2, iHeight / 2, hdcCompatible, 0, 0, SRCCOPY);
- EndPaint(hWnd, &ps);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- INT_PTR CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- int g;
- static int sel;
- switch (message)
- {
- case WM_CLOSE:
- EndDialog(hDlg, 5);
- break;
- case WM_INITDIALOG:
- g = (int) lParam;
- if (g == 0) CheckRadioButton(hDlg, IDC_RAD_LU, IDC_RAD_RD, IDC_RAD_CC);
- if (g == 1) CheckRadioButton(hDlg, IDC_RAD_LU, IDC_RAD_RD, IDC_RAD_LU);
- if (g == 2) CheckRadioButton(hDlg, IDC_RAD_LU, IDC_RAD_RD, IDC_RAD_RU);
- if (g == 3) CheckRadioButton(hDlg, IDC_RAD_LU, IDC_RAD_RD, IDC_RAD_LD);
- if (g == 4) CheckRadioButton(hDlg, IDC_RAD_LU, IDC_RAD_RD, IDC_RAD_RD);
- sel = g;
- break;
- case WM_COMMAND:
- g = LOWORD(wParam);
- if (g == IDC_OK) EndDialog(hDlg, sel);
- if (g == IDC_CANC) EndDialog(hDlg, 5);
- if (g == IDC_RAD_LU) sel = 1;
- if (g == IDC_RAD_RU) sel = 2;
- if (g == IDC_RAD_LD) sel = 3;
- if (g == IDC_RAD_RD) sel = 4;
- if (g == IDC_RAD_CC) sel = 0;
- break;
- }
- return (INT_PTR)FALSE;
- }
- INT_PTR CALLBACK DlgProc1(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- int g;
- static int sel;
- HWND edit;
- switch (message)
- {
- case WM_CLOSE:
- EndDialog(hDlg, 0);
- break;
- case WM_INITDIALOG:
- g = (int) lParam;
- edit = GetDlgItem(hDlg, IDC_EDIT2);
- SetDlgItemInt(hDlg, IDC_EDIT2, g, true);
- break;
- case WM_COMMAND:
- g = LOWORD(wParam);
- if (g == IDOK) EndDialog(hDlg, GetDlgItemInt(hDlg, IDC_EDIT2, NULL, FALSE));
- if (g == IDCANCEL) EndDialog(hDlg, 0);
- break;
- }
- return (INT_PTR)FALSE;
- }
- INT_PTR CALLBACK DlgProc2(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- int g;
- static int sel;
- static CHOOSEFONT chf;
- static HFONT hFont;
- static LOGFONT lf;
- switch (message)
- {
- case WM_INITDIALOG:
- SetWindowFont(GetDlgItem(hDlg, IDC_STATIC), hFont, true);
- chf.lStructSize = sizeof(CHOOSEFONT);
- chf.hwndOwner = hDlg;
- chf.lpLogFont = &lf;
- chf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
- break;
- case WM_CLOSE:
- EndDialog(hDlg, 0);
- break;
- case WM_COMMAND:
- g = LOWORD(wParam);
- if (g == IDOK) EndDialog(hDlg, 0);
- if (g == IDC_FONT && ChooseFont(&chf)) {
- hFont = CreateFontIndirect(chf.lpLogFont);
- SetWindowFont(GetDlgItem(hDlg, IDC_STATIC), hFont, false);
- InvalidateRect(GetDlgItem(hDlg, IDC_STATIC), NULL, true);
- }
- break;
- }
- return (INT_PTR)FALSE;
- }
Advertisement
Add Comment
Please, Sign In to add comment