在标签内画圆圈

时间:2010-02-25 18:35:57

标签: c#

我试图在标签控件中使用Windows Form在运行时绘制一个圆圈。我试图显示绿色或红色圆圈,并在其上显示文本(“L1”或“L2”)。能否请你帮忙。

4 个答案:

答案 0 :(得分:2)

首先,您需要在构造函数中使用SetStyle,以便控制绘制

  this.SetStyle(
                ControlStyles.UserPaint
                | ControlStyles.AllPaintingInWmPaint
                | ControlStyles.SupportsTransparentBackColor
                , true );

然后,您负责绘制整个标签,包括背景和文本。覆盖OnPaint

protected override void OnPaint( PaintEventArgs e ) {
  Graphics g = e.Graphics;
  Brush b = new SolidBrush( this.ForeColor );
  SizeF sf = g.MeasureString( this.Text, this.Font );

  Padding p = Padding.Add( this.Padding, this.Margin );

  // do not forget to include the offset for you circle and text in the x, y position
  float x = p.Left;
  float y = p.Top;

  g.DrawString(  this.Text, this.Font, b, x, y );
}

绘制标签文本后,使用g.DrawEllipse绘制所需的圆圈以及所需的圆圈。然后使用g.DrawString将文本放在它上面。

请记住,如果您必须提供设置属性的绘画的所有内容,那么这仅用于符合您要设置的标准的标签

答案 1 :(得分:1)

要以编程方式执行此操作,您可以使用Label的CreateGraphics方法获取Graphics。然后,您可以使用DrawElipseDrawString方法创建所需的图像。

答案 2 :(得分:0)

如果您有资源Image,则可以在Label上设置Image属性,然后设置Text Alignment和Image Alignment属性以使其落在您想要的位置。

答案 3 :(得分:0)

完全赞同尼克。拥有4种不同组合的图像并动态显示。

希望这有帮助。

谢谢, 拉加

相关问题