Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <iostream>
- #include <fstream>
- #include <Windowsx.h>
- using namespace std;
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- HWND listbox, b1, b2, hchildWnd1, hchildWnd2, hchildWnd3;
- int count_s = 0;
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- HWND hMainWnd = FindWindow("AppClass", "Лабораторная работа 2");
- MSG msg;
- WNDCLASSEX wc;
- if (hMainWnd) {
- MessageBox(NULL, "Ошибка!", "Приложение уже запущено!", MB_ICONERROR);
- SetForegroundWindow(hMainWnd);
- }
- else {
- char szClassName[] = "AppClass";
- wc.cbSize = sizeof(wc);
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
- wc.lpszMenuName = NULL;
- wc.lpszClassName = szClassName;
- wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- if (!RegisterClassEx(&wc)) {
- MessageBox(NULL, "Не удалось зарегистрировать класс окна", "Ошибка", MB_OK);
- return 0;
- }
- hMainWnd = CreateWindow(szClassName, "Лабораторная работа 2", WS_SYSMENU | WS_MAXIMIZE | DS_CENTER, 300, 200, 300, 300, NULL, NULL, hInstance, NULL);
- if (!hMainWnd) {
- MessageBox(NULL, "Не удалось создать главное окно программы", "Ошибка", MB_OK);
- return 0;
- }
- ShowWindow(hMainWnd, nCmdShow);
- }
- UpdateWindow(hMainWnd);
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- HDC hDC;
- PAINTSTRUCT ps;
- RECT rect;
- switch (uMsg) {
- case WM_CREATE: {
- //Создаем элементы
- hchildWnd1 = CreateWindow("listbox", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | LBS_DISABLENOSCROLL, 10, 20, 175, 220, hWnd, NULL, NULL, NULL);
- hchildWnd2 = CreateWindow("BUTTON", "Вверх", WS_CHILD | WS_VISIBLE | WS_BORDER, 200, 20, 70, 30, hWnd, (HMENU)1, NULL, NULL);
- hchildWnd3 = CreateWindow("BUTTON", "Вниз", WS_CHILD | WS_VISIBLE | WS_BORDER, 200, 60, 70, 30, hWnd, (HMENU)2, NULL, NULL);
- //Загружаем элементы в листбокс
- ifstream fin("list.txt");
- char buff[100];
- //Выполняем пока есть строки в файле
- do {
- fin.getline(buff, 100);
- //Проверка на пустую строку
- if (strlen(buff) > 0) {
- ListBox_AddString(hchildWnd1, buff);
- count_s++;
- }
- } while (strlen(buff) > 0);
- //Начальные позиции курсора
- ListBox_SetCurSel(hchildWnd1, 0);
- Button_Enable(hchildWnd2, 0);
- break;
- }
- case WM_COMMAND: {
- //Активируем все кнопки
- Button_Enable(hchildWnd2, 1);
- Button_Enable(hchildWnd3, 1);
- //Двигаем вниз
- if (LOWORD(wParam) == 2) {
- ListBox_SetCurSel(hchildWnd1, ListBox_GetCurSel(hchildWnd1) + 1);
- }
- else if (LOWORD(wParam) == 1) {
- ListBox_SetCurSel(hchildWnd1, ListBox_GetCurSel(hchildWnd1) - 1);
- }
- //Отключаем кнопку вверх, если индекс 0
- if (ListBox_GetCurSel(hchildWnd1) == 0) {
- Button_Enable(hchildWnd2, 0);
- }
- //Отключаем кнопку вниз, если индекс count_s
- if (ListBox_GetCurSel(hchildWnd1) == count_s-1) {
- Button_Enable(hchildWnd3, 0);
- }
- break;
- }
- case WM_CLOSE: {
- DestroyWindow(hWnd); //посылает окну сообщение WM_DESTROY, после которого окна на экране уже нет
- break;
- }
- case WM_DESTROY: {
- PostQuitMessage(0); //посылает сообщение WM_QUIT, после которого прекращается цикл обработки сообщений
- break;
- }
- default: {
- return DefWindowProc(hWnd, uMsg, wParam, lParam); //обработка сообщений по умолчанию
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment