C# 在标签周围绘制圆圈[关闭]

2024-05-03

如何在标签周围画一个圆圈?

现在我已经尝试过这个:

public void drawUseCase(int width, int height, UseCase useCase)
{
    Label lbUseCase = new Label();
    Graphics g = lbUseCase.CreateGraphics();
    Pen p = new Pen(Color.Black, 1);
    g.DrawEllipse(p, width, height, 200, 200);
    lbUseCase.Location = new System.Drawing.Point(width, height);
    lbUseCase.Text = useCase.name;
    mainPanel.Controls.Add(lbUseCase);
}

但这是行不通的。有任何想法吗?

是在winforms里的。对于“它不起作用”,我的意思是只显示标签,但没有圆圈或其他任何东西。


尝试这个:

private void Form1_Load(object sender, EventArgs e)
{
    Label Label = new Label();
    Label.Location = new System.Drawing.Point(50, 50);
    Label.Width = 50;
    Label.Height = 50;
    Label.Name = "lblTest";
    Label.Text = "test";
    this.Controls.Add(Label);
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    var lbl = this.Controls.Find("lblTest",true); // find label with name

    foreach (var item in lbl) 
    // there can be multiple lblTest with same name so I used foreach (this is optional btw you can remove it)
    {
        Label tempLabel = item as Label;
        System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
        System.Drawing.Pen myPen = new Pen(myBrush, 2);
        e.Graphics.DrawEllipse(myPen, new System.Drawing.Rectangle(tempLabel.Location.X - (tempLabel.Width / 2),
        tempLabel.Location.Y - (tempLabel.Height / 2)  , tempLabel.Width + 40, tempLabel.Height + 40));
        myBrush.Dispose();
        myPen.Dispose();
    }
}

Result: enter image description here

希望有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# 在标签周围绘制圆圈[关闭] 的相关文章

随机推荐