質問編集履歴

2

無駄な部分削除 書式再修正 文章微調整

2016/08/31 10:48

投稿

t-kimura
t-kimura

スコア13

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  塗りつぶした図形に枠線をつける
4
4
 
5
- エクセルの[挿入][図形]で作成されるずけいを作成したい
5
+ エクセルの[挿入][図形]で作成される図形を作成したい
6
6
 
7
7
 
8
8
 
@@ -12,10 +12,12 @@
12
12
 
13
13
 
14
14
 
15
- 塗りつぶした図形に対して線がかすれている
15
+ 塗りつぶした図形に対して線がかすれている
16
16
 
17
17
  三角形は、補正値を入れることで対応したが他の多角形や円も綺麗に描画できないかと
18
18
 
19
+
20
+
19
21
  ###該当のソースコード
20
22
 
21
23
  C#
@@ -24,273 +26,249 @@
24
26
 
25
27
  public Form1()
26
28
 
27
- {
28
-
29
- InitializeComponent();
30
-
31
-
32
-
33
- // 図形描画用パネル
34
-
35
- Panel pZukei = new panel
36
-
37
- pZukei.BackColor = System.Drawing.Color.Lime;
38
-
39
- pZukei.Location = new System.Drawing.Point(39, 177);
40
-
41
- pZukei.Name = "userControl11";
42
-
43
- pZukei.Size = new System.Drawing.Size(18, 18);
44
-
45
- pZukei.TabIndex = 9;
46
-
47
- pZukei.Visible = true;
48
-
49
-
50
-
51
- // 高さ変更用
52
-
53
- TextBox txtH=new TextBox();
54
-
55
- this.txtH.Location = new System.Drawing.Point(110, 12);
56
-
57
- this.txtH.Name = "txtH";
58
-
59
- this.txtH.Size = new System.Drawing.Size(100, 19);
60
-
61
- this.txtH.TabIndex = 0;
62
-
63
- this.txtH.Text = "18";
64
-
65
- this.txtH.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
66
-
67
- TextBox txtW=new TextBox();
68
-
69
- txtW.Location = new System.Drawing.Point(110, 37);
70
-
71
- txtW.Name = "txtW";
72
-
73
- txtW.Size = new System.Drawing.Size(100, 19);
74
-
75
- txtW.TabIndex = 1;
76
-
77
- txtW.Text = "18";
78
-
79
- txtW.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
80
-
81
- Button button1 =new Button();
82
-
83
- button1.Location = new System.Drawing.Point(234, 12);
84
-
85
- button1.Name = "button1";
86
-
87
- button1.Size = new System.Drawing.Size(75, 23);
88
-
89
- button1.TabIndex = 2;
90
-
91
- button1.Text = "button1";
92
-
93
- button1.UseVisualStyleBackColor = true;
94
-
95
- button1.Click += new System.EventHandler(this.button1_Click);
96
-
97
- this.Controls.Add(this.pZukei);
98
-
99
- this.Controls.Add(this.txtH);
100
-
101
- this.Controls.Add(this.txtW);
102
-
103
- this.Controls.Add(this.button1);
104
-
105
- }
106
-
107
-
108
-
109
- private void button1_Click(object sender, EventArgs e)
110
-
111
- {
112
-
113
- pZukei.Size = new Size(int.Parse(txtW.Text), int.Parse(txtH.Text));
114
-
115
- pZukei.Region = new Region(Sankaku(uZukei.Size));
116
-
117
- pZukei.Refresh();
118
-
119
-
120
-
121
- pZukei.BackgroundImage = gpLine(pZukei.Size);
122
-
123
- }
124
-
125
-
126
-
127
- private GraphicsPath Sankaku(Size size)
128
-
129
- {
130
-
131
- GraphicsPath gPath = new GraphicsPath();
132
-
133
-
134
-
135
- float fltWidth=0;
136
-
137
- float fltHeight = 0;
138
-
139
- float fltWidth2 = 0;
140
-
141
- float fltHeight2 = 0;
142
-
143
-
144
-
145
- PointF[] pntPos=new PointF[3];// 図形の点の位置
146
-
147
-
148
-
149
- fltWidth = size.Width;
150
-
151
- fltHeight = size.Height;
152
-
153
- fltWidth2 = (float)(size.Width / 2.0);
154
-
155
- fltHeight2 = (float)(size.Height / 2.0);
156
-
157
-
158
-
159
- pntPos[0].X = 0;
160
-
161
- pntPos[0].Y = fltHeight;
162
-
163
- pntPos[1].X = fltWidth2;
164
-
165
- pntPos[1].Y = 0;
166
-
167
- pntPos[2].X = fltWidth;
168
-
169
- pntPos[2].Y = fltHeight;
170
-
171
-
172
-
173
- gPath.AddPolygon(pntPos);
174
-
175
-
176
-
177
-
178
-
179
- return gPath;
180
-
181
- }
182
-
183
-
29
+ {
30
+
31
+ InitializeComponent();
32
+
33
+
34
+
35
+ // 図形描画用パネル
36
+
37
+ Panel pZukei = new panel
38
+
39
+ pZukei.BackColor = System.Drawing.Color.Lime;
40
+
41
+ pZukei.Location = new System.Drawing.Point(39, 177);
42
+
43
+ pZukei.Name = "userControl11";
44
+
45
+ pZukei.Size = new System.Drawing.Size(18, 18);
46
+
47
+ pZukei.TabIndex = 9;
48
+
49
+ pZukei.Visible = true;
50
+
51
+
52
+
53
+ // 高さ変更用
54
+
55
+ TextBox txtH=new TextBox();
56
+
57
+ this.txtH.Location = new System.Drawing.Point(110, 12);
58
+
59
+ this.txtH.Name = "txtH";
60
+
61
+ this.txtH.Size = new System.Drawing.Size(100, 19);
62
+
63
+ this.txtH.TabIndex = 0;
64
+
65
+ this.txtH.Text = "18";
66
+
67
+ this.txtH.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
68
+
69
+ TextBox txtW=new TextBox();
70
+
71
+ txtW.Location = new System.Drawing.Point(110, 37);
72
+
73
+ txtW.Name = "txtW";
74
+
75
+ txtW.Size = new System.Drawing.Size(100, 19);
76
+
77
+ txtW.TabIndex = 1;
78
+
79
+ txtW.Text = "18";
80
+
81
+ txtW.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
82
+
83
+ Button button1 =new Button();
84
+
85
+ button1.Location = new System.Drawing.Point(234, 12);
86
+
87
+ button1.Name = "button1";
88
+
89
+ button1.Size = new System.Drawing.Size(75, 23);
90
+
91
+ button1.TabIndex = 2;
92
+
93
+ button1.Text = "button1";
94
+
95
+ button1.UseVisualStyleBackColor = true;
96
+
97
+ button1.Click += new System.EventHandler(this.button1_Click);
98
+
99
+ this.Controls.Add(this.pZukei);
100
+
101
+ this.Controls.Add(this.txtH);
102
+
103
+ this.Controls.Add(this.txtW);
104
+
105
+ this.Controls.Add(this.button1);
106
+
107
+ }
108
+
109
+
110
+
111
+ // 図形描写
112
+
113
+ private void button1_Click(object sender, EventArgs e)
114
+
115
+ {
116
+
117
+ pZukei.Size = new Size(int.Parse(txtW.Text), int.Parse(txtH.Text));
118
+
119
+ // パネルの形を図形に合わせる
120
+
121
+ pZukei.Region = new Region(Sankaku(uZukei.Size));
122
+
123
+ pZukei.Refresh();
124
+
125
+
126
+
127
+ pZukei.BackgroundImage = gpLine(pZukei.Size);
128
+
129
+ }
130
+
131
+
132
+
133
+ // 図形描写
134
+
135
+ private GraphicsPath Sankaku(Size size)
136
+
137
+ {
138
+
139
+ GraphicsPath gPath = new GraphicsPath();
140
+
141
+
142
+
143
+ float fltWidth=0;
144
+
145
+ float fltHeight = 0;
146
+
147
+ float fltWidth2 = 0;
148
+
149
+ float fltHeight2 = 0;
150
+
151
+
152
+
153
+ PointF[] pntPos=new PointF[3];// 図形の点の位置
154
+
155
+
156
+
157
+ fltWidth = size.Width;
158
+
159
+ fltHeight = size.Height;
160
+
161
+ fltWidth2 = (float)(size.Width / 2.0);
162
+
163
+ fltHeight2 = (float)(size.Height / 2.0);
164
+
165
+
166
+
167
+ pntPos[0].X = 0;
168
+
169
+ pntPos[0].Y = fltHeight;
170
+
171
+ pntPos[1].X = fltWidth2;
172
+
173
+ pntPos[1].Y = 0;
174
+
175
+ pntPos[2].X = fltWidth;
176
+
177
+ pntPos[2].Y = fltHeight;
178
+
179
+
180
+
181
+ gPath.AddPolygon(pntPos);
182
+
183
+
184
+
185
+
186
+
187
+ return gPath;
188
+
189
+ }
190
+
191
+ // 線描写
184
192
 
