C#在标签周围画圆圈 [英] C# draw circle around label

查看:46
本文介绍了C#在标签周围画圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I draw a circle around a label?

现在我已经试过了:

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);
}

但这行不通.有什么想法吗?

But that's not working. Any ideas?

它在winforms中.'它不起作用'我的意思是只有标签显示,但没有圆圈或什么的.

It's in winforms. With 'it's not working' I mean that only the label shows up but no circle or what so ever.

推荐答案

试试这个:

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();
    }
}

结果:

希望有所帮助.

这篇关于C#在标签周围画圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