绘制椭圆表单运行时

时间:2018-06-23 19:18:59

标签: c# winforms gdi

我试图在运行时绘制椭圆形式。我将TransparentKey设置为与backColor相同,并将表单borderStyle设置为none。但是它对我没有用。当我运行下面的代码时,我没有得到椭圆。我不确定这里错过了什么。

public Form1()
{
    InitializeComponent();
    Graphics graphicsObj = this.CreateGraphics();
    SolidBrush sBrush=new SolidBrush(Color.Orange);
    graphicsObj.FillEllipse(sBrush, 30, 30, 60, 30);
    sBrush.Dispose();
    graphicsObj.Dispose();
}

1 个答案:

答案 0 :(得分:2)

在WinForms中进行绘制无法像这样工作,充其量您只会看到一次,但是当Paint事件重新触发时,它将被删除。
您可以做的是在Paint事件中绘制椭圆:

private void OnPaint(object sender, PaintEventArgs e)
{
    var g = e.Graphics;
    SolidBrush sBrush=new SolidBrush(Color.Orange);
    graphicsObj.FillEllipse(sBrush, 30, 30, 60, 30);
    sBrush.Dispose();
}

编辑:

您可以在窗体(“事件”选项卡)上找到OnPaint事件,也可以从构造函数中订阅它:
this.Paint += OnPaint;