我试图在运行时绘制椭圆形式。我将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();
}
答案 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;