185
193
  private Image gpLine(Size size)
186
194
 
187
- {
195
+ {
188
-
196
+
189
- //描画先とするImageオブジェクトを作成する
197
+ //描画先とするImageオブジェクトを作成する
190
-
191
-
192
-
198
+
199
+
200
+
193
- PointF[] ptan = null;
201
+ PointF[] ptan = null;
194
-
202
+
195
- Bitmap canvas = new Bitmap(size.Width, size.Height);
203
+ Bitmap canvas = new Bitmap(size.Width, size.Height);
196
-
197
-
198
-
204
+
205
+
206
+
199
- //ImageオブジェクトのGraphicsオブジェクトを作成する
207
+ //ImageオブジェクトのGraphicsオブジェクトを作成する
200
-
208
+
201
- Graphics gPath = Graphics.FromImage(canvas);
209
+ Graphics gPath = Graphics.FromImage(canvas);
202
-
203
-
204
-
210
+
211
+
212
+
205
- float fltWidth = 0;
213
+ float fltWidth = 0;
206
-
214
+
207
- float fltHeight = 0;
215
+ float fltHeight = 0;
208
-
216
+
209
- float fltWidth2 = 0;
217
+ float fltWidth2 = 0;
210
-
211
- //float fltHeight2 = 0;
218
+
212
-
213
-
214
-
219
+
220
+
215
- fltWidth = (float)size.Width - (float)1;
221
+ fltWidth = (float)size.Width - (float)1;
216
-
222
+
217
- fltHeight = (float)size.Height - (float)0.5;
223
+ fltHeight = (float)size.Height - (float)0.5;
218
-
219
-
220
-
224
+
225
+
226
+
221
- fltWidth2 = (float)(size.Width / 2.0);// 三角の上の横位置
227
+ fltWidth2 = (float)(size.Width / 2.0);// 三角の上の横位置
222
-
223
-
224
-
225
-
226
-
228
+
229
+
230
+
227
- ptan = new PointF[3];
231
+ ptan = new PointF[3];
228
232
 
229
233
 
230
234
 
231
- //Penオブジェクトの作成(幅2黒色)
235
+ //Penオブジェクトの作成(幅2黒色)
232
-
236
+
233
- Pen p = new Pen(Color.Black, 2);
237
+ Pen p = new Pen(Color.Black, 2);
234
-
238
+
239
+
240
+
235
- ptan[0].X = (float)0;
241
+ ptan[0].X = (float)0;
236
-
242
+
237
- ptan[0].Y = (float)size.Height ;
243
+ ptan[0].Y = (float)size.Height ;
238
-
239
-
240
-
244
+
245
+
246
+
241
- ptan[1].X = (float)size.Width ;
247
+ ptan[1].X = (float)size.Width ;
242
-
248
+
243
- ptan[1].Y = (float)size.Height ;
249
+ ptan[1].Y = (float)size.Height ;
244
-
245
-
246
-
247
-
248
-
250
+
251
+
252
+
249
- ptan[2].X = (float)size.Width / (float)2;
253
+ ptan[2].X = (float)size.Width / (float)2;
250
-
254
+
251
- ptan[2].Y = (float)0;
255
+ ptan[2].Y = (float)0;
252
-
253
-
254
-
256
+
257
+
258
+
255
- gPath.DrawPolygon(p, ptan);
259
+ gPath.DrawPolygon(p, ptan);
256
-
257
- //左の線
260
+
258
-
259
-
260
-
261
- //gPath.DrawLine(p, (float)0.5, fltHeight, fltWidth2, (float)0.5);
261
+
262
-
263
-
264
-
265
- ////p = new Pen(Color.Blue , 2);
262
+
266
-
267
- //// 右の線
268
-
269
- //gPath.DrawLine(p, fltWidth2,(float)0.5, fltWidth, fltHeight);
270
-
271
-
272
-
273
- ////p = new Pen(Color.Red, 3);
274
-
275
- //// 下線
276
-
277
- //gPath.DrawLine(p, fltWidth, fltHeight, (float)0.5, fltHeight);
278
-
279
-
280
-
281
- //リソースを解放する
263
+ //リソースを解放する
282
-
264
+
283
- gPath.Dispose();
265
+ gPath.Dispose();
284
-
266
+
285
- //PictureBox1に表示する
267
+ //PictureBox1に表示する
286
-
268
+
287
- return canvas;
269
+ return canvas;
288
-
289
-
290
-
291
-
292
-
270
+
293
- }
271
+ }
294
272
 
295
273
  ```
296
274
 
@@ -298,9 +276,9 @@
298
276
 
299
277
  それぞれの頂点の座標をずらすことを試したが
300
278
 
301
- 1図形に対して調整が難しい複数の図形大きさなどにより
279
+ 1図形に対して個々に調整が必要になります(図形の形、大きさ等)
302
-
303
- 変わってしまいます。
280
+
281
+
304
282
 
305
283
 
306
284
 

1

タグ追加 書式修正

2016/08/31 10:48

投稿

t-kimura
t-kimura

スコア13

test CHANGED
File without changes
test CHANGED
@@ -20,6 +20,8 @@
20
20
 
21
21
  C#
22
22
 
23
+ ```
24
+
23
25
  public Form1()
24
26
 
25
27
  {
@@ -290,7 +292,7 @@
290
292
 
291
293
  }
292
294
 
293
-
295
+ ```
294
296
 
295
297
  ###試したこと
296
298