505 lines
13 KiB
C++
505 lines
13 KiB
C++
// WindowsProject1.cpp : Определяет точку входа для приложения.
|
||
//
|
||
|
||
#include "framework.h"
|
||
#include "WindowsProject1.h"
|
||
#define MAX_LOADSTRING 100
|
||
|
||
// Глобальные переменные:
|
||
HINSTANCE hInst; // текущий экземпляр
|
||
WCHAR szTitle[MAX_LOADSTRING]; // Текст строки заголовка
|
||
WCHAR 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 wWinMain(_In_ HINSTANCE hInstance,
|
||
_In_opt_ HINSTANCE hPrevInstance,
|
||
_In_ LPWSTR lpCmdLine,
|
||
_In_ int nCmdShow)
|
||
{
|
||
UNREFERENCED_PARAMETER(hPrevInstance);
|
||
UNREFERENCED_PARAMETER(lpCmdLine);
|
||
|
||
// TODO: Разместите код здесь.
|
||
|
||
// Инициализация глобальных строк
|
||
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
|
||
LoadStringW(hInstance, IDC_WINDOWSPROJECT1, szWindowClass, MAX_LOADSTRING);
|
||
MyRegisterClass(hInstance);
|
||
|
||
// Выполнить инициализацию приложения:
|
||
if (!InitInstance(hInstance, nCmdShow))
|
||
{
|
||
return FALSE;
|
||
}
|
||
|
||
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINDOWSPROJECT1));
|
||
|
||
MSG msg;
|
||
|
||
// Цикл основного сообщения:
|
||
while (GetMessage(&msg, nullptr, 0, 0))
|
||
{
|
||
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
|
||
{
|
||
TranslateMessage(&msg);
|
||
DispatchMessage(&msg);
|
||
}
|
||
}
|
||
|
||
return (int)msg.wParam;
|
||
}
|
||
|
||
|
||
|
||
//
|
||
// ФУНКЦИЯ: MyRegisterClass()
|
||
//
|
||
// ЦЕЛЬ: Регистрирует класс окна.
|
||
//
|
||
ATOM MyRegisterClass(HINSTANCE hInstance)
|
||
{
|
||
WNDCLASSEXW 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_WINDOWSPROJECT1));
|
||
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
|
||
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
|
||
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINDOWSPROJECT1);
|
||
wcex.lpszClassName = szWindowClass;
|
||
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
|
||
|
||
return RegisterClassExW(&wcex);
|
||
}
|
||
|
||
//
|
||
// ФУНКЦИЯ: InitInstance(HINSTANCE, int)
|
||
//
|
||
// ЦЕЛЬ: Сохраняет маркер экземпляра и создает главное окно
|
||
//
|
||
// КОММЕНТАРИИ:
|
||
//
|
||
// В этой функции маркер экземпляра сохраняется в глобальной переменной, а также
|
||
// создается и выводится главное окно программы.
|
||
//
|
||
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
|
||
{
|
||
hInst = hInstance; // Сохранить маркер экземпляра в глобальной переменной
|
||
|
||
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
|
||
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
|
||
|
||
if (!hWnd)
|
||
{
|
||
return FALSE;
|
||
}
|
||
|
||
ShowWindow(hWnd, nCmdShow);
|
||
UpdateWindow(hWnd);
|
||
|
||
return TRUE;
|
||
}
|
||
void Image0(HDC hdc, int cx, int cy, COLORREF color) {
|
||
HPEN hPen;
|
||
hPen = CreatePen(PS_SOLID, 2, color);
|
||
SelectObject(hdc, hPen);
|
||
|
||
POINT p[4] = {
|
||
cx, cy + 20,
|
||
cx + 20, cy - 20,
|
||
cx - 20, cy - 20,
|
||
cx, cy + 20
|
||
};
|
||
|
||
|
||
|
||
|
||
Polyline(hdc, p, 4);
|
||
DeleteObject(hPen);
|
||
}
|
||
|
||
void Image1(HDC hdc, int cx, int cy, COLORREF color) {
|
||
HPEN hPen = CreatePen(PS_SOLID, 2, color);
|
||
SelectObject(hdc, hPen);
|
||
|
||
POINT p[4] = {
|
||
cx, cy - 20,
|
||
cx + 20, cy + 20,
|
||
cx - 20, cy + 20,
|
||
cx, cy - 20
|
||
};
|
||
Polyline(hdc, p, 4);
|
||
|
||
DeleteObject(hPen);
|
||
}
|
||
void Image2(HDC hdc, int cx, int cy, COLORREF color) {
|
||
HPEN hPen;
|
||
hPen = CreatePen(PS_SOLID, 2, color);
|
||
SelectObject(hdc, hPen);
|
||
POINT p[5] = {
|
||
cx, cy - 20,
|
||
cx + 20, cy,
|
||
cx, cy + 20,
|
||
cx - 20 , cy ,
|
||
cx, cy - 20
|
||
};
|
||
Polyline(hdc, p, 5);
|
||
DeleteObject(hPen);
|
||
}
|
||
void Image3(HDC hdc, int cx, int cy, COLORREF color) {
|
||
HPEN hPen;
|
||
hPen = CreatePen(PS_SOLID, 2, color);
|
||
SelectObject(hdc, hPen);
|
||
POINT p[5] = {
|
||
cx + 20, cy - 20,
|
||
cx , cy + 20,
|
||
cx - 20, cy - 20,
|
||
cx,cy,
|
||
cx + 20, cy - 20,
|
||
};
|
||
Polyline(hdc, p, 5);
|
||
DeleteObject(hPen);
|
||
}
|
||
void Image4(HDC hdc, int cx, int cy, COLORREF color) {
|
||
HPEN hPen;
|
||
hPen = CreatePen(PS_SOLID, 2, color);
|
||
SelectObject(hdc, hPen);
|
||
POINT p[7] = {
|
||
cx - 20,cy,
|
||
cx - 20,cy - 20,
|
||
cx + 20,cy - 20,
|
||
cx,cy - 10,
|
||
cx + 20,cy,
|
||
cx - 20,cy,
|
||
cx - 20,cy + 20,
|
||
};
|
||
Polyline(hdc, p, 7);
|
||
DeleteObject(hPen);
|
||
}
|
||
void Image5(HDC hdc, int cx, int cy, COLORREF color) {
|
||
HPEN hPen;
|
||
hPen = CreatePen(PS_SOLID, 2, color);
|
||
SelectObject(hdc, hPen);
|
||
POINT p[6] = {
|
||
cx - 20,cy - 20,
|
||
cx,cy,
|
||
cx + 20,cy - 20,
|
||
cx + 20,cy + 20,
|
||
cx - 20, cy + 20,
|
||
cx - 20,cy - 20,
|
||
};
|
||
Polyline(hdc, p, 6);
|
||
DeleteObject(hPen);
|
||
}
|
||
void Image6(HDC hdc, int cx, int cy, COLORREF color) {
|
||
HPEN hPen;
|
||
hPen = CreatePen(PS_SOLID, 2, color);
|
||
SelectObject(hdc, hPen);
|
||
POINT p[10] = {
|
||
cx - 20,cy + 20,
|
||
cx - 20,cy - 20,
|
||
cx + 20,cy - 20,
|
||
cx + 20, cy + 20,
|
||
cx - 10, cy + 20,
|
||
cx - 10,cy - 10,
|
||
cx + 10,cy - 10,
|
||
cx + 10, cy + 10,
|
||
cx, cy + 10,
|
||
cx, cy,
|
||
};
|
||
Polyline(hdc, p, 10);
|
||
DeleteObject(hPen);
|
||
}
|
||
void Image7(HDC hdc, int cx, int cy, COLORREF color) {
|
||
HPEN hPen;
|
||
hPen = CreatePen(PS_SOLID, 2, color);
|
||
SelectObject(hdc, hPen);
|
||
POINT p[12] = {
|
||
cx ,cy + 20,
|
||
cx + 10, cy + 20,
|
||
cx + 10, cy,
|
||
cx + 20,cy,
|
||
cx + 20,cy - 10,
|
||
cx + 10,cy - 10,
|
||
cx + 5,cy - 40,
|
||
cx,cy - 10,
|
||
cx - 10,cy - 10,
|
||
cx - 10,cy,
|
||
cx,cy,
|
||
cx ,cy + 20,
|
||
};
|
||
Polyline(hdc, p, 12);
|
||
DeleteObject(hPen);
|
||
}
|
||
void Image8(HDC hdc, int x, int y, COLORREF color) {
|
||
HPEN hPen;
|
||
hPen = CreatePen(PS_SOLID, 2, color);
|
||
SelectObject(hdc, hPen);
|
||
POINT pt[8];
|
||
pt[0].x = 150;
|
||
pt[0].y = 50;
|
||
pt[1].x = 400;
|
||
pt[1].y = 50;
|
||
pt[2].x = 400;
|
||
pt[2].y = 300;
|
||
pt[3].x = 300;
|
||
pt[3].y = 300;
|
||
pt[4].x = 300;
|
||
pt[4].y = 100;
|
||
pt[5].x = 250;
|
||
pt[5].y = 100;
|
||
pt[6].x = 250;
|
||
pt[6].y = 300;
|
||
pt[7].x = 150;
|
||
pt[7].y = 300;
|
||
Polygon(hdc, pt, 8);
|
||
}
|
||
//
|
||
// ФУНКЦИЯ: WndProc(HWND, UINT, WPARAM, LPARAM)
|
||
//
|
||
// ЦЕЛЬ: Обрабатывает сообщения в главном окне.
|
||
//
|
||
// WM_COMMAND - обработать меню приложения
|
||
// WM_PAINT - Отрисовка главного окна
|
||
// WM_DESTROY - отправить сообщение о выходе и вернуться
|
||
//
|
||
//
|
||
enum Modes {
|
||
image0,
|
||
image1,
|
||
image1_line,
|
||
image1_with_colors,
|
||
image2,
|
||
image2_with_colors,
|
||
image3,
|
||
image3_lines,
|
||
image3_with_colors,
|
||
image4,
|
||
image4_lines,
|
||
image4_with_colors,
|
||
image5,
|
||
image6,
|
||
image7,
|
||
image8,
|
||
mode_none
|
||
};
|
||
enum Modes mode = image0;
|
||
|
||
void Picture0(HDC hdc) {
|
||
Image0(hdc, 40, 40, RGB(255, 0, 0)); // Код был в лекции
|
||
Image0(hdc, 140, 40, RGB(255, 0, 0));
|
||
Image0(hdc, 240, 40, RGB(255, 0, 0));
|
||
Image0(hdc, 240, 240, RGB(255, 0, 0));
|
||
Image0(hdc, 40, 240, RGB(255, 0, 0));
|
||
}
|
||
|
||
|
||
void Picture1(HDC hdc) {
|
||
Image1(hdc, 40, 40, RGB(255, 0, 0));
|
||
Image1(hdc, 140, 40, RGB(255, 0, 0));
|
||
Image1(hdc, 240, 40, RGB(255, 0, 0));
|
||
Image1(hdc, 240, 240, RGB(255, 0, 0));
|
||
Image1(hdc, 40, 240, RGB(255, 0, 0));
|
||
}
|
||
void Picture2(HDC hdc) {
|
||
Image2(hdc, 40, 40, RGB(255, 0, 0));
|
||
Image2(hdc, 140, 40, RGB(255, 0, 0));
|
||
Image2(hdc, 240, 40, RGB(255, 0, 0));
|
||
Image2(hdc, 240, 240, RGB(255, 0, 0));
|
||
Image2(hdc, 40, 240, RGB(255, 0, 0));
|
||
}
|
||
void Picture3(HDC hdc) {
|
||
Image3(hdc, 40, 40, RGB(255, 0, 0));
|
||
Image3(hdc, 140, 40, RGB(255, 0, 0));
|
||
Image3(hdc, 240, 40, RGB(255, 0, 0));
|
||
Image3(hdc, 240, 240, RGB(255, 0, 0));
|
||
Image3(hdc, 40, 240, RGB(255, 0, 0));
|
||
}
|
||
void Picture4(HDC hdc) {
|
||
Image4(hdc, 40, 40, RGB(255, 0, 0));
|
||
Image4(hdc, 140, 40, RGB(255, 0, 0));
|
||
Image4(hdc, 240, 40, RGB(255, 0, 0));
|
||
Image4(hdc, 240, 240, RGB(255, 0, 0));
|
||
Image4(hdc, 40, 240, RGB(255, 0, 0));
|
||
}
|
||
void PictureFLineY(HDC hdc, void(*pfImage)(HDC hdc, int cx, int cy, COLORREF color)) {
|
||
short x = 40;
|
||
short y = 40;
|
||
char i = 0;
|
||
while (i < 6) {
|
||
pfImage(hdc, x, y, RGB(255, 0, 0));
|
||
y += 50;
|
||
i++;
|
||
}
|
||
}
|
||
void PictureFColorLines(HDC hdc, void(*pfImage)(HDC hdc, int cx, int cy, COLORREF color)) {
|
||
unsigned char r = 255;
|
||
unsigned char g = 0;
|
||
short y = 40;
|
||
char i = 0;
|
||
while (i < 6) {
|
||
short x = 40;
|
||
char j = 0;
|
||
unsigned char b = 0;
|
||
while (j < 10) {
|
||
pfImage(hdc, x, y, RGB(r, g, b));
|
||
x += 50;
|
||
j++;
|
||
b += 25;
|
||
}
|
||
y += 50;
|
||
i++;
|
||
g += 50;
|
||
}
|
||
}
|
||
void PictureFLines(HDC hdc, void(*pfImage)(HDC hdc, int cx, int cy, COLORREF color)) {
|
||
short y = 40;
|
||
char i = 0;
|
||
while (i < 6) {
|
||
short x = 40;
|
||
char j = 0;
|
||
while (j < 10) {
|
||
pfImage(hdc, x, y, RGB(255, 0, 0));
|
||
x += 50;
|
||
j++;
|
||
}
|
||
y += 50;
|
||
i++;
|
||
}
|
||
}
|
||
void PictureFLineX(HDC hdc, void(*per)(HDC hdc, int cx, int cy, COLORREF color)) {
|
||
short x = 40;
|
||
short y = 40;
|
||
char j = 0;
|
||
while (j < 10) {
|
||
per(hdc, x, y, RGB(255, 0, 0));
|
||
x += 50;
|
||
j++;
|
||
}
|
||
}
|
||
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
|
||
{
|
||
switch (message)
|
||
{
|
||
case WM_COMMAND:
|
||
{
|
||
int wmId = LOWORD(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:
|
||
{
|
||
PAINTSTRUCT ps;
|
||
HDC hdc = BeginPaint(hWnd, &ps);
|
||
// TODO: Добавьте сюда любой код прорисовки, использующий HDC...
|
||
switch (mode) {
|
||
case image0:
|
||
Picture0(hdc); // Код выше
|
||
break;
|
||
case image1:
|
||
Picture1(hdc); // Код выше
|
||
break;
|
||
case image1_line:
|
||
PictureFLineY(hdc, Image1); // Код был в лекции
|
||
break;
|
||
case image1_with_colors:
|
||
PictureFColorLines(hdc, Image1); // Код был в лекции
|
||
break;
|
||
case image2:
|
||
Picture2(hdc);
|
||
break;
|
||
case image2_with_colors:
|
||
PictureFColorLines(hdc, Image2); // Код был в лекции
|
||
break;
|
||
case image3:
|
||
PictureFLineX(hdc, Image3);
|
||
break;
|
||
case image3_lines:
|
||
PictureFLines(hdc, Image3);
|
||
break;
|
||
case image3_with_colors:
|
||
PictureFColorLines(hdc, Image3); // Код был в лекции
|
||
break;
|
||
case image4:
|
||
PictureFLineX(hdc, Image4);
|
||
break;
|
||
case image4_lines:
|
||
PictureFLines(hdc, Image4);
|
||
break;
|
||
case image4_with_colors:
|
||
PictureFColorLines(hdc, Image4); // Код был в лекции
|
||
break;
|
||
case image5:
|
||
Image5(hdc, 40, 40, RGB(255, 0, 0));
|
||
break;
|
||
case image6:
|
||
Image6(hdc, 40, 40, RGB(255, 0, 0));
|
||
break;
|
||
case image7:
|
||
Image7(hdc, 40, 40, RGB(255, 0, 0));
|
||
break;
|
||
case image8:
|
||
Image8(hdc, 40, 40, RGB(255, 0, 0));
|
||
break;
|
||
}
|
||
|
||
EndPaint(hWnd, &ps);
|
||
|
||
}
|
||
break;
|
||
case WM_KEYDOWN:
|
||
mode = (enum Modes)(mode + 1); // переключение на следующий режим
|
||
if (mode == mode_none) mode = image0; // если режимы закончились - начинаем заново
|
||
InvalidateRect(hWnd, NULL, TRUE);
|
||
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;
|
||
}
|
||
|