【问题标题】:Draw ellipse form runtime绘制椭圆形运行时
【发布时间】:2018-12-02 21:37:19
【问题描述】:

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

【问题讨论】:

  • using,在 c'tor 中绘图。
  • @UweKeim 明白了。但是如何在winform中画椭圆

标签: c# winforms gdi


【解决方案1】:

在 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;

【讨论】:

  • 在哪里可以找到这个 OnPaint 事件
  • 我在表单的事件选项卡中找不到 OnPaint 事件。通过覆盖事件来实现这一点。感谢您的所有帮助。
猜你喜欢
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多