回答編集履歴

1

見直しキャンペーン中

2023/07/20 12:57

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -1,441 +1,220 @@
1
1
  いじっていたら跡形がなくなってしまったのですが、何かの参考になれば。
2
-
3
2
  > 始点をMouseDownイベント、終点の処理をMouseUpイベント
4
3
 
5
-
6
-
7
4
  線の引き方にこだわりはなさそうなので「ペイント」のようなドラッグ方式です。
8
5
 
9
-
10
-
11
- Form1.cs
6
+ ```cs:Form1.cs
12
-
13
- ```C#
14
-
15
7
  using System;
16
-
17
8
  using System.Drawing;
18
-
19
9
  using System.Drawing.Drawing2D;
20
-
21
10
  using System.Linq;
22
-
23
11
  using System.Windows.Forms;
24
12
 
25
-
26
-
27
13
  namespace Questions240161
28
-
29
14
  {
30
-
31
15
  public partial class Form1 : Form
32
-
33
16
  {
34
-
35
17
  private readonly ToolStripButton[] modeButtons;
36
-
37
18
  private readonly ToolStripButton[] colorButtons;
38
19
 
39
-
40
-
41
20
  private Point startPoint;
42
-
43
21
  private Point endPoint;
44
22
 
45
-
46
-
47
23
  private Color penColor = Color.Black;
48
-
49
24
  private int penSize => (int)toolStripComboBox1.SelectedItem;
50
25
 
51
-
52
-
53
26
  private bool isPencilMode => toolStripButton1.Checked;
54
-
55
27
  private bool isLineMode => toolStripButton2.Checked;
56
-
57
28
  private bool isLineDragging;
58
29
 
59
-
60
-
61
30
  public Form1()
62
-
63
- {
31
+ {
64
-
65
32
  InitializeComponent();
66
33
 
67
-
68
-
69
34
  pictureBox1.Image = new Bitmap(800, 600);
70
35
 
71
-
72
-
73
36
  modeButtons = new[] { toolStripButton1, toolStripButton2, };
74
-
75
37
  modeButtons[0].Checked = true;
76
38
 
77
-
78
-
79
39
  toolStripComboBox1.ComboBox.DataSource = Enumerable.Range(1, 10).ToList();
80
-
81
40
  toolStripComboBox1.SelectedIndex = 2;
82
41
 
83
-
84
-
85
42
  colorButtons = new[] { toolStripButton3, toolStripButton4, };
86
-
87
43
  colorButtons[0].Checked = true;
88
-
89
- }
44
+ }
90
-
91
-
92
45
 
93
46
  private void PictureBox1_Paint(object sender, PaintEventArgs e)
94
-
95
- {
47
+ {
96
-
97
48
  if(!pictureBox1.Capture) return;
98
-
99
49
  if(!isLineDragging) return;
100
50
 
101
-
102
-
103
51
  using(var pen = new Pen(penColor, penSize))
104
-
105
- {
52
+ {
106
-
107
53
  e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
108
-
109
54
  e.Graphics.DrawLine(pen, startPoint, endPoint);
110
-
111
- }
55
+ }
112
-
113
- }
56
+ }
114
-
115
57
  private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
116
-
117
- {
58
+ {
118
-
119
59
  startPoint = e.Location;
120
-
121
60
  if(isLineMode) isLineDragging = true;
122
-
123
- }
61
+ }
124
-
125
62
  private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
126
-
127
- {
63
+ {
128
-
129
64
  SetMessage($"Position : {e.X}, {e.Y}");
130
-
131
65
  if(!pictureBox1.Capture) return;
132
66
 
133
-
134
-
135
67
  endPoint = e.Location;
136
-
137
68
  if(isPencilMode)
138
-
139
- {
69
+ {
140
-
141
70
  DrawLine(LineCap.Round);
142
-
143
71
  startPoint = endPoint;
144
-
145
- }
72
+ }
146
-
147
-
148
73
 
149
74
  if(isLineMode) pictureBox1.Invalidate();
150
-
151
- }
75
+ }
152
-
153
76
  private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
154
-
155
- {
77
+ {
156
-
157
78
  if(!isLineDragging) return;
158
79
 
159
-
160
-
161
80
  isLineDragging = false;
162
-
163
81
  if(isLineMode) DrawLine();
164
-
165
- }
82
+ }
166
-
167
-
168
83
 
169
84
  private void DrawLine(LineCap lineCap = LineCap.Flat)
170
-
171
- {
85
+ {
172
-
173
86
  using(var g = Graphics.FromImage(pictureBox1.Image))
174
-
175
87
  using(var pen = new Pen(penColor, penSize) { StartCap = lineCap, EndCap = lineCap })
176
-
177
- {
88
+ {
178
-
179
89
  g.SmoothingMode = SmoothingMode.AntiAlias;
180
-
181
90
  g.DrawLine(pen, startPoint, endPoint);
182
-
183
91
  pictureBox1.Image = pictureBox1.Image; // 再割り当てしないと反映されない??
184
-
185
- }
92
+ }
186
-
187
- }
93
+ }
188
-
189
-
190
94
 
191
95
  private void ToolStripModeButton_Click(object sender, EventArgs e)
192
-
193
- {
96
+ {
194
-
195
97
  foreach(var b in modeButtons) b.Checked = sender == b;
196
-
197
- }
98
+ }
198
-
199
99
  private void ToolStripColorButton_Click(object sender, EventArgs e)
200
-
201
- {
100
+ {
202
-
203
101
  foreach(var b in colorButtons) b.Checked = sender == b;
204
102
 
205
-
206
-
207
103
  penColor = ((ToolStripButton)sender).ForeColor;
208
-
209
- }
104
+ }
210
-
211
-
212
105
 
213
106
  private void SetMessage(string value) => toolStripStatusLabel1.Text = value;
214
-
215
107
  }
216
-
217
108
  }
