C# 用c语言在Winform标签中画一个尽可能大的内圈#

C# 用c语言在Winform标签中画一个尽可能大的内圈#,c#,winforms,label,system.drawing,drawing2d,C#,Winforms,Label,System.drawing,Drawing2d,我有一个WinForms标签,我试图在一个尽可能大的标签内画一个内圈(不填充) 我尝试了两种方法,一种应用于label1,另一种应用于两个label2。在这两种情况下,它都不起作用 注:标签应保持其背景颜色和内容 我怎样才能摆脱这个 代码: 下面是一个屏幕截图: 您是在表单上绘制的,而不是在标签上绘制的。尝试处理标签控件的绘制事件,而不是覆盖窗体的OnPaint方法。例如: private void label1_Paint(object sender, PaintEventArgs e) {

我有一个WinForms标签,我试图在一个尽可能大的标签内画一个内圈(不填充)

我尝试了两种方法,一种应用于label1,另一种应用于两个label2。在这两种情况下,它都不起作用

注:标签应保持其背景颜色和内容

我怎样才能摆脱这个

代码:

下面是一个屏幕截图:


您是在
表单上绘制的,而不是在
标签上绘制的。尝试处理
标签
控件的
绘制
事件,而不是覆盖
窗体的
OnPaint
方法。例如:

private void label1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    e.Graphics.DrawEllipse(Pens.Red, 0, 0, label1.Height - 1, label1.Height - 1);
}

您在
表单
上绘图,而不是
标签
。尝试处理
标签
控件的
绘制
事件,而不是覆盖
窗体的
OnPaint
方法。例如:

private void label1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    e.Graphics.DrawEllipse(Pens.Red, 0, 0, label1.Height - 1, label1.Height - 1);
}

与您现在使用的过程相同,从控件的
Paint()
事件调用它们。
如果要创建自定义控件,则相同。在这种情况下,使用重写的
OnPaint()
事件

在控件的
Paint()
事件中,调用一个或多个方法在控件的曲面上绘制形状

private void label1_Paint(object sender, PaintEventArgs e)
{
    DrawCircle1(e.Graphics, label1.ClientRectangle);
}

private void label2_Paint(object sender, PaintEventArgs e)
{
    DrawCircle2(e.Graphics, label2.ClientRectangle);
}
使用控件的
ClientRectangle
边界导出图形的大小。
这里,使用时
ClientRectangle
减少1,使用时减少2。这两种方法以稍微不同的方式计算与绘图区域相关的笔大小

private void DrawCircle1(Graphics g, RectangleF canvas)
{
    canvas.Inflate(-2, -2);
    g.SmoothingMode = SmoothingMode.AntiAlias;

    using (GraphicsPath path = new GraphicsPath())
    using (Pen p = new Pen(Color.Blue, 2)) {
        path.StartFigure();
        path.AddArc(canvas, 0, 360);
        path.CloseFigure();
        g.DrawPath(p, path);
    }
}

private void DrawCircle2(Graphics g, RectangleF canvas)
{
    canvas.Inflate(-1, -1);
    g.SmoothingMode = SmoothingMode.AntiAlias;

     using (Pen p = new Pen(Color.Red, 2)) {
        g.DrawEllipse(p, canvas);
     }
}

与您现在使用的过程相同,从控件的
Paint()
事件调用它们。
如果要创建自定义控件,则相同。在这种情况下,使用重写的
OnPaint()
事件

在控件的
Paint()
事件中,调用一个或多个方法在控件的曲面上绘制形状

private void label1_Paint(object sender, PaintEventArgs e)
{
    DrawCircle1(e.Graphics, label1.ClientRectangle);
}

private void label2_Paint(object sender, PaintEventArgs e)
{
    DrawCircle2(e.Graphics, label2.ClientRectangle);
}
使用控件的
ClientRectangle
边界导出图形的大小。
这里,使用时
ClientRectangle
减少1,使用时减少2。这两种方法以稍微不同的方式计算与绘图区域相关的笔大小

private void DrawCircle1(Graphics g, RectangleF canvas)
{
    canvas.Inflate(-2, -2);
    g.SmoothingMode = SmoothingMode.AntiAlias;

    using (GraphicsPath path = new GraphicsPath())
    using (Pen p = new Pen(Color.Blue, 2)) {
        path.StartFigure();
        path.AddArc(canvas, 0, 360);
        path.CloseFigure();
        g.DrawPath(p, path);
    }
}

private void DrawCircle2(Graphics g, RectangleF canvas)
{
    canvas.Inflate(-1, -1);
    g.SmoothingMode = SmoothingMode.AntiAlias;

     using (Pen p = new Pen(Color.Red, 2)) {
        g.DrawEllipse(p, canvas);
     }
}

看起来你是在表格而不是标签上画画。不要重写表单的
OnPaint
方法,而是尝试处理标签控件的
Paint
事件。我没有对标签尝试此操作,但对于其他控件(例如
ListView
),我需要设置
OwnerDraw=true
。我总是只覆盖要绘制的特定函数(用于控件)。您不能从label1覆盖的代码(
e.Graphics
)绘制到label2。看起来您是在表单而不是标签上绘制的。不要重写表单的
OnPaint
方法,而是尝试处理标签控件的
Paint
事件。我没有对标签尝试此操作,但对于其他控件(例如
ListView
),我需要设置
OwnerDraw=true
。我总是只覆盖要绘制的特定函数(用于控件)。您不能从label1覆盖代码(
e.Graphics
)绘制到label2。答案的要点是我在第一行中提到的,代码只是示例。您可以简单地在paint事件中使用任何其他逻辑。答案的要点是我在第一行中提到的,代码只是示例。您只需在绘制事件中使用任何其他逻辑即可。