C# 是否将文本设置为标签的右下边缘?

C# 是否将文本设置为标签的右下边缘?,c#,winforms,label,C#,Winforms,Label,简单的例子,假设我正在创建这样的标签: Label label = new Label(); label.Text = "Hello" + "20.50"; label.Width = 250; label.Height = 100; panel1.Controls.Add(label); 我怎么能说20.50应该出现在标签的右下角 为了清楚起见,我用word做了一个小例子: 我怎样才能做到这一点?感谢您的帮助 以下是您需要的自定义标签: public class CustomLabel :

简单的例子,假设我正在创建这样的标签:

Label label = new Label();
label.Text = "Hello" + "20.50";
label.Width = 250;
label.Height = 100;
panel1.Controls.Add(label);
我怎么能说20.50应该出现在标签的右下角

为了清楚起见,我用word做了一个小例子:


我怎样才能做到这一点?感谢您的帮助

以下是您需要的自定义标签:

public class CustomLabel : Label
{
    public CustomLabel()
    {
        TopLeftText = BottomRightText = "";
        AutoSize = false;
    }
    public string TopLeftText {get;set;}        
    public string BottomRightText {get;set;}                
    protected override void OnPaint(PaintEventArgs e)
    {
        using (StringFormat sf = new StringFormat() { LineAlignment = StringAlignment.Near})
        {
            using(SolidBrush brush = new SolidBrush(ForeColor)){
              e.Graphics.DrawString(TopLeftText, Font, brush, ClientRectangle, sf);
              sf.LineAlignment = StringAlignment.Far;
              sf.Alignment =  StringAlignment.Far;
              e.Graphics.DrawString(BottomRightText, Font, brush, ClientRectangle, sf);
            }
        }
    }
}
//use it:
//first, set its size to what you want.
customLabel1.TopLeftText = house.Name;
customLabel2.BottomRightText = house.Number;

以下是您需要的自定义标签:

public class CustomLabel : Label
{
    public CustomLabel()
    {
        TopLeftText = BottomRightText = "";
        AutoSize = false;
    }
    public string TopLeftText {get;set;}        
    public string BottomRightText {get;set;}                
    protected override void OnPaint(PaintEventArgs e)
    {
        using (StringFormat sf = new StringFormat() { LineAlignment = StringAlignment.Near})
        {
            using(SolidBrush brush = new SolidBrush(ForeColor)){
              e.Graphics.DrawString(TopLeftText, Font, brush, ClientRectangle, sf);
              sf.LineAlignment = StringAlignment.Far;
              sf.Alignment =  StringAlignment.Far;
              e.Graphics.DrawString(BottomRightText, Font, brush, ClientRectangle, sf);
            }
        }
    }
}
//use it:
//first, set its size to what you want.
customLabel1.TopLeftText = house.Name;
customLabel2.BottomRightText = house.Number;

标签控件对此没有内置支持。您需要从Label继承来创建自定义控件,然后自己编写绘制代码

当然,您还需要一些方法来区分这两个字符串。当+符号应用于两个字符串时,它是串联的。这两个字符串由编译器连接在一起,因此您得到的是:Hello20.50。您可能需要使用两个单独的属性,每个属性都有自己的字符串,或者在两个字符串之间插入某种分隔符,以便以后将它们分开。因为您已经创建了一个自定义控件类,所以我将使用单独的属性,这些属性的代码更清晰,更难出错

public class CornerLabel : Label
{
   public string Text2 { get; set; }

   public CornerLabel()
   {
      // This label doesn't support autosizing because the default autosize logic
      // only knows about the primary caption, not the secondary one.
      // 
      // You will either have to set its size manually, or override the
      // GetPreferredSize function and write your own logic. That would not be
      // hard to do: use TextRenderer.MeasureText to determine the space
      // required for both of your strings.
      this.AutoSize = false;
   }

   protected override void OnPaint(PaintEventArgs e)
   {
      // Call the base class to paint the regular caption in the top-left.
      base.OnPaint(e);

      // Paint the secondary caption in the bottom-right.
      TextRenderer.DrawText(e.Graphics,
                            this.Text2,
                            this.Font,
                            this.ClientRectangle,
                            this.ForeColor,
                            TextFormatFlags.Bottom | TextFormatFlags.Right);
   }
}

将此类添加到新文件,生成项目,然后将此控件放到窗体上。确保同时设置Text和Text2属性,然后在设计器中调整控件的大小,并观察发生的情况

标签控件对此没有内置支持。您需要从Label继承来创建自定义控件,然后自己编写绘制代码

当然,您还需要一些方法来区分这两个字符串。当+符号应用于两个字符串时,它是串联的。这两个字符串由编译器连接在一起,因此您得到的是:Hello20.50。您可能需要使用两个单独的属性,每个属性都有自己的字符串,或者在两个字符串之间插入某种分隔符,以便以后将它们分开。因为您已经创建了一个自定义控件类,所以我将使用单独的属性,这些属性的代码更清晰,更难出错

