如何在面板上画一条线?它没有出现 [英] How to draw a line on panel? It is not showing up

查看:72
本文介绍了如何在面板上画一条线?它没有出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 panel1Panel,我正在尝试使用以下代码在我的 panel1 上画一条线:

I have a Panel called panel1 and I am trying to draw a line on my panel1 using this code:

var g = panel1.CreateGraphics();
var p = new Pen(Color.Black, 3);

var point1 = new Point(234,118);
var point2 = new Point(293,228);

g.DrawLine(p, point1, point2);

但是什么都没有出现.有任何想法吗?这是一个窗口形式.

But nothing is showing up. Any ideas? This is in a windows form.

推荐答案

处理面板的 Paint event 并将其放在那里.发生的事情是它在构造函数中被绘制一次,然后在每次被调用时在 Paint 事件中被绘制.

Handle the Panel's Paint event and put it in there. What's happening is that it's being drawn once in the constructor but then being drawn over in the Paint event everytime it's called.

private void panel1_Paint(object sender, PaintEventArgs e)
{
    base.OnPaint(e);
    using(Graphics g = e.Graphics)
    {
       var p = new Pen(Color.Black, 3);
       var point1 = new Point(234,118);
       var point2 = new Point(293,228);
       g.DrawLine(p, point1, point2);
    }
}

这篇关于如何在面板上画一条线?它没有出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