面板上画线没有显示出来 [英] draw line on panel not showing up

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

问题描述

我有一个小组叫PANEL1,我试图借鉴使用此代码我PANEL1一行:

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

Graphics 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);



但没有什么是显示出来。有任何想法吗?这是一个Windows窗体。

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

推荐答案

处理小组的 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天全站免登陆