Windows Forms graphics not being drawn

本小妞迷上赌 提交于 2020-01-04 19:07:10

问题


I have some code that looks pretty simple and should draw an ellipse, but it doesn't seem to appear. Here is my code:

public partial class ThreeBodySim : Form
{

    public ThreeBodySim()
    {
        InitializeComponent();
        this.DoubleBuffered = true;
        Graphics graphics = displayPanel.CreateGraphics(); // Separate panel to display graphics
        Rectangle bbox1 = new Rectangle(30, 40, 50, 50);
        graphics.DrawEllipse(new Pen(Color.AliceBlue), bbox1);
    }
}

Am I missing something important?


回答1:


Use the Paint() event to draw on your form. I recommend using a PictureBox on the form as it will not have as much flicker.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        this.DoubleBuffered=true;
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

        Rectangle bbox1=new Rectangle(30, 40, 50, 50);
        e.Graphics.DrawEllipse(new Pen(Color.Purple), bbox1);
    }

    private void pictureBox1_Resize(object sender, EventArgs e)
    {
        pictureBox1.Invalidate();
    }
}



来源:https://stackoverflow.com/questions/24130298/windows-forms-graphics-not-being-drawn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!