0
在C#中,我試圖使用來自Microsoft.VisualBasic.PowerPacks的橢圓和線條組件繪製地圖(通過線條連接的圓形)。 繪製圓形或線條後,我將其父項設置爲形狀容器(Microsoft.VisualBasic.PowerPacks.ShapeContainer())。 我的問題是,線條顯示在圓圈上方,即使在我的代碼中我在圓圈之前創建了線條。 你有什麼想法,我可以把橢圓形前面?C#中橢圓形和線條重疊形狀容器
和代碼本身:
public partial class Map : Form
{
private int map_w, map_h;
private List<int> map_data;
int j, i;
private Microsoft.VisualBasic.PowerPacks.ShapeContainer canvas;
private const int circle_size=75;
private const int circle_spacing = 75;
private List<Microsoft.VisualBasic.PowerPacks.OvalShape> circles = new List<Microsoft.VisualBasic.PowerPacks.OvalShape>();
private List<Microsoft.VisualBasic.PowerPacks.LineShape> lines = new List<Microsoft.VisualBasic.PowerPacks.LineShape>();
/// <summary>
/// Shows the game map
/// </summary>
/// <param name="w">the width of the map</param>
/// <param name="h">the hieght of the map</param>
/// <param name="d">the list containing the map</param>
public Map(int w, int h, List<int> d)
{
InitializeComponent();
this.Width = w * circle_size + circle_spacing * w;//the number of territories vertically * radius of a territory * number of connections between circles
this.Height = h * circle_size + circle_spacing * h + circle_size/2 + circle_size/10;//same as above, only on vertical
canvas = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
canvas.Parent = this;
map_w = w;
map_h = h;
map_data = d;
for(j=0;j<h;j++)//draw lines
for (i = 0; i < w; i++)
{
if (j - 1 >= 0 && i - 1 >= 0 && map_data[i + map_w * j] == 1)//to upper left
if (map_data[(i - 1) + map_w * (j - 1)] == 1)
{
int x1, y1, x2, y2;//start and end points for the line
x1 = (i - 1) * circle_size + Math.Abs((i - 1)) * circle_spacing + circle_size/2;
//the circle(i-1, j-1 because it's upper left) and we also take acccount of the space between circles
y1 = (j - 1) * circle_size + Math.Abs((j - 1)) * circle_spacing + circle_size/2;
x2 = i * circle_size + i * circle_spacing + circle_size/2;
//the second circle(original one)
y2 = j * circle_size + j * circle_spacing + circle_size/2;
Microsoft.VisualBasic.PowerPacks.LineShape line = new Microsoft.VisualBasic.PowerPacks.LineShape(x1, y1, x2, y2);//this is our line
line.Parent = canvas;//we draw it
lines.Add(line);//add it to the array
/*int t1, t2;//for debugging
t1 = i - 1;
t2 = j - 1;
string str = "From " + i + "," + j + " to " + t1 + "," + t2;
//listBox1.Items.Add(str);*/
}
if (j + 1 < map_h && i - 1 >= 0 && map_data[i + map_w * j] == 1)//to lower left
if (map_data[(i - 1) + map_w * (j + 1)] == 1)
{
int x1, y1, x2, y2;
x1 = (i - 1) * circle_size + Math.Abs((i - 1)) * circle_spacing + circle_size/2;
y1 = (j + 1) * circle_size + Math.Abs((j + 1)) * circle_spacing + circle_size/2;
x2 = i * circle_size + i * circle_spacing + circle_size/2;
y2 = j * circle_size + j * circle_spacing + circle_size/2;
Microsoft.VisualBasic.PowerPacks.LineShape line = new Microsoft.VisualBasic.PowerPacks.LineShape(x1, y1, x2, y2);
line.Parent = canvas;
lines.Add(line);
}
if (j + 1 < map_h && map_data[i + map_w * j] == 1)//below
if (map_data[i + map_w * (j + 1)] == 1)
{
int x1, y1, x2, y2;
x1 = i * circle_size + Math.Abs(i) * circle_spacing + circle_size/2;
y1 = (j + 1) * circle_size + Math.Abs((j + 1)) * circle_spacing + circle_size/2;
x2 = i * circle_size + i * circle_spacing + circle_size/2;
y2 = j * circle_size + j * circle_spacing + circle_size/2;
Microsoft.VisualBasic.PowerPacks.LineShape line = new Microsoft.VisualBasic.PowerPacks.LineShape(x1, y1, x2, y2);
line.Parent = canvas;
lines.Add(line);
}
if (i + 1 < map_w && map_data[i + map_w * j] == 1)//right
if (map_data[i + 1 + map_w * j] == 1)
{
int x1, y1, x2, y2;
x1 = (i + 1) * circle_size + Math.Abs(i + 1) * circle_spacing + circle_size/2;
y1 = j * circle_size + Math.Abs(j) * circle_spacing + circle_size/2;
x2 = i * circle_size + i * circle_spacing + circle_size/2;
y2 = j * circle_size + j * circle_spacing + circle_size/2;
Microsoft.VisualBasic.PowerPacks.LineShape line = new Microsoft.VisualBasic.PowerPacks.LineShape(x1, y1, x2, y2);
line.Parent = canvas;
lines.Add(line);
}
}
for(j=0;j<h;j++)//draw circles
for (i = 0; i < w; i++)
{
if (map_data[i + w*j] == 1)//if there's a circle at that location, we draw it
{
//create a circle at the locaiton on screen
Microsoft.VisualBasic.PowerPacks.OvalShape oval = new Microsoft.VisualBasic.PowerPacks.OvalShape(i * circle_size + i * circle_spacing, j * circle_size + j * circle_spacing, circle_size, circle_size);
oval.FillColor = Color.Green;//set the backgorund color
oval.FillStyle = Microsoft.VisualBasic.PowerPacks.FillStyle.Solid;//and fill style
oval.BorderColor = Color.Black;//border
oval.BorderWidth = 2;//and width
oval.Parent = canvas;//set the parent to be the shape container
circles.Add(oval);//and add it to our list
}
}
}
}
這是相同的,甚至這一點。 –
我的錯誤,它的工作原理,謝謝 –
@TenescuAndrei我很高興它的工作。乾杯 –