无法在循环中绘制线条
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法在循环中绘制线条相关的知识,希望对你有一定的参考价值。
在mainForm(Form1)中,我正在调用另一个表单的方法。并使用this.Refresh()调用onPaint方法。在onPaint方法中,有一个绘制线的for循环。但DrawLine不起作用
这是代码。
private void onPaintobject sender, PaintEventArgs e)
{
try
{
DrawXYBse(e.Graphics);
if(logModels != null)
SetDots(e.Graphics);
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
public void SetDots(Graphics g)
{
g.DrawLine(Pens.Orange, 80f, 80f, 80.1f, 80.1f);
foreach (LogModel logModel in logModels)
{
g.DrawLine(Pens.Orange, logModel.DX + 80f, logModel.DY + 80f, logModel.DX + 80.1f, logModel.DY + 80.1f);
}
}
public void CallOnPaint(List<LogModel> logModels)
{
this.logModels = logModels;
//call onPaint()
this.Refresh();
}
答案
我查看了你的代码并假设这些行是不可见的,因为它们的尺寸变化太小了0.1像素。 (g.DrawLine(Pens.Orange, logModel.DX + 80f, logModel.DY + 80f, logModel.DX + 80.1f, logModel.DY + 80.1f)
)我把它改为10,他们现在在表格上。 (也许你忘了订阅Paint事件或者可能不调用CallOnPaint):
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Paint += onPaint;
}
List<LogModel> logModels;
private void onPaint(object sender, PaintEventArgs e)
{
try
{
DrawXYBse(e.Graphics);
if (logModels != null)
SetDots(e.Graphics);
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
private void DrawXYBse(Graphics g)
{ }
public void SetDots(Graphics g)
{
g.DrawLine(Pens.Orange, 80f, 80f, 80.1f, 80.1f);
foreach (LogModel logModel in logModels)
{
g.DrawLine(Pens.Orange, logModel.DX + 80f, logModel.DY + 80f, logModel.DX + 90.1f, logModel.DY + 90.1f);
}
}
public void CallOnPaint(List<LogModel> logModels)
{
this.logModels = logModels;
//call onPaint()
this.Refresh();
}
private void button1_Click(object sender, EventArgs e)
{
CallOnPaint(new List<LogModel>()
{
new LogModel(){DX = 20, DY =20},
new LogModel(){DX = 40, DY =30},
new LogModel(){DX = 60, DY =40},
new LogModel(){DX = 80, DY =50},
new LogModel(){DX = 90, DY =60},
new LogModel(){DX = 100, DY =70},
});
}
}
public class LogModel
{
public float DX { get; set; }
public float DY { get; set; }
}
以上是关于无法在循环中绘制线条的主要内容,如果未能解决你的问题,请参考以下文章
无法在 pyqt5 中嵌入的 matplotlib 上绘制线条