C#在标签周围画圆圈

Jam*_*mie 2 c# winforms

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

现在我已经尝试过这个:

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);
}
Run Code Online (Sandbox Code Playgroud)

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

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

Ber*_*kay 5

尝试这个:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

结果: 在此处输入图片说明

希望有所帮助。

  • 动画 gif 总是很酷。不管什么语境。 (4认同)
  • 不要忘记处理你的刷子(`使用`!) (2认同)