IDE:visual studio,c#,Windows from application
我想在面板上画一条线。我可以通过点击它在panel1上画线。
//Code
public partial class Form1 : Form
{
static int px=5, py=5;
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Red, 5, 5, px, py);
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
initilizeXY(e.X, e.Y);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
}
private void initilizeXY( int pxx, int pyy)
{
px = pxx;
py = pyy;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
panel1.Refresh();
}
private void panel2_MouseDown(object sender, MouseEventArgs e)
{
initilizeXY(e.X, e.Y);
}
}
//通过此代码,我可以在面板1上向下绘制一条线。
但是由于一些需求变化,还有另一个面板(panel2),它与panel1部分重叠。
现在我想在panel1上绘制相同的行,如果用户单击panel1或panel2。
请建议如何完成这项工作?
答案 0 :(得分:0)
编辑:修复了正常运行的代码。 在panel2与panel1右侧的panel1重叠的情况下测试,从panel1的顶部开始略低。
写这段代码我假设panel2的X和Y坐标大于panel1。如果是相反的话,也许你应该反转偏移量计算。
public partial class Form1 : Form
{
static int px = 5, py = 5;
static int p2x = 0, p2y = 0;
int offsetX;
int offsetY;
public Form1()
{
InitializeComponent();
offsetX = panel2.Location.X - panel1.Location.X;
offsetY = panel2.Location.Y - panel1.Location.Y;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Red, 5, 5, px, py);
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
initilizeXY(e.X, e.Y);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void initilizeXY(int pxx, int pyy)
{
px = pxx;
py = pyy;
}
private void initilizeXY2(int pxx, int pyy)
{
p2x = pxx;
p2y = pyy;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
panel1.Refresh();
panel2.Refresh();
}
private void panel2_MouseDown(object sender, MouseEventArgs e)
{
initilizeXY(e.X+offsetX, e.Y+offsetY);
initilizeXY2(e.X, e.Y);
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
if (px > offsetX && py > offsetY)
{
int p2start = findIntersectFromLineEquation(new Point(5, 5), new Point(px, py));
e.Graphics.DrawLine(Pens.Red, 0, p2start - offsetY, p2x, p2y);
}
}
private int findIntersectFromLineEquation(Point start, Point end)
{
if (start.X == end.X || start.Y == end.Y)
return 0;
double a = (double)(end.Y - start.Y) / (double)(end.X - start.X);
double b = (double)(start.Y) - (double)(a * start.X);
return (int)(a * offsetX + b);
}
}
请记住相应地订阅事件。
答案 1 :(得分:0)
此代码有效:
Point bgn1 = new Point(5, 5);
Point end1 = new Point(5, 5);
Point bgn2 = new Point(0, 0);
Point end2 = new Point(0, 0);
private void Form1_Load(object sender, EventArgs e)
{
Point pnt, pntscr;
pnt.X = 5;
pnt.Y = 5;
pntscr = Panel1.PointToScreen(pnt);
bgn2 = Panel2.PointToClient(pntscr);
end2 = bgn2;
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
end1.X = e.X;
end1.Y = e.Y;
Point pntscr;
pntscr = Panel1.PointToScreen(end1);
end2 = Panel2.PointToClient(pntscr);
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
panel1.Refresh();
panel2.Refresh();
}
private void panel2_MouseDown(object sender, MouseEventArgs e)
{
end2.X = e.X;
end2.Y = e.Y;
Point pntscr;
pntscr = Panel2.PointToScreen(end2);
end1 = Panel1.PointToClient(pntscr);
}
private void panel2_MouseUp(object sender, MouseEventArgs e)
{
panel1.Refresh();
panel2.Refresh();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Red, bgn1, end1);
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Red, bgn2, end2);
}
瓦尔特