alexx876

Untitled

Nov 27th, 2018
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <Windowsx.h>
  5.  
  6. using namespace std;
  7.  
  8. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  9. HWND listbox, b1, b2, hchildWnd1, hchildWnd2, hchildWnd3;
  10.  
  11. int count_s = 0;
  12.  
  13. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  14. {
  15.     HWND hMainWnd = FindWindow("AppClass", "Лабораторная работа 2");
  16.  
  17.     MSG msg;
  18.     WNDCLASSEX wc;
  19.  
  20.     if (hMainWnd) {
  21.         MessageBox(NULL, "Ошибка!", "Приложение уже запущено!", MB_ICONERROR);
  22.         SetForegroundWindow(hMainWnd);
  23.     }
  24.     else {
  25.         char szClassName[] = "AppClass";
  26.         wc.cbSize = sizeof(wc);
  27.         wc.style = CS_HREDRAW | CS_VREDRAW;
  28.         wc.lpfnWndProc = WndProc;
  29.         wc.cbClsExtra = 0;
  30.         wc.cbWndExtra = 0;
  31.         wc.hInstance = hInstance;
  32.         wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  33.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  34.         wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
  35.         wc.lpszMenuName = NULL;
  36.         wc.lpszClassName = szClassName;
  37.         wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  38.         if (!RegisterClassEx(&wc)) {
  39.             MessageBox(NULL, "Не удалось зарегистрировать класс окна", "Ошибка", MB_OK);
  40.             return 0;
  41.         }
  42.  
  43.         hMainWnd = CreateWindow(szClassName, "Лабораторная работа 2", WS_SYSMENU | WS_MAXIMIZE | DS_CENTER, 300, 200, 300, 300, NULL, NULL, hInstance, NULL);
  44.  
  45.         if (!hMainWnd) {
  46.             MessageBox(NULL, "Не удалось создать главное окно программы", "Ошибка", MB_OK);
  47.             return 0;
  48.         }
  49.  
  50.  
  51.         ShowWindow(hMainWnd, nCmdShow);
  52.     }
  53.     UpdateWindow(hMainWnd);
  54.  
  55.  
  56.     while (GetMessage(&msg, NULL, 0, 0)) {
  57.         TranslateMessage(&msg);
  58.         DispatchMessage(&msg);
  59.     }
  60.  
  61.     return msg.wParam;
  62. }
  63.  
  64. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  65. {
  66.     HDC hDC;
  67.     PAINTSTRUCT ps;
  68.     RECT rect;
  69.  
  70.     switch (uMsg) {
  71.     case WM_CREATE: {
  72.         //Создаем элементы
  73.         hchildWnd1 = CreateWindow("listbox", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | LBS_DISABLENOSCROLL, 10, 20, 175, 220, hWnd, NULL, NULL, NULL);
  74.         hchildWnd2 = CreateWindow("BUTTON", "Вверх", WS_CHILD | WS_VISIBLE | WS_BORDER, 200, 20, 70, 30, hWnd, (HMENU)1, NULL, NULL);
  75.         hchildWnd3 = CreateWindow("BUTTON", "Вниз", WS_CHILD | WS_VISIBLE | WS_BORDER, 200, 60, 70, 30, hWnd, (HMENU)2, NULL, NULL);
  76.        
  77.         //Загружаем элементы в листбокс
  78.         ifstream fin("list.txt");
  79.         char buff[100];
  80.         //Выполняем пока есть строки в файле
  81.         do {
  82.             fin.getline(buff, 100);
  83.             //Проверка на пустую строку
  84.             if (strlen(buff) > 0) {
  85.                 ListBox_AddString(hchildWnd1, buff);
  86.                 count_s++;
  87.             }
  88.         } while (strlen(buff) > 0);
  89.  
  90.         //Начальные позиции курсора
  91.         ListBox_SetCurSel(hchildWnd1, 0);
  92.         Button_Enable(hchildWnd2, 0);
  93.  
  94.         break;
  95.     }
  96.     case WM_COMMAND: {
  97.         //Активируем все кнопки
  98.         Button_Enable(hchildWnd2, 1);
  99.         Button_Enable(hchildWnd3, 1);
  100.         //Двигаем вниз
  101.         if (LOWORD(wParam) == 2) {
  102.             ListBox_SetCurSel(hchildWnd1, ListBox_GetCurSel(hchildWnd1) + 1);
  103.         }
  104.         else if (LOWORD(wParam) == 1) {
  105.             ListBox_SetCurSel(hchildWnd1, ListBox_GetCurSel(hchildWnd1) - 1);
  106.         }
  107.         //Отключаем кнопку вверх, если индекс 0
  108.         if (ListBox_GetCurSel(hchildWnd1) == 0) {
  109.             Button_Enable(hchildWnd2, 0);
  110.         }
  111.         //Отключаем кнопку вниз, если индекс count_s
  112.         if (ListBox_GetCurSel(hchildWnd1) == count_s-1) {
  113.             Button_Enable(hchildWnd3, 0);
  114.         }
  115.         break;
  116.     }
  117.     case WM_CLOSE: {
  118.         DestroyWindow(hWnd);    //посылает окну сообщение WM_DESTROY, после которого окна на экране уже нет
  119.         break;
  120.     }
  121.     case WM_DESTROY: {
  122.         PostQuitMessage(0);     //посылает сообщение WM_QUIT, после которого прекращается цикл обработки сообщений
  123.         break;
  124.     }
  125.     default: {
  126.         return DefWindowProc(hWnd, uMsg, wParam, lParam); //обработка сообщений по умолчанию
  127.     }
  128.     }
  129.  
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment