1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
| #include "stdafx.h"
#include "sp2.h"
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <commctrl.h>
#include <commdlg.h>
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
DWORD WINAPI ThreadFunc1(PVOID pvParam);
DWORD WINAPI ThreadFunc2(PVOID pvParam);
DWORD WINAPI ThreadFunc3(PVOID pvParam);
HINSTANCE hInst;
int cx = GetSystemMetrics(SM_CXSCREEN);
int cy = GetSystemMetrics(SM_CYSCREEN);
char szMainClassName[] = "MainWindow";
char szChildClassName[] = "ChildWindow";
char szAppTitle[] = "Window";
char Text[] = "0 1 2 3 4 5 6 7 8 9";
struct PARAM {
HWND hWnd;
BOOL stop;
};
HANDLE hThread1, hThread2, hThread3;
DWORD ThreadId1, ThreadId2, ThreadId3;
HWND hWndMain, hWndChild, hWnd;
HANDLE hWTimer, hMutex;
LARGE_INTEGER li;
static int x1 = 0, y1 = 0, x2 = 150, y2 = 120, texth = 10, textw = 8, x1Child = 0, y1Child = 0, x2Child = 30, y2Child = 25;
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
hWTimer = CreateWaitableTimer(NULL, FALSE, "Timer"); //создание объекта ожидаемого таймера с автосбросом
hMutex = CreateMutex(NULL, FALSE, "Mutex"); //создание объекта мьютекса
WNDCLASS wc2, wc1;
MSG msg;
memset(&wc1, 0, sizeof(wc1));
wc1.lpszClassName = szMainClassName;
wc1.lpfnWndProc = (WNDPROC)MainWndProc;
wc1.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc1.hInstance = hInstance;
wc1.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc1.hCursor = LoadCursor(NULL, IDC_ARROW);
wc1.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc1.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wc1.cbClsExtra = 0;
wc1.cbWndExtra = 0;
RegisterClass(&wc1);
memset(&wc2, 0, sizeof(wc2));
wc2.style = NULL;
wc2.lpszClassName = szChildClassName;
wc2.lpfnWndProc = (WNDPROC)ChildWndProc;
wc2.style = CS_HREDRAW | CS_DBLCLKS;
wc2.hInstance = hInstance;
wc2.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc2.hCursor = LoadCursor(NULL, IDC_ARROW);
wc2.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(221, 160, 221));
wc2.lpszMenuName = NULL;
wc2.cbClsExtra = 0;
wc2.cbWndExtra = 0;
RegisterClass(&wc2);
hWndMain = CreateWindow(szMainClassName, szAppTitle, WS_OVERLAPPEDWINDOW | WS_MAXIMIZE, 0, 0, cx, cy, NULL, NULL, hInst, NULL);
ShowWindow(hWndMain, nCmdShow);
UpdateWindow(hWndMain);
int xchild = cx / 3, ychild = cy / 3;
hWndChild = CreateWindow(szChildClassName, szChildClassName, WS_CHILDWINDOW | WS_CAPTION | WS_VISIBLE | WS_THICKFRAME, (cx - xchild) / 15, (cy - ychild) / 1.3, xchild, ychild, hWndMain, NULL, hInstance, NULL);
ShowWindow(hWndChild, nCmdShow);
UpdateWindow(hWndChild);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
CloseHandle(hMutex);
CloseHandle(hWTimer);
return TRUE;
}
// --- Функция главного окна
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static struct PARAM p;
PROCESS_INFORMATION pi;
STARTUPINFO si;
PAINTSTRUCT ps;
HDC hdc;
HBRUSH hRndBrush, hOldRndBrush;
static char NameProcess[256] = "D:/univer/Новая папка/sp2/Debug/sp2.exe"; //путь на экземпляр приложения
switch (msg) {
case WM_PAINT: {
//рисование эллипса
hRndBrush = CreateSolidBrush(RGB(255, 255, 255));
hdc = BeginPaint(hWndMain, &ps);
hOldRndBrush = (HBRUSH)SelectObject(hdc, hRndBrush);
Ellipse(hdc, x1, y1, x2, y2);
SelectObject(hdc, hOldRndBrush);
DeleteObject(hRndBrush); break;
}
case WM_COMMAND: {
switch (LOWORD(wParam)) {
case ID_process: {
if (MessageBox(NULL, "Запустить экземпляр приложения?", "Подтверждение", MB_YESNO) == IDYES) { //подтверждение на запуск экземпляра
ZeroMemory(&si, sizeof(STARTUPINFO));
CreateProcess(NameProcess, NULL, NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi); //запуск экземпляра
}
}; break;
case ID_potok1_start: {
hThread1 = CreateThread(NULL, 0, ThreadFunc1, (PVOID)(&p), CREATE_SUSPENDED, &ThreadId1);
ResumeThread(hThread1); break;
}
case ID_potok1_stop: {
SuspendThread(hThread1); break;
}
case ID_potok2_start: {
UpdateWindow(hWndChild);
ResumeThread(hThread2); break;
}
case ID_potok2_stop: {
SuspendThread(hThread2); break;
}
case ID_potok3_start: {
UpdateWindow(hWndChild);
ResumeThread(hThread3); break;
}
case ID_potok3_stop: {
SuspendThread(hThread3); break;
}
case ID_exit: {
PostQuitMessage(0); break;
}}}; break;
default: return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0l;
}
// Функция потока1 эллипс , мьютекс, 6 повторений
DWORD WINAPI ThreadFunc1(PVOID pvParam){
DWORD dwResult = 0;
struct PARAM *p = (struct PARAM *)pvParam;
HDC hDC = GetDC(p->hWnd), hCompatibleDC;
BITMAP Bitmap;
int k = 1;
RECT LpRect;
GetClientRect(hWndMain, &LpRect);
while (!p->stop) {
WaitForSingleObject(hMutex, 100);
for (int i = 0; i < 3; i++) {
switch (k) {
case 1: {
if (x2 >= LpRect.bottom && y2 >= LpRect.bottom) { k++; break; }
x1 += 28; y1 += 13; x2 += 28; y2 += 13;
Sleep(50);
InvalidateRect(p->hWnd, NULL, TRUE);
}; break;
case 2: {
if (x1 == 0) { k = 1; break; }
x1 -= 28; y1 -= 13; x2 -= 28; y2 -= 13;
Sleep(50);
InvalidateRect(p->hWnd, NULL, TRUE);
}; break;
}
}
ReleaseMutex(hMutex);
ReleaseDC(p->hWnd, hDC);
}
return(dwResult);
}
// --- Функция дочернего окна
LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static struct PARAM p; // структура для управления потоком
PAINTSTRUCT ps, ps2;
HDC hdc, hdc2;
HBRUSH hRndBrush, hOldRndBrush;
static int textx, texty;
RECT LpRect;
GetClientRect(hWndChild, &LpRect);
int bottomChild = LpRect.bottom, rightChild = LpRect.right;
switch (msg) {
case WM_PAINT: {
//рисование эллипса
hRndBrush = CreateSolidBrush(RGB(255, 255, 255));
hdc2 = BeginPaint(hWndChild, &ps2);
hOldRndBrush = (HBRUSH)SelectObject(hdc2, hRndBrush);
Ellipse(hdc2, x1Child, y1Child, x2Child, y2Child);
SelectObject(hdc2, hOldRndBrush);
DeleteObject(hRndBrush);
//рисование цифер
hdc = BeginPaint(hWndChild, &ps);
HFONT font = CreateFont(texth, textw, 0, 0, 0, 0, 0, 0, RUSSIAN_CHARSET, 0, 0, 0, 0, "Times New Roman");
SelectObject(hdc, font);
textx = (rightChild / 2.5) - texth * 4;
texty = (bottomChild / 2) - textw;
TextOut(hdc, textx, texty, Text, strlen(Text));
EndPaint(hWndChild, &ps);
DeleteObject(hdc);
break;
}
case WM_CREATE: {
p.hWnd = hWnd; p.stop = 0;
li.QuadPart = -(5000 * 10000);
SetWaitableTimer(hWTimer, &li, 0, NULL, NULL, FALSE);
hThread2 = CreateThread(NULL, 0, ThreadFunc2, (PVOID)(&p), CREATE_SUSPENDED, &ThreadId2);
li.QuadPart = -(5000 * 10000);
SetWaitableTimer(hWTimer, &li, 0, NULL, NULL, FALSE);
hThread3 = CreateThread(NULL, 0, ThreadFunc3, (PVOID)(&p), CREATE_SUSPENDED, &ThreadId3); break;
}
case WM_DESTROY: {
DestroyWindow(hWnd); break;
}
default: return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0l;
}
// -- функция потока 2, ожидаемый таймер, 10 повторений
DWORD WINAPI ThreadFunc2(PVOID pvParam)
{
DWORD dwResult = 0;
struct PARAM *p = (struct PARAM *)pvParam;
HDC hDC = GetDC(p->hWnd), hCompatibleDC;
int k = 1;
while (1) {
switch (k) {
case 1: {
if (texth >= 20) { k++; break; }
texth += 1; textw += 1;
Sleep(150);
InvalidateRect(p->hWnd, NULL, TRUE);
}; break;
case 2: {
if (texth <= 8) { k = 1; break; }
texth -= 1; textw -= 1;
Sleep(150);
InvalidateRect(p->hWnd, NULL, TRUE);
}; break;
}
}
ReleaseDC(p->hWnd, hDC);
return(dwResult);
}
//поток 3, эллипс, ожидаеый таймер, 8 повторений
DWORD WINAPI ThreadFunc3(PVOID pvParam) {
DWORD dwResult = 0;
struct PARAM *p = (struct PARAM *)pvParam;
HDC hDC = GetDC(p->hWnd), hCompatibleDC;
int k = 1;
RECT LpRect;
GetClientRect(hWndChild, &LpRect);
while (1) {
switch (k) {
case 1: {
if (x2Child >= LpRect.bottom && y2Child >= LpRect.bottom) { k++; break; }
x1Child += 2; y1Child += 1; x2Child += 2; y2Child += 1;
Sleep(100);
InvalidateRect(p->hWnd, NULL, TRUE);
}; break;
case 2: {
if (x1Child == 0) { k = 1; break; }
x1Child -= 2; y1Child -= 1; x2Child -= 2; y2Child -= 1;
Sleep(100);
InvalidateRect(p->hWnd, NULL, TRUE);
}; break;
}
}
ReleaseDC(p->hWnd, hDC);
return(dwResult);
} |