未绘制 Windows 窗体图形

Jon*_*tin 3 c# graphics winforms

我有一些看起来很简单的代码,应该绘制一个椭圆,但它似乎没有出现。这是我的代码:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么重要的东西吗?

ja7*_*a72 5

使用该Paint()事件在您的表单上绘图。我建议PictureBox在表单上使用 a ,因为它不会有太多的闪烁。

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

形式