include stdafx include windowsx include AtlConv include conio class tr

  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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "stdafx.h"
#include <windowsx.h>
#include <AtlConv.h>
#include <conio.h>
class triangle
{
int color_of_screen[3], color_of_line[3],type_of_line,thickness_of_line,color_of_fill[3], type_of_fill ;
int x1,x2,y1,y2,x3,y3;
public:
void set_coordinate(int, int, int, int, int, int);
void set_screen(int[]);
void set_line(int [],int);
void set_fill(int []);
void get_coordinate(int*, int*, int*, int*,int*,int*);
void get_screen(int []);
void get_line(int [],int*);
void get_fill(int [], int*);
void draw_triangle(HDC);
void moving(int,int);
int save();
int load();
int prov();
void print_parametrs();
};
void triangle::set_coordinate(int a1, int b1, int a2, int b2, int a3, int b3)
{
x1=a1;
y1=b1;
x2=a2;
y2=b2;
x3=a3;
y3=b3;
}
void triangle::set_line(int color[], int thickness)
{
for(int i=0; i<3;i++)
color_of_line[i]=color[i];
thickness_of_line=thickness;
}
void triangle::set_fill(int color[3])
{
for(int i=0; i<3; i++)
color_of_fill[i]=color[i];
}
void triangle::set_screen(int color[])
{
for(int i=0; i<3;i++)
color_of_screen[i]=color[i];
}
void triangle::get_coordinate(int *a1, int *b1, int *a2, int *b2, int *a3, int *b3)
{
*a1=x1;
*b1=y1;
*a2=x2;
*b2=y2;
*a3=x3;
*b3=y3;
}
void triangle::get_line(int color[], int *thickness)
{
for(int i=0; i<3;i++)
color[i]=color_of_line[i];
*thickness=thickness_of_line;
}
void triangle::get_fill(int color[], int *type)
{
for(int i=0; i<3;i++)
color[i]=color_of_fill[i];
*type=type_of_fill;
}
void triangle::get_screen(int color[])
{
for(int i=0; i<3; i++)
color[i]=color_of_screen[i];
}
void triangle::draw_triangle(HDC hdc)
{
HPEN hPen;
HBRUSH hBrush;
hPen = CreatePen(PS_SOLID, thickness_of_line, RGB(color_of_line[0],color_of_line[1],color_of_line[2]));
HPEN hOldPen = SelectPen (hdc, hPen);
hBrush = CreateSolidBrush(RGB(color_of_fill[0],color_of_fill[1],color_of_fill[2]));
HBRUSH hOldBrush = SelectBrush(hdc, hBrush);
POINT ppt[3]={{x1,y1},{x2,y2},{x3,y3}};
Polygon(hdc,ppt,3);
SelectPen(hdc, hOldPen);
SelectBrush(hdc, hOldBrush);
DeletePen(hPen);
DeleteBrush(hBrush);
}
void triangle::moving(int dx,int dy)
{
x1=x1+dx;
y1=y1+dy;
x2=x2+dx;
y2=y2+dy;
x3=x3+dx;
y3=y3+dy;
}
int triangle::prov()
{
if((x1==y2)&&(x2==y1) && (x3==y3))
return 0;
else return 1;
}
int triangle::save()// сохранение фигуры в файл
{
FILE *fileIN;
int i; char filename[10];
printf("\nEnter file name: ");
scanf_s("%s", &filename);
if((fileIN=fopen(filename,"w")) == NULL)
{ getch(); return 0;}
fprintf(fileIN,"\n%d %d %d %d",x1,y1,x2,y2,x3,y3);
fprintf(fileIN, "\n");
for(i=0;i<3;i++){
fprintf (fileIN,"%d ",color_of_screen[i]);
}
fprintf(fileIN, "\n");
for(i=0; i<3; i++){
fprintf (fileIN,"%d ",color_of_line);
}
fprintf (fileIN,"\n%d",thickness_of_line);
fprintf(fileIN, "\n");
for(i=0; i<3;i++)
fprintf (fileIN,"%d ",color_of_fill[i]);
fclose(fileIN);
return 1;
}
int triangle::load()// загрузка фигуры с файла
{
FILE *fileOUT;
int i;
char filename[10];
printf("\n Enter file name: ");
scanf("%s", &filename);
if((fileOUT=fopen(filename,"r")) == NULL)
{ getch(); return 0;}
fscanf(fileOUT," %d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
for(i=0;i<3;i++)
fscanf (fileOUT,"%d",&color_of_screen[i]);
for(i=0;i<3;i++)
fscanf (fileOUT,"%d", &color_of_line[i]);
fscanf (fileOUT,"%d", &thickness_of_line);
for(i=0;i<3;i++)
fscanf (fileOUT,"%d", &color_of_fill[i]);
fclose(fileOUT);
return 1;
}
void triangle:: print_parametrs()// вывод параметров на экран
{
int i;
printf("First point=(%d;%d)\nSecond point=(%d;%d)\nThird point=(%d;%d)\n",x1,y1,x2,y2,x3,y3);
printf("\nColor of the screen: ");
for(i=0;i<3;i++)
printf ("%d ",color_of_screen[i]);
printf("\nColor of line: ");
for(i=0;i<3;i++)
printf ("%d ", color_of_line[i]);
printf ("\nThikness of line=%d", thickness_of_line);
printf("\nColor of fill: ");
for(i=0;i<3;i++)
printf ("%d ", color_of_fill[i]); printf ("\n");
getch();
}
int control1 (triangle *sq, RECT r, HWND hwnd)//проверка на вложенность в экран
{
int f=0;
GetClientRect(hwnd, &r);
int x1,y1,x2,y2,x3,y3;
sq->get_coordinate(&x1,&y1,&x2,&y2,&x3,&y3);
if(
(x1>r.left) && (x2>r.left) && (x3>r.left) && (x1<r.right) && (x2<r.right) && (x3<r.right)
&& (y1>r.top) && (y2>r.top)&& (y3>r.top) && (y1<r.bottom) && (y2<r.bottom) && (y3<r.bottom)
)
{
getch();
f=1;
}
else
{
printf("Error: Your triangle miss from screen ");
getch();
}
return f;
}
int control2(triangle sq1, triangle sq2,RECT r) //проверка на вложенность фигур
{
int f=0;
int x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6;
sq1.get_coordinate(&x1,&y1,&x2,&y2,&x3,&x3);
sq2.get_coordinate(&x4,&y4,&x5,&y5,&x6,&y6);
return f;
}
int control3(triangle *sq1)
{
int f=0;
int x1,x2,y1,y2,x3,y3;
sq1->get_coordinate(&x1,&y1,&x2,&y2,&x3,&y3);
return f;
}
void SaveInfo (triangle sq1, triangle sq2)
{
system("cls");
printf("\nSave info\n");
sq1.save();
sq2.save();
}
int LoadInfo(triangle *sq1,triangle *sq2,HWND hwnd )
{
RECT r;
GetClientRect(hwnd, &r);
system("cls");
printf("\nInfo Load\n");
if( !sq1->load() )return 0;
sq1->print_parametrs();
if(control3(sq1)==1) {printf("\n It's not a triangle"); getch(); return 1;}
if( !sq2->load() ) return 0;
sq2->print_parametrs();
if(control3(sq2)==1) {printf("\n It's not a triangle"); getch(); return 1;}
if((control1(sq1,r, hwnd))==1) return 1; else return 0;
}
void Printr(triangle sq1,triangle sq2,HDC hdc,RECT r, HWND hwnd)
{
system("cls");
sq1.draw_triangle(hdc);
sq2.draw_triangle(hdc);
}
int clrcls(HDC hdc)
{
HBRUSH hBrush;
hBrush = CreateSolidBrush(RGB(0,0,0));
HBRUSH hOldBrush = SelectBrush(hdc, hBrush);
Rectangle(hdc, 640, 300, 0, 0);
SelectBrush(hdc, hOldBrush);
DeleteBrush(hBrush);
return 0;
}
void Animation(triangle *sq1,triangle *sq2,HWND hwnd, HDC hdc)
{
RECT r;
GetClientRect(hwnd, &r);
system("cls");
int dx,dy;
printf("\n dx=");
scanf("%d", &dx);
printf("\n dy=");
scanf("%d", &dy);
Printr(*sq1,*sq2,hdc,r,hwnd);
sq1->moving(dx,dy);
sq2->moving(dx,dy);
if ((control1(sq1,r,hwnd))==0)
{sq1->moving(-dx,-dy);
sq2->moving(-dx,-dy);
system("cls");
printf("\nError:triangle miss from screen\nPress any key"); getch();
} else { clrcls(hdc); Printr(*sq1,*sq2,hdc,r,hwnd); getch();}
}
int _tmain()
{
char buf[100];
USES_CONVERSION;
HWND hwnd = FindWindow(A2W("ConsoleWindowClass"), NULL);
HDC hdc = GetDC(hwnd);
RECT r;
int i =0;
GetClientRect(hwnd, &r);
SetBkColor (hdc, RGB(0, 0, 0));
SetTextColor (hdc, RGB(255, 0, 0));
triangle sq1,sq2;
int exit = 0, vvod = 0;
while(exit == 0)
{
printf("\n ENTER YOUR CHOICE! ");
printf("\n1. Enter unformation about rectangle!");
printf("\n2. Load information");
printf("\n3. Saving information");
printf("\n4. Paint empty rectange 1");
printf("\n5. Paint full rectangle! ");
printf("\n6. Paint 2 rectangles!");
printf("\n7. Moving of the rectangles!");
printf("\n8. Exit");
if(!vvod)printf("\nData NO in\n");
switch(getche())
{
case '1':{system("cls");getch();}break;
case '2':if(LoadInfo(&sq1, &sq2,hwnd))vvod = 1; break; // ввод фигуры с файла
case '3':if(vvod)SaveInfo(sq1,sq2); break; // соранение фигуры
case '4':if(vvod){ // не закрашенная фигура
system("cls");
sq1.draw_triangle(hdc);
sprintf_s(buf, "triangle!!!");
TextOut(hdc, 180, 20,A2W(buf),strlen(buf));
getch();
clrcls(hdc);
} break;
case '5':if(vvod){ // закрашенная фигура
system("cls");
sq2.draw_triangle(hdc);
sprintf_s(buf, "Full triangle!");
TextOut(hdc, 180, 20,A2W(buf),strlen(buf));
getch();
clrcls(hdc);
} break;
case '6':if(vvod){ // вложенные фигуры
system("cls");
if(control2(sq1,sq2,r)==0)
{ Printr(sq1,sq2,hdc,r,hwnd);
}
else printf("\nFigury ne vlogeny!");
sprintf_s(buf, "2 triangle!");
TextOut(hdc, 180, 20,A2W(buf),strlen(buf));
getch();
clrcls(hdc);
} break;
case '7':{if(vvod) Animation(&sq1, &sq2,hwnd, hdc); clrcls(hdc);} break; // перемещение фигуры
case '8':exit = 1; break;
}
system("cls");
}
}