如何在Panel上画出直线?

new_smile new_smile 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
public class LineObj
{
private Point m_start;
private Point m_end;
public LineObj(Point start,Point end)
{
this.m_start = start;
this.m_end = end;

}
public void draw(Graphics g, Pen pen)
{
g.DrawLine(pen,m_start,m_end);
}

}

private Point m_startPoint = Point.Empty;
List<LineObj> lineList = new List<LineObj>();
private void drawLine(Graphics graphics, Point startPoint, Point endPoint)
{
BufferedGraphicsContext context = BufferedGraphicsManager.Current;
BufferedGraphics bg = context.Allocate(graphics,this.ClientRectangle);
bg.Graphics.Clear(this.BackColor);
foreach(LineObj line in this.lineList)
{
line.draw(bg.Graphics,SystemPens.ControlText);
}
bg.Graphics.DrawLine(SystemPens.ControlText,startPoint,endPoint);
bg.Render();
bg.Dispose();
bg = null;

}

private void onPaint(object sender, PaintEventArgs e)
{
foreach(LineObj line in this.lineList)
{
line.draw(e.Graphics,SystemPens.ControlText);
}
}

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
// base.OnMouseDown(e);
this.m_startPoint = new Point(e.X,e.Y);
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
//base.OnMouseMove(e);
if(e.Button==MouseButtons.Left)
{
this.drawLine(this.CreateGraphics(),m_startPoint,new Point(e.X,e.Y));
}
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
//base.OnMouseUp(e);
LineObj line = new LineObj(this.m_startPoint,e.Location);
this.lineList.Add(line);
这个它不会显示出画在panel上的直线,只会有画在窗体上的直线,该如何修改呢?
...全文
评论 2
收藏 已收藏
分享
返回顶部
new_smile
C# 111,129 成员 642,538 内容