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

質問編集履歴

2

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

2016/08/31 10:48

投稿

t-kimura
t-kimura

スコア13

title CHANGED
File without changes
body CHANGED
@@ -1,156 +1,145 @@
1
1
  ###前提・実現したいこと
2
2
  塗りつぶした図形に枠線をつける
3
- エクセルの[挿入][図形]で作成されるずけいを作成したい
3
+ エクセルの[挿入][図形]で作成される図形を作成したい
4
4
 
5
5
  よろしくお願いします。
6
6
  ###発生している問題・エラーメッセージ
7
7
 
8
- 塗りつぶした図形に対して線がかすれている
8
+ 塗りつぶした図形に対して線がかすれている
9
9
  三角形は、補正値を入れることで対応したが他の多角形や円も綺麗に描画できないかと
10
+
10
11
  ###該当のソースコード
11
12
  C#
12
13
  ```
13
14
  public Form1()
14
- {
15
+ {
15
- InitializeComponent();
16
+ InitializeComponent();
16
17
 
17
- // 図形描画用パネル
18
+ // 図形描画用パネル
18
- Panel pZukei = new panel
19
+ Panel pZukei = new panel
19
- pZukei.BackColor = System.Drawing.Color.Lime;
20
+ pZukei.BackColor = System.Drawing.Color.Lime;
20
- pZukei.Location = new System.Drawing.Point(39, 177);
21
+ pZukei.Location = new System.Drawing.Point(39, 177);
21
- pZukei.Name = "userControl11";
22
+ pZukei.Name = "userControl11";
22
- pZukei.Size = new System.Drawing.Size(18, 18);
23
+ pZukei.Size = new System.Drawing.Size(18, 18);
23
- pZukei.TabIndex = 9;
24
+ pZukei.TabIndex = 9;
24
- pZukei.Visible = true;
25
+ pZukei.Visible = true;
25
26
 
26
- // 高さ変更用
27
+ // 高さ変更用
27
- TextBox txtH=new TextBox();
28
+ TextBox txtH=new TextBox();
28
- this.txtH.Location = new System.Drawing.Point(110, 12);
29
+ this.txtH.Location = new System.Drawing.Point(110, 12);
29
- this.txtH.Name = "txtH";
30
+ this.txtH.Name = "txtH";
30
- this.txtH.Size = new System.Drawing.Size(100, 19);
31
+ this.txtH.Size = new System.Drawing.Size(100, 19);
31
- this.txtH.TabIndex = 0;
32
+ this.txtH.TabIndex = 0;
32
- this.txtH.Text = "18";
33
+ this.txtH.Text = "18";
33
- this.txtH.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
34
+ this.txtH.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
34
- TextBox txtW=new TextBox();
35
+ TextBox txtW=new TextBox();
35
- txtW.Location = new System.Drawing.Point(110, 37);
36
+ txtW.Location = new System.Drawing.Point(110, 37);
36
- txtW.Name = "txtW";
37
+ txtW.Name = "txtW";
37
- txtW.Size = new System.Drawing.Size(100, 19);
38
+ txtW.Size = new System.Drawing.Size(100, 19);
38
- txtW.TabIndex = 1;
39
+ txtW.TabIndex = 1;
39
- txtW.Text = "18";
40
+ txtW.Text = "18";
40
- txtW.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
41
+ txtW.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
41
- Button button1 =new Button();
42
+ Button button1 =new Button();
42
- button1.Location = new System.Drawing.Point(234, 12);
43
+ button1.Location = new System.Drawing.Point(234, 12);
43
- button1.Name = "button1";
44
+ button1.Name = "button1";
44
- button1.Size = new System.Drawing.Size(75, 23);
45
+ button1.Size = new System.Drawing.Size(75, 23);
45
- button1.TabIndex = 2;
46
+ button1.TabIndex = 2;
46
- button1.Text = "button1";
47
+ button1.Text = "button1";
47
- button1.UseVisualStyleBackColor = true;
48
+ button1.UseVisualStyleBackColor = true;
48
- button1.Click += new System.EventHandler(this.button1_Click);
49
+ button1.Click += new System.EventHandler(this.button1_Click);
49
- this.Controls.Add(this.pZukei);
50
+ this.Controls.Add(this.pZukei);
50
- this.Controls.Add(this.txtH);
51
+ this.Controls.Add(this.txtH);
51
- this.Controls.Add(this.txtW);
52
+ this.Controls.Add(this.txtW);
52
- this.Controls.Add(this.button1);
53
+ this.Controls.Add(this.button1);
53
- }
54
+ }
54
55
 
56
+ // 図形描写
55
- private void button1_Click(object sender, EventArgs e)
57
+ private void button1_Click(object sender, EventArgs e)
56
- {
58
+ {
57
- pZukei.Size = new Size(int.Parse(txtW.Text), int.Parse(txtH.Text));
59
+ pZukei.Size = new Size(int.Parse(txtW.Text), int.Parse(txtH.Text));
60
+ // パネルの形を図形に合わせる
58
- pZukei.Region = new Region(Sankaku(uZukei.Size));
61
+ pZukei.Region = new Region(Sankaku(uZukei.Size));
59
- pZukei.Refresh();
62
+ pZukei.Refresh();
60
63
 
61
- pZukei.BackgroundImage = gpLine(pZukei.Size);
64
+ pZukei.BackgroundImage = gpLine(pZukei.Size);
62
- }
65
+ }
63
66
 
67
+ // 図形描写
64
- private GraphicsPath Sankaku(Size size)
68
+ private GraphicsPath Sankaku(Size size)
65
- {
69
+ {
66
- GraphicsPath gPath = new GraphicsPath();
70
+ GraphicsPath gPath = new GraphicsPath();
67
71
 
68
- float fltWidth=0;
72
+ float fltWidth=0;
69
- float fltHeight = 0;
73
+ float fltHeight = 0;
70
- float fltWidth2 = 0;
74
+ float fltWidth2 = 0;
71
- float fltHeight2 = 0;
75
+ float fltHeight2 = 0;
72
76
 
73
- PointF[] pntPos=new PointF[3];// 図形の点の位置
77
+ PointF[] pntPos=new PointF[3];// 図形の点の位置
74
78
 
75
- fltWidth = size.Width;
79
+ fltWidth = size.Width;
76
- fltHeight = size.Height;
80
+ fltHeight = size.Height;
77
- fltWidth2 = (float)(size.Width / 2.0);
81
+ fltWidth2 = (float)(size.Width / 2.0);
78
- fltHeight2 = (float)(size.Height / 2.0);
82
+ fltHeight2 = (float)(size.Height / 2.0);
79
83
 
80
- pntPos[0].X = 0;
84
+ pntPos[0].X = 0;
81
- pntPos[0].Y = fltHeight;
85
+ pntPos[0].Y = fltHeight;
82
- pntPos[1].X = fltWidth2;
86
+ pntPos[1].X = fltWidth2;
83
- pntPos[1].Y = 0;
87
+ pntPos[1].Y = 0;
84
- pntPos[2].X = fltWidth;
88
+ pntPos[2].X = fltWidth;
85
- pntPos[2].Y = fltHeight;
89
+ pntPos[2].Y = fltHeight;
86
90
 
87
- gPath.AddPolygon(pntPos);
91
+ gPath.AddPolygon(pntPos);
88
92
 
89
93
 
90
- return gPath;
94
+ return gPath;
91
- }
95
+ }
92
-
96
+ // 線描写
93
97
  private Image gpLine(Size size)
94
- {
98
+ {
95
- //描画先とするImageオブジェクトを作成する
99
+ //描画先とするImageオブジェクトを作成する
96
100
 
97
- PointF[] ptan = null;
101
+ PointF[] ptan = null;
98
- Bitmap canvas = new Bitmap(size.Width, size.Height);
102
+ Bitmap canvas = new Bitmap(size.Width, size.Height);
99
103
 
100
- //ImageオブジェクトのGraphicsオブジェクトを作成する
104
+ //ImageオブジェクトのGraphicsオブジェクトを作成する
101
- Graphics gPath = Graphics.FromImage(canvas);
105
+ Graphics gPath = Graphics.FromImage(canvas);
102
106
 
103
- float fltWidth = 0;
107
+ float fltWidth = 0;
104
- float fltHeight = 0;
108
+ float fltHeight = 0;
105
- float fltWidth2 = 0;
109
+ float fltWidth2 = 0;
110
+
111
+ fltWidth = (float)size.Width - (float)1;
106
- //float fltHeight2 = 0;
112
+ fltHeight = (float)size.Height - (float)0.5;
107
113
 
108
- fltWidth = (float)size.Width - (float)1;
109
- fltHeight = (float)size.Height - (float)0.5;
110
-
111
- fltWidth2 = (float)(size.Width / 2.0);// 三角の上の横位置
114
+ fltWidth2 = (float)(size.Width / 2.0);// 三角の上の横位置
112
-
113
115
 
114
- ptan = new PointF[3];
116
+ ptan = new PointF[3];
115
117
 
116
- //Penオブジェクトの作成(幅2黒色)
118
+ //Penオブジェクトの作成(幅2黒色)
117
- Pen p = new Pen(Color.Black, 2);
119
+ Pen p = new Pen(Color.Black, 2);
118
- ptan[0].X = (float)0;
119
- ptan[0].Y = (float)size.Height ;
120
120
 
121
- ptan[1].X = (float)size.Width ;
121
+ ptan[0].X = (float)0;
122
- ptan[1].Y = (float)size.Height ;
122
+ ptan[0].Y = (float)size.Height ;
123
123
 
124
+ ptan[1].X = (float)size.Width ;
125
+ ptan[1].Y = (float)size.Height ;
124
126
 
125
- ptan[2].X = (float)size.Width / (float)2;
127
+ ptan[2].X = (float)size.Width / (float)2;
126
- ptan[2].Y = (float)0;
128
+ ptan[2].Y = (float)0;
127
129
 
128
- gPath.DrawPolygon(p, ptan);
130
+ gPath.DrawPolygon(p, ptan);
129
- //左の線
130
-
131
- //gPath.DrawLine(p, (float)0.5, fltHeight, fltWidth2, (float)0.5);
132
131
 
133
- ////p = new Pen(Color.Blue , 2);
134
- //// 右の線
135
- //gPath.DrawLine(p, fltWidth2,(float)0.5, fltWidth, fltHeight);
136
-
137
- ////p = new Pen(Color.Red, 3);
138
- //// 下線
139
- //gPath.DrawLine(p, fltWidth, fltHeight, (float)0.5, fltHeight);
140
-
141
- //リソースを解放する
132
+ //リソースを解放する
142
- gPath.Dispose();
133
+ gPath.Dispose();
143
- //PictureBox1に表示する
134
+ //PictureBox1に表示する
144
- return canvas;
135
+ return canvas;
145
-
146
-
147
- }
136
+ }
148
137
  ```
149
138
  ###試したこと
150
139
  それぞれの頂点の座標をずらすことを試したが
151
- 1図形に対して調整が難しい複数の図形大きさなどにより
140
+ 1図形に対して個々に調整が必要になります(図形の形、大きさ等)
152
- 変わってしまいます。
153
141
 
142
+
154
143
  ###補足情報(言語/FW/ツール等のバージョンなど)
155
144
  Microsoft Visual Studio 2008
156
145
  C#にて作成

1

タグ追加 書式修正

2016/08/31 10:48

投稿

t-kimura
t-kimura

スコア13

title CHANGED
File without changes
body CHANGED
@@ -9,6 +9,7 @@
9
9
  三角形は、補正値を入れることで対応したが他の多角形や円も綺麗に描画できないかと
10
10
  ###該当のソースコード
11
11
  C#
12
+ ```
12
13
  public Form1()
13
14
  {
14
15
  InitializeComponent();
@@ -144,7 +145,7 @@
144
145
 
145
146
 
146
147
  }
147
-
148
+ ```
148
149
  ###試したこと
149
150
  それぞれの頂点の座標をずらすことを試したが
150
151
  1図形に対して調整が難しい複数の図形大きさなどにより