-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormMain.cs
583 lines (465 loc) · 19.5 KB
/
FormMain.cs
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Drawing.Drawing2D;
namespace KPFU_2_sem_programming_PaintPlusPlus {
public partial class FormMain : Form {
public const string title = "ПеинтПлюсПлюс";
private const string fileUnnamed = "Безымянный";
private string fileName = fileUnnamed;
private bool fileIsSaved = true;
private bool fileIsNew = true;
private OpenFileDialog ofd = new OpenFileDialog();
private SaveFileDialog sfd = new SaveFileDialog();
public const string pathIconBlue = "Resources/csharp_blue.ico";
public const string pathIconRed = "Resources/csharp_red.ico";
private bool mouseDown = false;
private string printingText = string.Empty;
private byte printingTextSize = 0;
private Point paintingFigureFirstPoint;
private Point paintingFigureSecondPoint;
private Points points = new Points(2);
private int windowSizeX = Screen.PrimaryScreen.Bounds.Width;
private int windowSizeY = Screen.PrimaryScreen.Bounds.Height;
private Bitmap mapDrawing;
private Bitmap mapSaved;
private Graphics graphics;
private Image clipboard;
private Pen pen;
public FormMain() {
InitializeComponent();
this.DoubleBuffered = true;
ofd.Filter = "Bitmap file|*.bmp|PNG file|.png|JPG file|.jpg|TIFF file|.tiff|GIF file|.gif|Any format|*.*";
sfd.Filter = "Bitmap file|*.bmp|PNG file|.png|JPG file|.jpg|TIFF file|.tiff|GIF file|.gif|Any format|*.*";
pen = createTool(Color.Black);
updateScreen();
updateStatusBar();
changeIcon();
changeTitle();
}
private void updateScreen() {
mapDrawing = new Bitmap(windowSizeX, windowSizeY);
mapSaved = new Bitmap(windowSizeX, windowSizeY);
graphics = Graphics.FromImage(mapDrawing);
graphics.Clear(Color.White);
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
formMainPictureBox.Image = mapDrawing;
formMainPictureBox.Invalidate();
}
private void changeIcon() {
if (fileIsSaved == true) {
this.Icon = new Icon(pathIconBlue);
} else {
this.Icon = new Icon(pathIconRed);
}
}
private void changeTitle() {
string new_title = "";
if (fileIsSaved == false) {
new_title += "* ";
}
if (fileIsNew == true) {
new_title += fileUnnamed;
} else {
new_title += fileName;
}
new_title += " - " + title;
this.Text = new_title;
}
public void updateStatusBar() {
formMainStatusBarSize.Text = string.Format("Размер окна: {0} x {1}", this.Width, this.Height);
formMainStatusBarScale.Text = "Масштаб: 100%";
formMainStatusBar.Update();
}
private Pen createTool(Color color) {
Pen p = new Pen(color, formMainTrackBar.Value);
p.Brush = Brushes.Black;
p.StartCap = LineCap.Round;
p.EndCap = LineCap.Round;
p.Alignment = PenAlignment.Center;
p.DashStyle = DashStyle.Custom;
p.LineJoin = LineJoin.Round;
return p;
}
private void formMainPictureBox_MouseDown(object sender, MouseEventArgs e) {
if (e.Button != MouseButtons.Left) {
return;
}
paintingFigureFirstPoint = e.Location;
mouseDown = true;
if (formMainMenuStripToolsText.Checked == true) {
graphics.DrawString(printingText, new Font("Arial", printingTextSize), pen.Brush, e.Location.X, e.Location.Y);
printingText = string.Empty;
printingTextSize = 0;
formMainMenuStripToolsText.Checked = false;
formMainPictureBox.Invalidate();
}
}
private void formMainPictureBox_MouseMove(object sender, MouseEventArgs e) {
if (e.Button != MouseButtons.Left) {
return;
}
paintingFigureSecondPoint = e.Location;
if (mouseDown == false) {
return;
}
if (formMainMenuStripToolsRect.Checked == true || formMainMenuStripToolsEllipse.Checked == true) {
graphics.Clear(Color.White);
graphics.DrawImage(mapSaved, Point.Empty);
Rectangle rect = RectFrom2Points(paintingFigureFirstPoint, paintingFigureSecondPoint);
if (formMainMenuStripToolsRect.Checked == true) {
graphics.DrawRectangle(pen, rect);
if (formMainMenuStripToolsFill.Checked == true) {
graphics.FillRectangle(pen.Brush, rect);
}
} else {
graphics.DrawEllipse(pen, rect);
if (formMainMenuStripToolsFill.Checked == true) {
graphics.FillEllipse(pen.Brush, rect);
}
}
formMainPictureBox.Invalidate();
} else if (formMainMenuStripToolsLine.Checked == true) {
graphics.Clear(Color.White);
graphics.DrawImage(mapSaved, Point.Empty);
graphics.DrawLine(pen, paintingFigureFirstPoint, paintingFigureSecondPoint);
formMainPictureBox.Invalidate();
} else if (formMainMenuStripToolsRubber.Checked == true) {
Rectangle rect = RectFromPoint(paintingFigureSecondPoint, 30);
graphics.FillEllipse(Brushes.White, rect);
formMainPictureBox.Invalidate();
} else {
points.setPoint(e.X, e.Y);
if (points.getCountPoints() == 2) {
graphics.DrawLines(pen, points.getPoints());
formMainPictureBox.Image = mapDrawing;
points.setPoint(e.X, e.Y);
}
formMainPictureBox.Invalidate();
}
fileIsSaved = false;
changeIcon();
changeTitle();
}
private void formMainPictureBox_MouseUp(object sender, MouseEventArgs e) {
if (e.Button != MouseButtons.Left) {
return;
}
mapSaved = new Bitmap(mapDrawing);
mouseDown = false;
fileIsSaved = false;
points.resetPoints();
this.formMainPictureBox.Cursor = Cursors.Default;
changeIcon();
changeTitle();
}
private void formMainPictureBox_Paint(object sender, PaintEventArgs e) {
e.Graphics.DrawImage(mapSaved, Point.Empty);
e.Graphics.DrawImage(mapDrawing, Point.Empty);
}
private void FormMain_Resize(object sender, EventArgs e) {
formMainPictureBox.Width = this.Width - 30;
formMainPictureBox.Height = this.Height - 120;
updateStatusBar();
}
private void formMainGroupColorSetColor(object sender, EventArgs e) {
pen.Color = ((Button) sender).BackColor;
}
private void newFile() {
fileName = fileUnnamed;
fileIsNew = true;
fileIsSaved = true;
updateScreen();
updateStatusBar();
changeIcon();
changeTitle();
}
private void formMainFileCreate_Click(object sender, EventArgs e) {
if (fileIsSaved == false) {
if (showFormSave() == false) {
return;
}
}
newFile();
}
private void formMainFileOpen_Click(object sender, EventArgs e) {
if (fileIsSaved == false) {
if (showFormSave() == true) {
if (sfd.ShowDialog() == DialogResult.OK) {
saveFile();
fileIsNew = false;
fileIsSaved = true;
} else {
fileIsNew = false;
fileIsSaved = false;
}
}
}
newFile();
if (ofd.ShowDialog() == DialogResult.OK) {
fileName = ofd.FileName;
sfd.FileName = ofd.FileName;
clipboard = new Bitmap(ofd.FileName);
CtrlV();
clipboard = null;
fileIsNew = false;
fileIsSaved = true;
} else {
return;
}
changeIcon();
changeTitle();
}
private void formMainFileSave_Click(object sender, EventArgs e) {
if (fileIsNew == true) {
if (sfd.ShowDialog() == DialogResult.OK) {
saveFile();
fileIsNew = false;
} else {
fileIsSaved = false;
}
} else {
saveFile();
}
}
private void formMainFileSaveAs_Click(object sender, EventArgs e) {
if (sfd.ShowDialog() == DialogResult.OK) {
saveFile();
}
}
private void formMainFilePrint_Click(object sender, EventArgs e) {
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += PrintPageHandler;
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
if (printDialog.ShowDialog() == DialogResult.OK) {
printDialog.Document.Print();
}
}
private void PrintPageHandler(object sender, PrintPageEventArgs e) {
e.Graphics.DrawImageUnscaled(formMainPictureBox.Image, 0, 0);
}
private void formMainFileExit_Click(object sender, EventArgs e) {
if (fileIsSaved == false) {
if (showFormSave() == false) {
return;
}
}
this.Close();
}
private bool showFormSave() {
FormSave formSave = new FormSave(fileName);
DialogResult dialogResult = formSave.ShowDialog();
return handlerFormSave(dialogResult);
}
private bool handlerFormSave(DialogResult dialogResult) {
if (dialogResult == DialogResult.Yes) {
if (fileIsNew == true) {
if (sfd.ShowDialog() == DialogResult.OK) {
saveFile();
return true;
} else {
return false;
}
} else {
saveFile();
return true;
}
} else if (dialogResult == DialogResult.No) {
this.Close();
return false;
} else if (dialogResult == DialogResult.Cancel) {
return false;
} else {
return false;
}
}
private void saveFile() {
ImageFormat imageFormat;
if (sfd.FileName.EndsWith(".bmp")) {
imageFormat = ImageFormat.Bmp;
} else if (sfd.FileName.EndsWith(".jpg")) {
imageFormat = ImageFormat.Jpeg;
} else if (sfd.FileName.EndsWith(".png")) {
imageFormat = ImageFormat.Png;
} else if (sfd.FileName.EndsWith(".tiff")) {
imageFormat = ImageFormat.Tiff;
} else if (sfd.FileName.EndsWith(".gif")) {
imageFormat = ImageFormat.Gif;
} else {
imageFormat = ImageFormat.Bmp;
}
formMainPictureBox.Image.Save(sfd.FileName, imageFormat);
fileName = sfd.FileName;
fileIsSaved = true;
changeIcon();
changeTitle();
}
private void formMainTrackBar_Scroll(object sender, EventArgs e) {
pen.Width = formMainTrackBar.Value;
}
private void formMainChooseColor_Click(object sender, EventArgs e) {
if (formMainColorDialog.ShowDialog() == DialogResult.OK) {
pen.Color = formMainColorDialog.Color;
((Button) sender).BackColor = formMainColorDialog.Color;
}
}
private void formMainMenuStripReferenceAbout_Click(object sender, EventArgs e) {
new FormAbout().Show();
}
private void formMainMenuStripViewStatusBar_Click(object sender, EventArgs e) {
if (formMainMenuStripViewStatusBar.CheckState == CheckState.Checked) {
formMainMenuStripViewStatusBar.CheckState = CheckState.Unchecked;
formMainStatusBar.Visible = false;
formMainPictureBox.Size = new Size(formMainMenuStripViewStatusBar.Size.Width, formMainPictureBox.Size.Height + formMainMenuStripViewStatusBar.Size.Height);
} else {
formMainMenuStripViewStatusBar.CheckState = CheckState.Checked;
formMainStatusBar.Visible = true;
formMainPictureBox.Size = new Size(formMainMenuStripViewStatusBar.Size.Width, formMainPictureBox.Size.Height - formMainMenuStripViewStatusBar.Size.Height);
}
}
private void formMainMenuStripViewScaleUp_Click(object sender, EventArgs e) {
changeZoom("up");
}
private void formMainMenuStripViewScaleDown_Click(object sender, EventArgs e) {
changeZoom("down");
}
private void formMainMenuStripViewScaleRecover_Click(object sender, EventArgs e) {
changeZoom("recover");
}
private void changeZoom(string value) {
updateStatusBar();
}
private Rectangle RectFromPoint(Point p, int radius) {
Rectangle rect = new Rectangle(new Point(p.X - radius, p.Y - radius), new Size(radius * 2, radius * 2));
return rect;
}
private Rectangle RectFrom2Points(Point start, Point end) {
Rectangle rect = new Rectangle(
Math.Min(start.X, end.X),
Math.Min(start.Y, end.Y),
Math.Abs(end.X - start.X),
Math.Abs(end.Y - start.Y)
);
return rect;
}
private void formMainMenuStripToolsRect_Click(object sender, EventArgs e) {
if (formMainMenuStripToolsRect.Checked == true) {
formMainMenuStripToolsRect.Checked = false;
} else {
formMainMenuStripToolsSetOFF();
formMainMenuStripToolsRect.Checked = true;
}
}
private void formMainMenuStripToolsEllipse_Click(object sender, EventArgs e) {
if (formMainMenuStripToolsEllipse.Checked == true) {
formMainMenuStripToolsEllipse.Checked = false;
} else {
formMainMenuStripToolsSetOFF();
formMainMenuStripToolsEllipse.Checked = true;
}
}
private void formMainMenuStripToolsLine_Click(object sender, EventArgs e) {
if (formMainMenuStripToolsLine.Checked == true) {
formMainMenuStripToolsLine.Checked = false;
} else {
formMainMenuStripToolsSetOFF();
formMainMenuStripToolsLine.Checked = true;
}
}
private void formMainMenuStripToolsFill_Click(object sender, EventArgs e) {
if (formMainMenuStripToolsFill.Checked == true) {
formMainMenuStripToolsFill.Checked = false;
} else {
formMainMenuStripToolsFill.Checked = true;
}
}
private void formMainMenuStripToolsRubber_Click(object sender, EventArgs e) {
if (formMainMenuStripToolsRubber.Checked == true) {
formMainMenuStripToolsRubber.Checked = false;
} else {
formMainMenuStripToolsSetOFF();
formMainMenuStripToolsRubber.Checked = true;
}
}
private void formMainMenuStripToolsText_Click(object sender, EventArgs e) {
if (formMainMenuStripToolsText.Checked == true) {
formMainMenuStripToolsText.Checked = false;
} else {
formMainMenuStripToolsSetOFF();
formMainMenuStripToolsText.Checked = true;
InputText inputText = new InputText();
if (inputText.ShowDialog() == DialogResult.OK) {
printingText = inputText.formInputTextTextBox.Text;
printingTextSize = Convert.ToByte(inputText.formInputTextTextSize.Text);
this.formMainPictureBox.Cursor = Cursors.Cross;
} else {
formMainMenuStripToolsText.Checked = false;
}
}
}
private void formMainMenuStripToolsSetOFF() {
foreach (ToolStripMenuItem item in formMainMenuStripTools.Items) {
item.Checked = false;
}
}
private void formMainMenuStripCorrectionCut_Click(object sender, EventArgs e) {
CtrlX();
}
private void formMainMenuStripCorrectionCopy_Click(object sender, EventArgs e) {
CtrlC();
}
private void formMainMenuStripCorrectionPaste_Click(object sender, EventArgs e) {
CtrlV();
}
private void formMainMenuStripCorrectionDelete_Click(object sender, EventArgs e) {
CtrlDelete();
}
private void formMainContextMenuCut_Click(object sender, EventArgs e) {
CtrlX();
}
private void formMainContextMenuCopy_Click(object sender, EventArgs e) {
CtrlC();
}
private void formMainContextMenuPaste_Click(object sender, EventArgs e) {
CtrlV();
}
private void formMainContextMenuDelete_Click(object sender, EventArgs e) {
CtrlDelete();
}
private void CtrlX() {
if (formMainPictureBox.Image != null) {
clipboard = new Bitmap(formMainPictureBox.Image);
mapDrawing = new Bitmap(formMainPictureBox.Width, formMainPictureBox.Height);
mapSaved = new Bitmap(mapDrawing);
graphics = Graphics.FromImage(mapDrawing);
formMainPictureBox.Image = mapDrawing;
formMainPictureBox.Invalidate();
}
}
private void CtrlC() {
if (formMainPictureBox.Image != null) {
clipboard = new Bitmap(formMainPictureBox.Image);
}
}
private void CtrlV() {
if (clipboard != null) {
mapDrawing = new Bitmap(clipboard);
mapSaved = new Bitmap(mapDrawing);
graphics = Graphics.FromImage(mapDrawing);
formMainPictureBox.Image = mapDrawing;
formMainPictureBox.Invalidate();
}
}
private void CtrlDelete() {
mapDrawing = new Bitmap(formMainPictureBox.Width, formMainPictureBox.Height);
mapSaved = new Bitmap(mapDrawing);
graphics = Graphics.FromImage(mapDrawing);
formMainPictureBox.Image = mapDrawing;
formMainPictureBox.Invalidate();
}
}
}