質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/20 12:57

投稿

TN8001
TN8001

スコア10180

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