Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lab2.cpp: определяет точку входа для приложения.
- //
- #include "stdafx.h"
- #include "lab2.h"
- #define MAX_LOADSTRING 100
- // Глобальные переменные:
- HINSTANCE hInst; // текущий экземпляр
- TCHAR szTitle[MAX_LOADSTRING]; // Текст строки заголовка
- TCHAR szWindowClass[MAX_LOADSTRING]; // имя класса главного окна
- // Отправить объявления функций, включенных в этот модуль кода:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
- // TODO: разместите код здесь.
- MSG msg;
- HACCEL hAccelTable;
- // Инициализация глобальных строк
- LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadString(hInstance, IDC_LAB2, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
- // Выполнить инициализацию приложения:
- if (!InitInstance (hInstance, nCmdShow))
- {
- return FALSE;
- }
- hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LAB2));
- // Цикл основного сообщения:
- while (GetMessage(&msg, NULL, 0, 0))
- {
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (int) msg.wParam;
- }
- //
- // ФУНКЦИЯ: MyRegisterClass()
- //
- // НАЗНАЧЕНИЕ: регистрирует класс окна.
- //
- // КОММЕНТАРИИ:
- //
- // Эта функция и ее использование необходимы только в случае, если нужно, чтобы данный код
- // был совместим с системами Win32, не имеющими функции RegisterClassEx'
- // которая была добавлена в Windows 95. Вызов этой функции важен для того,
- // чтобы приложение получило "качественные" мелкие значки и установило связь
- // с ними.
- //
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- 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, MAKEINTRESOURCE(IDI_LAB2));
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = MAKEINTRESOURCE(IDC_LAB2);
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
- return RegisterClassEx(&wcex);
- }
- //
- // ФУНКЦИЯ: InitInstance(HINSTANCE, int)
- //
- // НАЗНАЧЕНИЕ: сохраняет обработку экземпляра и создает главное окно.
- //
- // КОММЕНТАРИИ:
- //
- // В данной функции дескриптор экземпляра сохраняется в глобальной переменной, а также
- // создается и выводится на экран главное окно программы.
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- HWND hWnd;
- hInst = hInstance; // Сохранить дескриптор экземпляра в глобальной переменной
- hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
- if (!hWnd)
- {
- return FALSE;
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- RECT Rect(int left, int top, int right, int bottom)
- {
- RECT rect;
- rect.left = left;
- rect.top = top;
- rect.right = right;
- rect.bottom = bottom;
- return rect;
- }
- POINT Point(int x, int y)
- {
- POINT point;
- point.x = x;
- point.y = y;
- return point;
- }
- void Push(POINT *points, int *count, POINT point)
- {
- (*count)++;
- points = (POINT*) realloc(points, *count * sizeof(POINT));
- points[*count] = point;
- }
- POINT Pop(POINT *points, int *count)
- {
- POINT point = points[*count];
- (*count)--;
- points = (POINT*) realloc(points, *count * sizeof(POINT));
- return point;
- }
- void FloodFill_1(HDC hdc, POINT point, COLORREF color, COLORREF border) //Простой алгоритм заполнения с затравкой
- {
- int count = 1;
- POINT *points = (POINT*) malloc(count * sizeof(POINT));
- points[0] = point;
- COLORREF bg = GetPixel(hdc, point.x, point.y);
- while(count != 0)
- {
- POINT tmp = Pop(points, &count);
- if(GetPixel(hdc, tmp.x, tmp.y) != bg) SetPixel(hdc, tmp.x, tmp.y, color);
- if((GetPixel(hdc, tmp.x+1, tmp.y) != bg) && (GetPixel(hdc, tmp.x+1, tmp.y) != border))
- Push(points, &count, Point(tmp.x+1,tmp.y));
- else
- if((GetPixel(hdc, tmp.x, tmp.y+1) != bg) && (GetPixel(hdc, tmp.x+1, tmp.y) != border))
- Push(points, &count, Point(tmp.x,tmp.y+1));
- else
- if((GetPixel(hdc, tmp.x-1, tmp.y) != bg) && (GetPixel(hdc, tmp.x+1, tmp.y) != border))
- Push(points, &count, Point(tmp.x-1,tmp.y));
- else
- if((GetPixel(hdc, tmp.x, tmp.y-1) != bg) && (GetPixel(hdc, tmp.x+1, tmp.y) != border))
- Push(points, &count, Point(tmp.x,tmp.y-1));
- }
- }
- void FloodFill_2(HDC hdc, POINT point, COLORREF color, COLORREF border) //Построчный алгоритм заполнения с затравкой
- {
- int count = 1;
- POINT* points = (POINT*) malloc(count * sizeof(POINT));
- points[0] = point;
- COLORREF bg = GetPixel(hdc, point.x, point.y);
- while(count != 0)
- {
- POINT tmp = Pop(points, &count);
- SetPixel(hdc, tmp.x, tmp.y, color);
- int x = tmp.x;
- tmp.x++;
- while(GetPixel(hdc, tmp.x, tmp.y) != border)
- {
- SetPixel(hdc, tmp.x, tmp.y, color);
- tmp.x++;
- }
- int x_right = tmp.x - 1;
- tmp.x = x;
- tmp.x--;
- while(GetPixel(hdc, tmp.x, tmp.y) != border)
- {
- SetPixel(hdc, tmp.x, tmp.y, color);
- tmp.x--;
- }
- int x_left = tmp.x + 1;
- tmp.x = x;
- //проверка для строки выше
- tmp.x = x_left;
- tmp.y++;
- while(tmp.x <= x_right)
- {
- bool flag = false;
- while((GetPixel(hdc,tmp.x,tmp.y) != border) && (GetPixel(hdc,tmp.x,tmp.y) != color) && (tmp.x < x_right))
- {
- if(flag == false) flag = true;
- tmp.x++;
- }
- if(flag)
- if((tmp.x == x_right) && (GetPixel(hdc,tmp.x,tmp.y) != border) && (GetPixel(hdc,tmp.x,tmp.y) != color))
- Push(points, &count, tmp);
- else
- Push(points, &count, Point(tmp.x-1,tmp.y));
- flag = false;
- }
- int x_in = tmp.x;
- while((GetPixel(hdc,tmp.x,tmp.y) != border) && (GetPixel(hdc,tmp.x,tmp.y) != color) && (tmp.x < x_right))
- {
- tmp.x++;
- }
- if(tmp.x == x_in) tmp.x++;
- //проверка для строки ниже
- tmp.x = x_left;
- tmp.y--;
- while(tmp.x <= x_right)
- {
- bool flag = false;
- while((GetPixel(hdc,tmp.x,tmp.y) != border) && (GetPixel(hdc,tmp.x,tmp.y) != color) && (tmp.x < x_right))
- {
- if(flag == false) flag = true;
- tmp.x++;
- }
- if(flag)
- if((tmp.x == x_right) && (GetPixel(hdc,tmp.x,tmp.y) != border) && (GetPixel(hdc,tmp.x,tmp.y) != color))
- Push(points, &count, tmp);
- else
- Push(points, &count, Point(tmp.x-1,tmp.y));
- flag = false;
- }
- x_in = tmp.x;
- while((GetPixel(hdc,tmp.x,tmp.y) != border) && (GetPixel(hdc,tmp.x,tmp.y) != color) && (tmp.x < x_right))
- {
- tmp.x++;
- }
- if(tmp.x == x_in) tmp.x++;
- }
- }
- void FloodFill_3(HDC hdc, POINT point, COLORREF color, COLORREF border) //
- {
- }
- void FloodFillEx(HDC hdc, POINT point, COLORREF color, COLORREF border, int mode) //Main
- {
- switch(mode)
- {
- case 1: FloodFill_1(hdc, point, color, border);
- case 2: FloodFill_2(hdc, point, color, border);
- case 3: FloodFill_3(hdc, point, color, border);
- }
- }
- //
- // ФУНКЦИЯ: WndProc(HWND, UINT, WPARAM, LPARAM)
- //
- // НАЗНАЧЕНИЕ: обрабатывает сообщения в главном окне.
- //
- // WM_COMMAND - обработка меню приложения
- // WM_PAINT -Закрасить главное окно
- // WM_DESTROY - ввести сообщение о выходе и вернуться.
- //
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- int wmId, wmEvent;
- PAINTSTRUCT ps;
- HDC hdc;
- POINT pt[4];
- pt[0] = Point(200, 100);
- pt[1] = Point(300, 100);
- pt[2] = Point(350, 200);
- pt[3] = Point(150, 200);
- switch (message)
- {
- case WM_COMMAND:
- wmId = LOWORD(wParam);
- wmEvent = HIWORD(wParam);
- // Разобрать выбор в меню:
- switch (wmId)
- {
- case IDM_ABOUT:
- DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
- break;
- case IDM_EXIT:
- DestroyWindow(hWnd);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- break;
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- // TODO: добавьте любой код отрисовки...
- Polygon(hdc, pt, 4);
- FloodFillEx(hdc, Point(200,150), RGB(255,255,255), RGB(255,255,255), 1);
- EndPaint(hWnd, &ps);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- // Обработчик сообщений для окна "О программе".
- INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- UNREFERENCED_PARAMETER(lParam);
- switch (message)
- {
- case WM_INITDIALOG:
- return (INT_PTR)TRUE;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return (INT_PTR)TRUE;
- }
- break;
- }
- return (INT_PTR)FALSE;
- }
Advertisement
Add Comment
Please, Sign In to add comment