winform加载时,为什么画图不执行了
今天看C#图形处理,发现一个问题,不知道是我的原因,还是确实存在
设计界面
后台代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Graphic_Processing
{
public partial class DrawLine : Form
{
public DrawLine()
{
InitializeComponent();
}
private void DrawLine_Load(object sender, EventArgs e)
{
Pen redpen = new Pen(Color.Red,10);
Point p1 = new Point(10,50);
Point p2 = new Point(10,200);
Graphics g = this.CreateGraphics();
g.DrawLine(redpen, p1, p2);
}
private void button1_Click(object sender, EventArgs e)
{
Pen redpen = new Pen(Color.Black, 10);
Point p1 = new Point(10, 50);
Point p2 = new Point(200, 50);
Graphics g = this.CreateGraphics();
g.DrawLine(redpen, p1, p2);
}
大家猜猜会出现什么情况啊,
程序加载时:
按钮点击时
为什么会出现这样的问题了,我真的不知道啊。很好奇啊,如果哪位大师知道,请告诉我原因吧
想了想可能是窗体重绘的结果,把原来的给重绘掉了
如果把那段代码这么写,就不会出错了
private void DrawLine_Paint(object sender, EventArgs e)
{
Pen redpen = new Pen(Color.Red,10);
Point p1 = new Point(10,50);
Point p2 = new Point(10,200);
Graphics g = this.CreateGraphics();
g.DrawLine(redpen, p1, p2);
}