我希望您能为我提供帮助,这是一个很大的代码,所以我将尽力解释一下。我正在制作一个可以放置逻辑门并且可以相互连接的程序,我使用的是OnPaint(PaintEventArgs e)
方法,我很难将门与线连接起来。 (代码也确实很混乱)
我将闸门添加到列表中
私人List<Tuple<Point, int>> droppedShapes = new List<Tuple<Point, int>>();
而我的台词
私人List<Tuple<Point, Point>> allLines = new List<Tuple<Point, Point>>();
这样我就可以保存x-y坐标。
我使用按钮来连接线路,因此在连接的每一端都有一个可以单击的按钮。但是我只能将一个组件的2个输入彼此连接。
这是我的代码,希望您能为我提供帮助!
在这里您可以看到我如何绘制组件和按钮。
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
screen.Clear(Color.Black);
Pen whitePen = new Pen(Color.White);
SolidBrush tealBrush = new SolidBrush(Color.Teal);
SolidBrush whiteBrush = new SolidBrush(Color.White);
SolidBrush yellowBrush = new SolidBrush(Color.Yellow);
SolidBrush grayBrush = new SolidBrush(Color.Gray);
SolidBrush redBrush = new SolidBrush(Color.Red);
foreach (var line in allLines)
{
screen.DrawLine(whitePen, line.Item1, line.Item2);
}
foreach (var pair in this.droppedShapes)
{
var shapeType = pair.Item2; // Reveal your own shape object here
var location = pair.Item1;
switch (shapeType) // Reveal your own shape object here
{
case 0:
screen.DrawRectangle(whitePen, location.X - 10, location.Y - 10, 20, 20);
screen.DrawString("&", new Font(FontFamily.GenericMonospace, (float)18), whiteBrush, location.X - 11, location.Y - 13);
screen.DrawLine(whitePen, location.X - 20, location.Y + 6, location.X - 10, location.Y + 6);
screen.DrawLine(whitePen, location.X - 20, location.Y - 6, location.X - 10, location.Y - 6);
screen.DrawLine(whitePen, location.X + 20, location.Y, location.X + 10, location.Y);
newB = new Point(location.X - 25, location.Y + 2);
newB2 = new Point(location.X - 25, location.Y - 10);
newB3 = new Point(location.X + 20, location.Y - 3);
button1 = new Button();
button2 = new Button();
button3 = new Button();
button1.Size = new Size(8, 8);
button2.Size = new Size(8, 8);
button3.Size = new Size(8, 8);
newB.Offset(0, 0);
newB2.Offset(0, 0);
newB3.Offset(0, 0);
button1.Location = newB;
button2.Location = newB2;
button3.Location = newB3;
button1.Click += Button_Click1;
button2.Click += Button_Click2;
button3.Click += Button_Click3;
drawPanel.Controls.Add(button1);
drawPanel.Controls.Add(button2);
drawPanel.Controls.Add(button3);
if (jaOfNee == true)
{
screen.DrawString(pair.Item1.ToString(), new Font(FontFamily.GenericMonospace, (float) 6), whiteBrush, location.X - 25, location.Y - 25);
}
break;
case 1:
screen.DrawRectangle(whitePen, location.X - 10, location.Y - 10, 20, 20);
screen.DrawString("1", new Font(FontFamily.GenericMonospace, (float)10), whiteBrush, location.X, location.Y - 9);
screen.DrawString(">", new Font(FontFamily.GenericMonospace, (float)8), whiteBrush, location.X - 8, location.Y - 10);
screen.DrawLine(whitePen, location.X + 1, location.Y + 1, location.X - 5, location.Y + 1);
screen.DrawLine(whitePen, location.X - 20, location.Y + 6, location.X - 10, location.Y + 6);
screen.DrawLine(whitePen, location.X - 20, location.Y - 6, location.X - 10, location.Y - 6);
screen.DrawLine(whitePen, location.X + 20, location.Y, location.X + 10, location.Y);
newB = new Point(location.X - 25, location.Y + 2);
newB2 = new Point(location.X - 25, location.Y - 10);
newB3 = new Point(location.X + 20, location.Y - 3);
button1 = new Button();
button2 = new Button();
button3 = new Button();
button1.Size = new Size(8, 8);
button2.Size = new Size(8, 8);
button3.Size = new Size(8, 8);
newB.Offset(0, 0);
newB2.Offset(0, 0);
newB3.Offset(0, 0);
button1.Location = newB;
button2.Location = newB2;
button3.Location = newB3;
button1.Click += Button_Click1;
button2.Click += Button_Click2;
button3.Click += Button_Click3;
drawPanel.Controls.Add(button1);
drawPanel.Controls.Add(button2);
drawPanel.Controls.Add(button3);
if (jaOfNee == true)
{
screen.DrawString(pair.Item1.ToString(), new Font(FontFamily.GenericMonospace, (float) 6), whiteBrush, location.X - 25, location.Y - 25);
}
break;
default:
break;
}
}
for (int i = drawPanel.Left - 5; i < drawPanel.Right + 10; i += 5)
{
for (int j = drawPanel.Top - 50; j < drawPanel.Bottom - 5; j += 5)
{
screen.FillEllipse(redBrush, i, j, 2, 2); //fills screen with a grid
}
}
// base.OnPaint(e);
// draw current location
if (currentLocation != null)
{
Point p = currentLocation.Value;
screen.FillEllipse(tealBrush, p.X + 1, p.Y, -4, -4);
}
drawPanel.CreateGraphics().DrawImage(backBuffer, 0, 0); //allows you to draw
}
}
(这是我尝试连接2个按钮的方式)
foreach (var line in allLines)
{
screen.DrawLine(whitePen, line.Item1, line.Item2);
}
我还没有使用按钮3,因为我想先连接我的输入,所以不要介意按钮3。 这就是我尝试保存X-Y坐标的方式。
private Point point1;
private Point point2;
private void Button_Click1(object sender, EventArgs e)
{
Console.WriteLine("button1");
point1 = button1.Location;
}
private void Button_Click2(object sender, EventArgs e)
{
Console.WriteLine("button2");
point2 = button2.Location;
}
private void Button_Click3(object sender, EventArgs e)
{
Console.WriteLine("button3");
}
private void DrawLine_Click(object sender, EventArgs e)
{
allLines.Add(new Tuple<Point, Point>(point1, point2));
}
当我按下DrawLine按钮时,它将位置添加到列表中。 抱歉,代码太糟糕了。 希望您对我的问题有所了解,这真的很难解释我的问题。