-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.cs
170 lines (153 loc) · 5.78 KB
/
01.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PrinterApp
{
public class Form1 : Form
{
private PrintDocument printDocument;
private PrintPreviewDialog printPreviewDialog;
private Button printPreviewButton;
private Button clearDrawingButton;
private Button moveLeftButton;
private Button moveRightButton;
private Button moveUpButton;
private Button moveDownButton;
private Panel drawingPanel;
private bool isDrawing = false;
private Pen drawingPen = new Pen(Color.Black, 2);
private List<Point> drawingPoints = new List<Point>();
private Bitmap drawingBitmap;
private float imageX = 100, imageY = 100;
public Form1()
{
InitializeComponents();
AttachEventHandlers();
}
private void InitializeComponents()
{
printDocument = new PrintDocument();
printPreviewDialog = new PrintPreviewDialog();
printPreviewButton = new Button { Text = "Print Preview" };
clearDrawingButton = new Button { Text = "Clear Drawing" };
moveLeftButton = new Button { Text = "←" };
moveRightButton = new Button { Text = "→" };
moveUpButton = new Button { Text = "↑" };
moveDownButton = new Button { Text = "↓" };
drawingPanel = new Panel { Dock = DockStyle.Fill };
Size = new Size(800, 600);
Controls.Add(drawingPanel);
Controls.AddRange(new Control[] { printPreviewButton, clearDrawingButton, moveLeftButton, moveRightButton, moveUpButton, moveDownButton });
foreach (var button in new[] { printPreviewButton, clearDrawingButton, moveLeftButton, moveRightButton, moveUpButton, moveDownButton })
{
button.Dock = DockStyle.Top;
}
}
private void AttachEventHandlers()
{
printPreviewButton.Click += async (sender, e) => await OpenPrintPreviewAsync();
clearDrawingButton.Click += (sender, e) => ClearDrawing();
moveLeftButton.Click += (sender, e) => AdjustImagePosition(-10, 0);
moveRightButton.Click += (sender, e) => AdjustImagePosition(10, 0);
moveUpButton.Click += (sender, e) => AdjustImagePosition(0, -10);
moveDownButton.Click += (sender, e) => AdjustImagePosition(0, 10);
drawingPanel.Paint += DrawingPanel_Paint;
drawingPanel.MouseDown += DrawingPanel_MouseDown;
drawingPanel.MouseMove += DrawingPanel_MouseMove;
drawingPanel.MouseUp += DrawingPanel_MouseUp;
printDocument.PrintPage += PrintDocument_PrintPage;
}
private async Task OpenPrintPreviewAsync()
{
await Task.Run(() =>
{
Invoke(new Action(() =>
{
printPreviewDialog.Document = printDocument;
printPreviewDialog.ShowDialog();
}));
}).ConfigureAwait(true);
}
private void ClearDrawing()
{
if (drawingBitmap != null) drawingBitmap.Dispose();
drawingBitmap = new Bitmap(drawingPanel.Width, drawingPanel.Height);
using (var g = Graphics.FromImage(drawingBitmap))
{
g.Clear(Color.White);
if (drawingPoints.Count > 1)
{
g.DrawLines(drawingPen, drawingPoints.ToArray());
}
}
drawingPoints.Clear();
OpenPrintPreviewAsync();
}
private void AdjustImagePosition(float dx, float dy)
{
imageX += dx;
imageY += dy;
drawingPanel.Invalidate();
}
private void DrawingPanel_Paint(object sender, PaintEventArgs e)
{
if (drawingBitmap != null)
{
e.Graphics.DrawImage(drawingBitmap, 0, 0);
}
if (isDrawing && drawingPoints.Count > 1)
{
e.Graphics.DrawLines(drawingPen, drawingPoints.ToArray());
}
}
private void DrawingPanel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrawing = true;
drawingPoints.Add(e.Location);
}
}
private void DrawingPanel_MouseMove(object sender, MouseEventArgs e)
{
if (isDrawing)
{
drawingPoints.Add(e.Location);
drawingPanel.Invalidate();
}
}
private void DrawingPanel_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
SaveCurrentDrawing();
}
private void SaveCurrentDrawing()
{
using (var g = Graphics.FromImage(drawingBitmap))
{
if (drawingPoints.Count > 1)
{
g.DrawLines(drawingPen, drawingPoints.ToArray());
}
}
drawingPoints.Clear();
}
private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
if (drawingBitmap != null)
{
e.Graphics.DrawImage(drawingBitmap, imageX, imageY, drawingBitmap.Width, drawingBitmap.Height);
}
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}