Guest User

Untitled

a guest
Apr 17th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.05 KB | None | 0 0
  1. // lab2.cpp: определяет точку входа для приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "lab2.h"
  6.  
  7. #define MAX_LOADSTRING 100
  8.  
  9. // Глобальные переменные:
  10. HINSTANCE hInst;                                // текущий экземпляр
  11. TCHAR szTitle[MAX_LOADSTRING];                  // Текст строки заголовка
  12. TCHAR szWindowClass[MAX_LOADSTRING];            // имя класса главного окна
  13.  
  14. // Отправить объявления функций, включенных в этот модуль кода:
  15. ATOM                MyRegisterClass(HINSTANCE hInstance);
  16. BOOL                InitInstance(HINSTANCE, int);
  17. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  18. INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  19.  
  20. int APIENTRY _tWinMain(HINSTANCE hInstance,
  21.                      HINSTANCE hPrevInstance,
  22.                      LPTSTR    lpCmdLine,
  23.                      int       nCmdShow)
  24. {
  25.     UNREFERENCED_PARAMETER(hPrevInstance);
  26.     UNREFERENCED_PARAMETER(lpCmdLine);
  27.  
  28.     // TODO: разместите код здесь.
  29.     MSG msg;
  30.     HACCEL hAccelTable;
  31.  
  32.     // Инициализация глобальных строк
  33.     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  34.     LoadString(hInstance, IDC_LAB2, szWindowClass, MAX_LOADSTRING);
  35.     MyRegisterClass(hInstance);
  36.  
  37.     // Выполнить инициализацию приложения:
  38.     if (!InitInstance (hInstance, nCmdShow))
  39.     {
  40.         return FALSE;
  41.     }
  42.  
  43.     hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LAB2));
  44.  
  45.     // Цикл основного сообщения:
  46.     while (GetMessage(&msg, NULL, 0, 0))
  47.     {
  48.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  49.         {
  50.             TranslateMessage(&msg);
  51.             DispatchMessage(&msg);
  52.         }
  53.     }
  54.  
  55.     return (int) msg.wParam;
  56. }
  57.  
  58.  
  59.  
  60. //
  61. //  ФУНКЦИЯ: MyRegisterClass()
  62. //
  63. //  НАЗНАЧЕНИЕ: регистрирует класс окна.
  64. //
  65. //  КОММЕНТАРИИ:
  66. //
  67. //    Эта функция и ее использование необходимы только в случае, если нужно, чтобы данный код
  68. //    был совместим с системами Win32, не имеющими функции RegisterClassEx'
  69. //    которая была добавлена в Windows 95. Вызов этой функции важен для того,
  70. //    чтобы приложение получило "качественные" мелкие значки и установило связь
  71. //    с ними.
  72. //
  73. ATOM MyRegisterClass(HINSTANCE hInstance)
  74. {
  75.     WNDCLASSEX wcex;
  76.  
  77.     wcex.cbSize = sizeof(WNDCLASSEX);
  78.  
  79.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  80.     wcex.lpfnWndProc    = WndProc;
  81.     wcex.cbClsExtra     = 0;
  82.     wcex.cbWndExtra     = 0;
  83.     wcex.hInstance      = hInstance;
  84.     wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LAB2));
  85.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  86.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  87.     wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_LAB2);
  88.     wcex.lpszClassName  = szWindowClass;
  89.     wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  90.  
  91.     return RegisterClassEx(&wcex);
  92. }
  93.  
  94. //
  95. //   ФУНКЦИЯ: InitInstance(HINSTANCE, int)
  96. //
  97. //   НАЗНАЧЕНИЕ: сохраняет обработку экземпляра и создает главное окно.
  98. //
  99. //   КОММЕНТАРИИ:
  100. //
  101. //        В данной функции дескриптор экземпляра сохраняется в глобальной переменной, а также
  102. //        создается и выводится на экран главное окно программы.
  103. //
  104. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  105. {
  106.    HWND hWnd;
  107.  
  108.    hInst = hInstance; // Сохранить дескриптор экземпляра в глобальной переменной
  109.  
  110.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  111.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  112.  
  113.    if (!hWnd)
  114.    {
  115.       return FALSE;
  116.    }
  117.  
  118.    ShowWindow(hWnd, nCmdShow);
  119.    UpdateWindow(hWnd);
  120.  
  121.    return TRUE;
  122. }
  123.  
  124. RECT Rect(int left, int top, int right, int bottom)
  125. {
  126.     RECT rect;
  127.     rect.left = left;
  128.     rect.top = top;
  129.     rect.right = right;
  130.     rect.bottom = bottom;
  131.     return rect;
  132. }
  133.  
  134. POINT Point(int x, int y)
  135. {
  136.     POINT point;
  137.     point.x = x;
  138.     point.y = y;
  139.     return point;
  140. }
  141.  
  142. void Push(POINT *points, int *count, POINT point)
  143. {
  144.     (*count)++;
  145.     points = (POINT*) realloc(points, *count * sizeof(POINT));
  146.     points[*count] = point;
  147. }
  148.  
  149. POINT Pop(POINT *points, int *count)
  150. {
  151.     POINT point = points[*count];
  152.     (*count)--;
  153.     points = (POINT*) realloc(points, *count * sizeof(POINT));
  154.     return point;
  155. }
  156.  
  157. void FloodFill_1(HDC hdc, POINT point, COLORREF color, COLORREF border) //Простой алгоритм заполнения с затравкой
  158. {
  159.     int count = 1;
  160.     POINT *points = (POINT*) malloc(count * sizeof(POINT));
  161.     points[0] = point;
  162.     COLORREF bg = GetPixel(hdc, point.x, point.y);
  163.  
  164.     while(count != 0)
  165.     {
  166.         POINT tmp = Pop(points, &count);
  167.         if(GetPixel(hdc, tmp.x, tmp.y) != bg) SetPixel(hdc, tmp.x, tmp.y, color);
  168.         if((GetPixel(hdc, tmp.x+1, tmp.y) != bg) && (GetPixel(hdc, tmp.x+1, tmp.y) != border))
  169.             Push(points, &count, Point(tmp.x+1,tmp.y));
  170.         else
  171.             if((GetPixel(hdc, tmp.x, tmp.y+1) != bg) && (GetPixel(hdc, tmp.x+1, tmp.y) != border))
  172.                 Push(points, &count, Point(tmp.x,tmp.y+1));
  173.             else
  174.                 if((GetPixel(hdc, tmp.x-1, tmp.y) != bg) && (GetPixel(hdc, tmp.x+1, tmp.y) != border))
  175.                     Push(points, &count, Point(tmp.x-1,tmp.y));
  176.                 else
  177.                     if((GetPixel(hdc, tmp.x, tmp.y-1) != bg) && (GetPixel(hdc, tmp.x+1, tmp.y) != border))
  178.                         Push(points, &count, Point(tmp.x,tmp.y-1));
  179.     }
  180. }
  181.  
  182. void FloodFill_2(HDC hdc, POINT point, COLORREF color, COLORREF border) //Построчный алгоритм заполнения с затравкой
  183. {
  184.     int count = 1;
  185.     POINT* points = (POINT*) malloc(count * sizeof(POINT));
  186.     points[0] = point;
  187.     COLORREF bg = GetPixel(hdc, point.x, point.y);
  188.  
  189.     while(count != 0)
  190.     {
  191.         POINT tmp = Pop(points, &count);
  192.         SetPixel(hdc, tmp.x, tmp.y, color);
  193.         int x = tmp.x;
  194.         tmp.x++;
  195.         while(GetPixel(hdc, tmp.x, tmp.y) != border)
  196.         {
  197.             SetPixel(hdc, tmp.x, tmp.y, color);
  198.             tmp.x++;
  199.         }
  200.         int x_right = tmp.x - 1;
  201.         tmp.x = x;
  202.         tmp.x--;
  203.         while(GetPixel(hdc, tmp.x, tmp.y) != border)
  204.         {
  205.             SetPixel(hdc, tmp.x, tmp.y, color);
  206.             tmp.x--;
  207.         }
  208.         int x_left = tmp.x + 1;
  209.         tmp.x = x;
  210.        
  211.         //проверка для строки выше
  212.         tmp.x = x_left;
  213.         tmp.y++;
  214.         while(tmp.x <= x_right)
  215.         {
  216.             bool flag = false;
  217.             while((GetPixel(hdc,tmp.x,tmp.y) != border) && (GetPixel(hdc,tmp.x,tmp.y) != color) && (tmp.x < x_right))
  218.             {
  219.                 if(flag == false) flag = true;
  220.                 tmp.x++;
  221.             }
  222.             if(flag)
  223.                 if((tmp.x == x_right) && (GetPixel(hdc,tmp.x,tmp.y) != border) && (GetPixel(hdc,tmp.x,tmp.y) != color))
  224.                     Push(points, &count, tmp);
  225.                 else
  226.                     Push(points, &count, Point(tmp.x-1,tmp.y));
  227.             flag = false;
  228.         }
  229.         int x_in = tmp.x;
  230.         while((GetPixel(hdc,tmp.x,tmp.y) != border) && (GetPixel(hdc,tmp.x,tmp.y) != color) && (tmp.x < x_right))
  231.         {
  232.             tmp.x++;
  233.         }
  234.         if(tmp.x == x_in) tmp.x++;
  235.  
  236.         //проверка для строки ниже
  237.         tmp.x = x_left;
  238.         tmp.y--;
  239.         while(tmp.x <= x_right)
  240.         {
  241.             bool flag = false;
  242.             while((GetPixel(hdc,tmp.x,tmp.y) != border) && (GetPixel(hdc,tmp.x,tmp.y) != color) && (tmp.x < x_right))
  243.             {
  244.                 if(flag == false) flag = true;
  245.                 tmp.x++;
  246.             }
  247.             if(flag)
  248.                 if((tmp.x == x_right) && (GetPixel(hdc,tmp.x,tmp.y) != border) && (GetPixel(hdc,tmp.x,tmp.y) != color))
  249.                     Push(points, &count, tmp);
  250.                 else
  251.                     Push(points, &count, Point(tmp.x-1,tmp.y));
  252.             flag = false;
  253.         }
  254.         x_in = tmp.x;
  255.         while((GetPixel(hdc,tmp.x,tmp.y) != border) && (GetPixel(hdc,tmp.x,tmp.y) != color) && (tmp.x < x_right))
  256.         {
  257.             tmp.x++;
  258.         }
  259.         if(tmp.x == x_in) tmp.x++;
  260.     }
  261. }
  262.  
  263. void FloodFill_3(HDC hdc, POINT point, COLORREF color, COLORREF border) //
  264. {
  265. }
  266.  
  267. void FloodFillEx(HDC hdc, POINT point, COLORREF color, COLORREF border, int mode) //Main
  268. {
  269.     switch(mode)
  270.     {
  271.         case 1: FloodFill_1(hdc, point, color, border);
  272.         case 2: FloodFill_2(hdc, point, color, border);
  273.         case 3: FloodFill_3(hdc, point, color, border);
  274.     }
  275. }
  276.  
  277. //
  278. //  ФУНКЦИЯ: WndProc(HWND, UINT, WPARAM, LPARAM)
  279. //
  280. //  НАЗНАЧЕНИЕ:  обрабатывает сообщения в главном окне.
  281. //
  282. //  WM_COMMAND  - обработка меню приложения
  283. //  WM_PAINT    -Закрасить главное окно
  284. //  WM_DESTROY   - ввести сообщение о выходе и вернуться.
  285. //
  286. //
  287. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  288. {
  289.     int wmId, wmEvent;
  290.     PAINTSTRUCT ps;
  291.     HDC hdc;
  292.  
  293.     POINT pt[4];
  294.    
  295.     pt[0] = Point(200, 100);
  296.     pt[1] = Point(300, 100);
  297.     pt[2] = Point(350, 200);
  298.     pt[3] = Point(150, 200);
  299.  
  300.     switch (message)
  301.     {
  302.     case WM_COMMAND:
  303.         wmId    = LOWORD(wParam);
  304.         wmEvent = HIWORD(wParam);
  305.         // Разобрать выбор в меню:
  306.         switch (wmId)
  307.         {
  308.         case IDM_ABOUT:
  309.             DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  310.             break;
  311.         case IDM_EXIT:
  312.             DestroyWindow(hWnd);
  313.             break;
  314.         default:
  315.             return DefWindowProc(hWnd, message, wParam, lParam);
  316.         }
  317.         break;
  318.     case WM_PAINT:
  319.         hdc = BeginPaint(hWnd, &ps);
  320.         // TODO: добавьте любой код отрисовки...
  321.         Polygon(hdc, pt, 4);
  322.         FloodFillEx(hdc, Point(200,150), RGB(255,255,255), RGB(255,255,255), 1);
  323.         EndPaint(hWnd, &ps);
  324.         break;
  325.     case WM_DESTROY:
  326.         PostQuitMessage(0);
  327.         break;
  328.     default:
  329.         return DefWindowProc(hWnd, message, wParam, lParam);
  330.     }
  331.     return 0;
  332. }
  333.  
  334. // Обработчик сообщений для окна "О программе".
  335. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  336. {
  337.     UNREFERENCED_PARAMETER(lParam);
  338.     switch (message)
  339.     {
  340.     case WM_INITDIALOG:
  341.         return (INT_PTR)TRUE;
  342.  
  343.     case WM_COMMAND:
  344.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  345.         {
  346.             EndDialog(hDlg, LOWORD(wParam));
  347.             return (INT_PTR)TRUE;
  348.         }
  349.         break;
  350.     }
  351.     return (INT_PTR)FALSE;
  352. }
Advertisement
Add Comment
Please, Sign In to add comment