winforms 未绘制Windows窗体图形

slwdgvem  于 2023-08-07  发布在  Windows
关注(0)|答案(2)|浏览(136)

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

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

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

v440hwme

v440hwme1#

使用Paint()事件在窗体上绘制。我建议在表单上使用PictureBox,因为它不会有太多的 Flink 。

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();
    }
}

字符串


的数据

63lcw9qa

63lcw9qa2#

PictureBox工作正常,但如果您想直接在窗体上绘制,则可以使用窗体自己的OnPaint事件,如:

protected override void OnPaint(PaintEventArgs pe)
{
    base.OnPaint(pe);
    for (int x = 0; x < 100; x++)
    {
            for (int y = 0; y < 100; y++)
            {
                pe.Graphics.FillEllipse(Brushes.Tomato, new Rectangle(x, y, 2, 2));
            }
    }
}

字符串

相关问题