218
-
219
109
  ```
220
110
 
221
- Form1.Designer.cs
111
+ ```cs:Form1.Designer.cs
222
-
223
- ```C#
224
-
225
112
  namespace Questions240161
226
-
227
113
  {
228
-
229
114
  partial class Form1
230
-
231
115
  {
232
-
233
116
  private System.ComponentModel.IContainer components = null;
234
117
 
235
-
236
-
237
118
  protected override void Dispose(bool disposing)
238
-
239
- {
119
+ {
240
-
241
120
  if(disposing && (components != null))
242
-
243
- {
121
+ {
244
-
245
122
  components.Dispose();
246
-
247
- }
123
+ }
248
-
249
124
  base.Dispose(disposing);
250
-
251
- }
125
+ }
252
-
253
-
254
126
 
255
127
  #region Windows フォーム デザイナーで生成されたコード
256
-
257
128
  private void InitializeComponent()
258
-
259
- {
129
+ {
260
-
261
130
  this.pictureBox1 = new System.Windows.Forms.PictureBox();
262
-
263
131
  this.panel1 = new System.Windows.Forms.Panel();
264
-
265
132
  this.toolStrip1 = new System.Windows.Forms.ToolStrip();
266
-
267
133
  this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
268
-
269
134
  this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
270
-
271
135
  this.toolStripComboBox1 = new System.Windows.Forms.ToolStripComboBox();
272
-
273
136
  this.toolStripButton3 = new System.Windows.Forms.ToolStripButton();
274
-
275
137
  this.toolStripButton4 = new System.Windows.Forms.ToolStripButton();
276
-
277
138
  this.statusStrip1 = new System.Windows.Forms.StatusStrip();
278
-
279
139
  this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
280
-
281
140
  ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
282
-
283
141
  this.panel1.SuspendLayout();
284
-
285
142
  this.toolStrip1.SuspendLayout();
286
-
287
143
  this.statusStrip1.SuspendLayout();
288
-
289
144
  this.SuspendLayout();
290
145
 
291
-
292
-
293
146
  this.pictureBox1.BackColor = System.Drawing.Color.White;
294
-
295
147
  this.pictureBox1.Location = new System.Drawing.Point(0, 0);
296
-
297
148
  this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
298
-
299
149
  this.pictureBox1.TabStop = false;
300
-
301
150
  this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.PictureBox1_Paint);
302
-
303
151
  this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseDown);
304
-
305
152
  this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseMove);
306
-
307
153
  this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseUp);
308
154
 
309
-
310
-
311
155
  this.panel1.AutoScroll = true;
312
-
313
156
  this.panel1.Controls.Add(this.pictureBox1);
314
-
315
157
  this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
316
158
 
317
-
318
-
319
159
  this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
320
-
321
160
  this.toolStripButton1,
322
-
323
161
  this.toolStripButton2,
324
-
325
162
  this.toolStripComboBox1,
326
-
327
163
  this.toolStripButton3,
328
-
329
164
  this.toolStripButton4});
330
165
 
331
-
332
-
333
166
  this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
334
-
335
167
  this.toolStripButton1.Text = "Pen";
336
-
337
168
  this.toolStripButton1.Click += new System.EventHandler(this.ToolStripModeButton_Click);
338
169
 
339
-
340
-
341
170
  this.toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
342
-
343
171
  this.toolStripButton2.Text = "Line";
344
-
345
172
  this.toolStripButton2.Click += new System.EventHandler(this.ToolStripModeButton_Click);
346
173
 
347
-
348
-
349
174
  this.toolStripComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
350
175
 
351
-
352
-
353
176
  this.toolStripButton3.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
354
-
355
177
  this.toolStripButton3.Font = new System.Drawing.Font("メイリオ", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
356
-
357
178
  this.toolStripButton3.ForeColor = System.Drawing.Color.Black;
358
-
359
179
  this.toolStripButton3.Text = "■";
360
-
361
180
  this.toolStripButton3.Click += new System.EventHandler(this.ToolStripColorButton_Click);
362
181
 
363
-
364
-
365
182
  this.toolStripButton4.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
366
-
367
183
  this.toolStripButton4.ForeColor = System.Drawing.Color.Red;
368
-
369
184
  this.toolStripButton4.Text = "■";
370
-
371
185
  this.toolStripButton4.Click += new System.EventHandler(this.ToolStripColorButton_Click);
372
186
 
373
-
374
-
375
187
  this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
376
-
377
188
  this.toolStripStatusLabel1});
378
189
 
379
-
380
-
381
190
  this.ClientSize = new System.Drawing.Size(800, 450);
382
-
383
191
  this.Controls.Add(this.panel1);
384
-
385
192
  this.Controls.Add(this.statusStrip1);
386
-
387
193
  this.Controls.Add(this.toolStrip1);
388
-
389
194
  ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
390
-
391
195
  this.panel1.ResumeLayout(false);
392
-
393
196
  this.panel1.PerformLayout();
394
-
395
197
  this.toolStrip1.ResumeLayout(false);
396
-
397
198
  this.toolStrip1.PerformLayout();
398
-
399
199
  this.statusStrip1.ResumeLayout(false);
400
-
401
200
  this.statusStrip1.PerformLayout();
402
-
403
201
  this.ResumeLayout(false);
404
-
405
202
  this.PerformLayout();
406
203
 
407
-
408
-
409
- }
204
+ }
410
-
411
205
  #endregion
412
206
 
413
-
414
-
415
207
  private System.Windows.Forms.PictureBox pictureBox1;
416
-
417
208
  private System.Windows.Forms.Panel panel1;
418
-
419
209
  private System.Windows.Forms.ToolStrip toolStrip1;
420
-
421
210
  private System.Windows.Forms.ToolStripButton toolStripButton1;
422
-
423
211
  private System.Windows.Forms.ToolStripButton toolStripButton2;
424
-
425
212
  private System.Windows.Forms.ToolStripComboBox toolStripComboBox1;
426
-
427
213
  private System.Windows.Forms.ToolStripButton toolStripButton3;
428
-
429
214
  private System.Windows.Forms.ToolStripButton toolStripButton4;
430
-
431
215
  private System.Windows.Forms.StatusStrip statusStrip1;
432
-
433
216
  private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
434
-
435
217
  }
436
-
437
218
  }
438
-
439
219
  ```
440
-
441
220
  Designer.csを限界まで削ったので何か不具合があるかもしれません^^;