回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,221 +1,220 @@
|
|
1
|
-
いじっていたら跡形がなくなってしまったのですが、何かの参考になれば。
|
2
|
-
> 始点をMouseDownイベント、終点の処理をMouseUpイベント
|
3
|
-
|
4
|
-
線の引き方にこだわりはなさそうなので「ペイント」のようなドラッグ方式です。
|
5
|
-
|
6
|
-
Form1.cs
|
7
|
-
|
8
|
-
using System;
|
9
|
-
using System.Drawing;
|
10
|
-
using System.
|
11
|
-
using System.
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
private readonly ToolStripButton[]
|
19
|
-
|
20
|
-
|
21
|
-
private Point
|
22
|
-
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
|
27
|
-
private bool
|
28
|
-
private bool
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
modeButtons
|
38
|
-
|
39
|
-
|
40
|
-
toolStripComboBox1.
|
41
|
-
|
42
|
-
|
43
|
-
colorButtons
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
if(!
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
e.Graphics.
|
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
|
-
using(var
|
88
|
-
|
89
|
-
|
90
|
-
g.
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
Form1.Designer.cs
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
this.
|
132
|
-
this.
|
133
|
-
this.
|
134
|
-
this.
|
135
|
-
this.
|
136
|
-
this.
|
137
|
-
this.
|
138
|
-
this.
|
139
|
-
this.
|
140
|
-
|
141
|
-
|
142
|
-
this.
|
143
|
-
this.
|
144
|
-
this.
|
145
|
-
|
146
|
-
|
147
|
-
this.pictureBox1.
|
148
|
-
this.pictureBox1.
|
149
|
-
this.pictureBox1.
|
150
|
-
this.pictureBox1.
|
151
|
-
this.pictureBox1.
|
152
|
-
this.pictureBox1.
|
153
|
-
this.pictureBox1.
|
154
|
-
|
155
|
-
|
156
|
-
this.panel1.
|
157
|
-
this.panel1.
|
158
|
-
|
159
|
-
|
160
|
-
this.
|
161
|
-
this.
|
162
|
-
this.
|
163
|
-
this.
|
164
|
-
this.
|
165
|
-
|
166
|
-
|
167
|
-
this.toolStripButton1.
|
168
|
-
this.toolStripButton1.
|
169
|
-
|
170
|
-
|
171
|
-
this.toolStripButton2.
|
172
|
-
this.toolStripButton2.
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
this.toolStripButton3.
|
178
|
-
this.toolStripButton3.
|
179
|
-
this.toolStripButton3.
|
180
|
-
this.toolStripButton3.
|
181
|
-
|
182
|
-
|
183
|
-
this.toolStripButton4.
|
184
|
-
this.toolStripButton4.
|
185
|
-
this.toolStripButton4.
|
186
|
-
|
187
|
-
|
188
|
-
this.
|
189
|
-
|
190
|
-
|
191
|
-
this.
|
192
|
-
this.Controls.Add(this.
|
193
|
-
this.Controls.Add(this.
|
194
|
-
|
195
|
-
|
196
|
-
this.panel1.
|
197
|
-
this.
|
198
|
-
this.toolStrip1.
|
199
|
-
this.
|
200
|
-
this.statusStrip1.
|
201
|
-
this.
|
202
|
-
this.
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
private System.Windows.Forms.
|
209
|
-
private System.Windows.Forms.
|
210
|
-
private System.Windows.Forms.
|
211
|
-
private System.Windows.Forms.ToolStripButton
|
212
|
-
private System.Windows.Forms.
|
213
|
-
private System.Windows.Forms.
|
214
|
-
private System.Windows.Forms.ToolStripButton
|
215
|
-
private System.Windows.Forms.
|
216
|
-
private System.Windows.Forms.
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
```
|
1
|
+
いじっていたら跡形がなくなってしまったのですが、何かの参考になれば。
|
2
|
+
> 始点をMouseDownイベント、終点の処理をMouseUpイベント
|
3
|
+
|
4
|
+
線の引き方にこだわりはなさそうなので「ペイント」のようなドラッグ方式です。
|
5
|
+
|
6
|
+
```cs:Form1.cs
|
7
|
+
using System;
|
8
|
+
using System.Drawing;
|
9
|
+
using System.Drawing.Drawing2D;
|
10
|
+
using System.Linq;
|
11
|
+
using System.Windows.Forms;
|
12
|
+
|
13
|
+
namespace Questions240161
|
14
|
+
{
|
15
|
+
public partial class Form1 : Form
|
16
|
+
{
|
17
|
+
private readonly ToolStripButton[] modeButtons;
|
18
|
+
private readonly ToolStripButton[] colorButtons;
|
19
|
+
|
20
|
+
private Point startPoint;
|
21
|
+
private Point endPoint;
|
22
|
+
|
23
|
+
private Color penColor = Color.Black;
|
24
|
+
private int penSize => (int)toolStripComboBox1.SelectedItem;
|
25
|
+
|
26
|
+
private bool isPencilMode => toolStripButton1.Checked;
|
27
|
+
private bool isLineMode => toolStripButton2.Checked;
|
28
|
+
private bool isLineDragging;
|
29
|
+
|
30
|
+
public Form1()
|
31
|
+
{
|
32
|
+
InitializeComponent();
|
33
|
+
|
34
|
+
pictureBox1.Image = new Bitmap(800, 600);
|
35
|
+
|
36
|
+
modeButtons = new[] { toolStripButton1, toolStripButton2, };
|
37
|
+
modeButtons[0].Checked = true;
|
38
|
+
|
39
|
+
toolStripComboBox1.ComboBox.DataSource = Enumerable.Range(1, 10).ToList();
|
40
|
+
toolStripComboBox1.SelectedIndex = 2;
|
41
|
+
|
42
|
+
colorButtons = new[] { toolStripButton3, toolStripButton4, };
|
43
|
+
colorButtons[0].Checked = true;
|
44
|
+
}
|
45
|
+
|
46
|
+
private void PictureBox1_Paint(object sender, PaintEventArgs e)
|
47
|
+
{
|
48
|
+
if(!pictureBox1.Capture) return;
|
49
|
+
if(!isLineDragging) return;
|
50
|
+
|
51
|
+
using(var pen = new Pen(penColor, penSize))
|
52
|
+
{
|
53
|
+
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
|
54
|
+
e.Graphics.DrawLine(pen, startPoint, endPoint);
|
55
|
+
}
|
56
|
+
}
|
57
|
+
private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
|
58
|
+
{
|
59
|
+
startPoint = e.Location;
|
60
|
+
if(isLineMode) isLineDragging = true;
|
61
|
+
}
|
62
|
+
private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
|
63
|
+
{
|
64
|
+
SetMessage($"Position : {e.X}, {e.Y}");
|
65
|
+
if(!pictureBox1.Capture) return;
|
66
|
+
|
67
|
+
endPoint = e.Location;
|
68
|
+
if(isPencilMode)
|
69
|
+
{
|
70
|
+
DrawLine(LineCap.Round);
|
71
|
+
startPoint = endPoint;
|
72
|
+
}
|
73
|
+
|
74
|
+
if(isLineMode) pictureBox1.Invalidate();
|
75
|
+
}
|
76
|
+
private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
|
77
|
+
{
|
78
|
+
if(!isLineDragging) return;
|
79
|
+
|
80
|
+
isLineDragging = false;
|
81
|
+
if(isLineMode) DrawLine();
|
82
|
+
}
|
83
|
+
|
84
|
+
private void DrawLine(LineCap lineCap = LineCap.Flat)
|
85
|
+
{
|
86
|
+
using(var g = Graphics.FromImage(pictureBox1.Image))
|
87
|
+
using(var pen = new Pen(penColor, penSize) { StartCap = lineCap, EndCap = lineCap })
|
88
|
+
{
|
89
|
+
g.SmoothingMode = SmoothingMode.AntiAlias;
|
90
|
+
g.DrawLine(pen, startPoint, endPoint);
|
91
|
+
pictureBox1.Image = pictureBox1.Image; // 再割り当てしないと反映されない??
|
92
|
+
}
|
93
|
+
}
|
94
|
+
|
95
|
+
private void ToolStripModeButton_Click(object sender, EventArgs e)
|
96
|
+
{
|
97
|
+
foreach(var b in modeButtons) b.Checked = sender == b;
|
98
|
+
}
|
99
|
+
private void ToolStripColorButton_Click(object sender, EventArgs e)
|
100
|
+
{
|
101
|
+
foreach(var b in colorButtons) b.Checked = sender == b;
|
102
|
+
|
103
|
+
penColor = ((ToolStripButton)sender).ForeColor;
|
104
|
+
}
|
105
|
+
|
106
|
+
private void SetMessage(string value) => toolStripStatusLabel1.Text = value;
|
107
|
+
}
|
108
|
+
}
|
109
|
+
```
|
110
|
+
|
111
|
+
```cs:Form1.Designer.cs
|
112
|
+
namespace Questions240161
|
113
|
+
{
|
114
|
+
partial class Form1
|
115
|
+
{
|
116
|
+
private System.ComponentModel.IContainer components = null;
|
117
|
+
|
118
|
+
protected override void Dispose(bool disposing)
|
119
|
+
{
|
120
|
+
if(disposing && (components != null))
|
121
|
+
{
|
122
|
+
components.Dispose();
|
123
|
+
}
|
124
|
+
base.Dispose(disposing);
|
125
|
+
}
|
126
|
+
|
127
|
+
#region Windows フォーム デザイナーで生成されたコード
|
128
|
+
private void InitializeComponent()
|
129
|
+
{
|
130
|
+
this.pictureBox1 = new System.Windows.Forms.PictureBox();
|
131
|
+
this.panel1 = new System.Windows.Forms.Panel();
|
132
|
+
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
|
133
|
+
this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
|
134
|
+
this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
|
135
|
+
this.toolStripComboBox1 = new System.Windows.Forms.ToolStripComboBox();
|
136
|
+
this.toolStripButton3 = new System.Windows.Forms.ToolStripButton();
|
137
|
+
this.toolStripButton4 = new System.Windows.Forms.ToolStripButton();
|
138
|
+
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
|
139
|
+
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
|
140
|
+
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
|
141
|
+
this.panel1.SuspendLayout();
|
142
|
+
this.toolStrip1.SuspendLayout();
|
143
|
+
this.statusStrip1.SuspendLayout();
|
144
|
+
this.SuspendLayout();
|
145
|
+
|
146
|
+
this.pictureBox1.BackColor = System.Drawing.Color.White;
|
147
|
+
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
|
148
|
+
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
|
149
|
+
this.pictureBox1.TabStop = false;
|
150
|
+
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.PictureBox1_Paint);
|
151
|
+
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseDown);
|
152
|
+
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseMove);
|
153
|
+
this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseUp);
|
154
|
+
|
155
|
+
this.panel1.AutoScroll = true;
|
156
|
+
this.panel1.Controls.Add(this.pictureBox1);
|
157
|
+
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
|
158
|
+
|
159
|
+
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
|
160
|
+
this.toolStripButton1,
|
161
|
+
this.toolStripButton2,
|
162
|
+
this.toolStripComboBox1,
|
163
|
+
this.toolStripButton3,
|
164
|
+
this.toolStripButton4});
|
165
|
+
|
166
|
+
this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
|
167
|
+
this.toolStripButton1.Text = "Pen";
|
168
|
+
this.toolStripButton1.Click += new System.EventHandler(this.ToolStripModeButton_Click);
|
169
|
+
|
170
|
+
this.toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
|
171
|
+
this.toolStripButton2.Text = "Line";
|
172
|
+
this.toolStripButton2.Click += new System.EventHandler(this.ToolStripModeButton_Click);
|
173
|
+
|
174
|
+
this.toolStripComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
|
175
|
+
|
176
|
+
this.toolStripButton3.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
|
177
|
+
this.toolStripButton3.Font = new System.Drawing.Font("メイリオ", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
|
178
|
+
this.toolStripButton3.ForeColor = System.Drawing.Color.Black;
|
179
|
+
this.toolStripButton3.Text = "■";
|
180
|
+
this.toolStripButton3.Click += new System.EventHandler(this.ToolStripColorButton_Click);
|
181
|
+
|
182
|
+
this.toolStripButton4.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
|
183
|
+
this.toolStripButton4.ForeColor = System.Drawing.Color.Red;
|
184
|
+
this.toolStripButton4.Text = "■";
|
185
|
+
this.toolStripButton4.Click += new System.EventHandler(this.ToolStripColorButton_Click);
|
186
|
+
|
187
|
+
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
|
188
|
+
this.toolStripStatusLabel1});
|
189
|
+
|
190
|
+
this.ClientSize = new System.Drawing.Size(800, 450);
|
191
|
+
this.Controls.Add(this.panel1);
|
192
|
+
this.Controls.Add(this.statusStrip1);
|
193
|
+
this.Controls.Add(this.toolStrip1);
|
194
|
+
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
|
195
|
+
this.panel1.ResumeLayout(false);
|
196
|
+
this.panel1.PerformLayout();
|
197
|
+
this.toolStrip1.ResumeLayout(false);
|
198
|
+
this.toolStrip1.PerformLayout();
|
199
|
+
this.statusStrip1.ResumeLayout(false);
|
200
|
+
this.statusStrip1.PerformLayout();
|
201
|
+
this.ResumeLayout(false);
|
202
|
+
this.PerformLayout();
|
203
|
+
|
204
|
+
}
|
205
|
+
#endregion
|
206
|
+
|
207
|
+
private System.Windows.Forms.PictureBox pictureBox1;
|
208
|
+
private System.Windows.Forms.Panel panel1;
|
209
|
+
private System.Windows.Forms.ToolStrip toolStrip1;
|
210
|
+
private System.Windows.Forms.ToolStripButton toolStripButton1;
|
211
|
+
private System.Windows.Forms.ToolStripButton toolStripButton2;
|
212
|
+
private System.Windows.Forms.ToolStripComboBox toolStripComboBox1;
|
213
|
+
private System.Windows.Forms.ToolStripButton toolStripButton3;
|
214
|
+
private System.Windows.Forms.ToolStripButton toolStripButton4;
|
215
|
+
private System.Windows.Forms.StatusStrip statusStrip1;
|
216
|
+
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
|
217
|
+
}
|
218
|
+
}
|
219
|
+
```
|
221
220
|
Designer.csを限界まで削ったので何か不具合があるかもしれません^^;
|