public class CornerLabel : Label
{
   public string Text2 { get; set; }

   public CornerLabel()
   {
      // This label doesn't support autosizing because the default autosize logic
      // only knows about the primary caption, not the secondary one.
      // 
      // You will either have to set its size manually, or override the
      // GetPreferredSize function and write your own logic. That would not be
      // hard to do: use TextRenderer.MeasureText to determine the space
      // required for both of your strings.
      this.AutoSize = false;
   }

   protected override void OnPaint(PaintEventArgs e)
   {
      // Call the base class to paint the regular caption in the top-left.
      base.OnPaint(e);

      // Paint the secondary caption in the bottom-right.
      TextRenderer.DrawText(e.Graphics,
                            this.Text2,
                            this.Font,
                            this.ClientRectangle,
                            this.ForeColor,
                            TextFormatFlags.Bottom | TextFormatFlags.Right);
   }
}

将此类添加到新文件,生成项目,然后将此控件放到窗体上。确保同时设置Text和Text2属性,然后在设计器中调整控件的大小,并观察发生的情况

label.Text=Hello+20.50与label.Text=Hello20.50相同。那么,决定什么应该在左上角,什么应该在右下角的规则是什么呢?没有这样的规则。。在我的真实例子中,我有一个对象,比如说房子。。房子有两个属性:名称和编号。。。现在我将形成文本:label.text=house.Name+house.Number,并且该housenumber应始终显示在右下角..创建一个由两个LabelLabel组成的自定义用户控件。text=Hello+20.50与label.text=Hello20.50相同。那么,决定什么应该在左上角,什么应该在右下角的规则是什么呢?没有这样的规则。。在我的真实例子中,我有一个对象,比如说房子。。房子有两个属性:名称和编号。。。现在,我将形成文本:label.text=house.Name+house.Number,并且housenumber始终应显示在右下角。创建一个由两个Labels组成的自定义usercontrol,但不使用DrawString。更喜欢TextRenderer.DrawText以匹配WinForms绘制控件的方式。DrawText使用GDI,这是Windows在UI内部使用的。抽绳为GDI+。网上有很多关于它的信息。我可能自己也在上面贴了答案。此外,如果要使用TextRenderer.MeasureText,则必须使用相同的子系统来绘制文本。测量结果将略有不同。如果您使用的是抽绳,请使用MeasureString。@CodyGray谢谢您,我希望我能多投一次赞成票。它比我的简单得多,也更好。我在这里发布的代码不完整,如果在运行时更改字体,它将无法工作,它不会包装文本,……除非我添加更多代码来处理更多事件:不过,您的代码很容易修复。没有理由跟踪私有变量中两个字符串的大小。您可以在重新绘制时动态计算该值。这样就省去了大约一半的代码,包括处理事件的需要。然后将DrawString更改为DrawText,这不仅可以改善渲染效果,还可以避免创建新笔刷,在本例中还可以避免泄漏新笔刷。@CodyGray好的,我可以在其他代码中使用DrawText,但不能在本答案中使用,因为它可能看起来像您的答案,我使用添加了更多内容,发现LineAlignment和Alignment的组合将产生与TextFormatFlags相同的效果,刚刚编辑过,现在看起来还可以,只是默认的文本绘图应该与您的答案一样使用。不过,对于我的这个问题,它已经足够了,否则它可能是你代码的一些副本。除非不要使用抽绳。更喜欢TextRenderer.DrawText来匹配WinForms绘制控件的方式
ext使用GDI,这是Windows在UI内部使用的。抽绳为GDI+。网上有很多关于它的信息。我可能自己也在上面贴了答案。此外,如果要使用TextRenderer.MeasureText,则必须使用相同的子系统来绘制文本。测量结果将略有不同。如果您使用的是抽绳,请使用MeasureString。@CodyGray谢谢您,我希望我能多投一次赞成票。它比我的简单得多,也更好。我在这里发布的代码不完整,如果在运行时更改字体,它将无法工作,它不会包装文本,……除非我添加更多代码来处理更多事件:不过,您的代码很容易修复。没有理由跟踪私有变量中两个字符串的大小。您可以在重新绘制时动态计算该值。这样就省去了大约一半的代码,包括处理事件的需要。然后将DrawString更改为DrawText,这不仅可以改善渲染效果,还可以避免创建新笔刷,在本例中还可以避免泄漏新笔刷。@CodyGray好的,我可以在其他代码中使用DrawText,但不能在本答案中使用,因为它可能看起来像您的答案,我使用添加了更多内容,发现LineAlignment和Alignment的组合将产生与TextFormatFlags相同的效果,刚刚编辑过,现在看起来还可以,只是默认的文本绘图应该与您的答案一样使用。但是,对于我的这个问题,它已经足够了,否则它可能是您代码的一些副本。+1用于使用现有的默认文本绘图,:我忘记了。+1用于使用现有的默认文本绘图,:我忘记了。