在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)
我看过您的代码,并假定线条不可见,因为它们的尺寸变化很小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; }
}