Skip to content

Instantly share code, notes, and snippets.

@artemkurylo
Last active December 13, 2023 14:26
Show Gist options
  • Save artemkurylo/7236267c04004ae13ee35fe1a2c2b8bc to your computer and use it in GitHub Desktop.
Save artemkurylo/7236267c04004ae13ee35fe1a2c2b8bc to your computer and use it in GitHub Desktop.
Laba 1
#include <windows.h>
HINSTANCE hInst;
HWND hWnd;
HMENU hMenu;
COLORREF bgColor = RGB(211, 211, 211);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void CreateMainWindow(HINSTANCE hInstance, int nCmdShow);
void CreateMainMenu();
void ShowAboutDialog();
void ShowConfirmationDialog();
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
hInst = hInstance;
CreateMainWindow(hInstance, nCmdShow);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return static_cast<int>(msg.wParam);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE:
break;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
SetTextColor(hdc, RGB(0, 0, 0));
SetBkColor(hdc, bgColor);
RECT rect;
GetClientRect(hWnd, &rect);
DrawText(hdc, L"Системне програмування Лаб №1", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint(hWnd, &ps);
break;
}
case WM_SIZE:
InvalidateRect(hWnd, nullptr, TRUE);
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case 1:
ShowConfirmationDialog();
break;
case 2:
SetWindowPos(hWnd, nullptr, 0, 0, 900, 600, SWP_NOMOVE | SWP_NOZORDER);
break;
case 3:
bgColor = RGB(255, 255, 153); // Жовтий колір
InvalidateRect(hWnd, nullptr, TRUE);
break;
case 4:
SetWindowText(hWnd, L"Новий рядок заголовка");
break;
case 5:
while (true) {
SetCursor(LoadCursor(nullptr, IDC_APPSTARTING));
Sleep(4000);
break;
}
break;
case 6:
ShowAboutDialog();
break;
}
break;
case WM_CLOSE: {
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
void CreateMainWindow(HINSTANCE hInstance, int nCmdShow) {
const wchar_t* className = L"Artem Kurylo";
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, IDI_ERROR);
wc.hCursor = LoadCursor(nullptr, IDC_SIZEWE);
wc.hbrBackground = CreateSolidBrush(bgColor);
wc.lpszClassName = className;
RegisterClass(&wc);
hWnd = CreateWindow(
className,
L"Завдання з системного програмування",
WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
0,
0,
500,
200,
nullptr,
nullptr,
hInstance,
nullptr
);
ShowWindow(hWnd, SW_SHOWMINIMIZED);
UpdateWindow(hWnd);
CreateMainMenu();
}
void CreateMainMenu() {
hMenu = CreateMenu();
HMENU hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, 1, L"Закрити програму");
AppendMenu(hSubMenu, MF_STRING, 2, L"Змінити розмір");
AppendMenu(hSubMenu, MF_STRING, 3, L"Змінити колір фону");
AppendMenu(hSubMenu, MF_STRING, 4, L"Змінити рядок заголовка");
AppendMenu(hSubMenu, MF_STRING, 5, L"Змінити курсор");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT_PTR)hSubMenu, L"Опції");
AppendMenu(hMenu, MF_STRING, 6, L"Про програму");
SetMenu(hWnd, hMenu);
}
void ShowAboutDialog() {
MessageBox(hWnd, L"Інформація про студента: КІУКІ-22-7 Артем Курило, , Бригада 1", L"Про програму", MB_OK | MB_ICONINFORMATION);
}
void ShowConfirmationDialog() {
int result = MessageBox(hWnd, L"Ви впевнені?", L"Підтвердження закриття", MB_YESNO | MB_ICONQUESTION);
if (result == IDYES) {
DestroyWindow(hWnd);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment