覆盖C#中派生的Label控件的AutoSize [英] Override AutoSize for derived Label Control in C#

查看:340
本文介绍了覆盖C#中派生的Label控件的AutoSize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展System.Windows.Forms.Label类以支持垂直绘制的文本.为此,我创建了一个名为MyLabelOrientation的新属性,用户可以将其设置为Horizo​​ntal或Vertical.当用户更改此设置时,将交换宽度和高度的值,以将控件的大小调整为新的方向.最后,我重写了OnPaint函数以绘制我的标签.

I am trying to extend the System.Windows.Forms.Label class to support vertically drawn text. I do this by creating a new property called MyLabelOrientation that the user can set to Horizontal or Vertical. When the user changes this setting, the values for width and height are swapped to resize the control to its new orientation. Finally, I override the OnPaint function to draw my Label.

我也想扩展此控件的AutoSize属性,以便我的Label自动调整其包含的文本的大小.对于水平方向,基本功能为我实现了这一点.对于垂直方向,我创建一个Graphics对象,并将控件的高度设置为从Graphics.MeasureString(Text,Font)返回的SizeF对象的宽度.您可以在下面看到我正在使用的代码示例.

I would like to extend the AutoSize property for this control as well so that my Label will auto-size to the text it contains. For the horizontal orientation, the base functionality implements this for me. For the vertical orientation, I create a Graphics object and set the height of the control to the width of the SizeF object returned from Graphics.MeasureString(Text, Font). You can see an example of the code I'm using below.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;

public class MyLabel : Label
{
    public enum MyLabelOrientation {Horizontal, Vertical};
    protected MyLabelOrientation m_orientation = MyLabelOrientation.Horizontal;

    [Category("Appearance")]
    public virtual MyLabelOrientation Orientation
    {
        get { return m_orientation; }
        set
        {
            m_orientation = value;
            int temp = Height;
            Width = Height;
            Height = temp;
            Refresh();
        }
    }

    private Size ResizeLabel()
    {
        Graphics g = Graphics.FromHwnd(this.Handle);
        SizeF newSize = g.MeasureString(Text, Font);
        if (m_orientation == MyLabelOrientation.Horizontal)
            Width = (int)newSize.Width;
        else
            Height = (int)newSize.Width;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Brush textBrush = new SolidBrush(this.ForeColor);

        if (m_orientation == LabelOrientation.Vertical)
        {
            e.Graphics.TranslateTransform(Width, 0);
            e.Graphics.RotateTransform(90);
            e.Graphics.DrawString(Text, Font, textBrush, Padding.Left, Padding.Top);
        }
        else
        {
            base.OnPaint(e);
        }
    }
}

但是,将AutoSize设置为true似乎可以防止和/或覆盖对控件大小的任何更改.这意味着我想更改标签的方向时无法更改宽度或高度.我想知道是否可以重写此行为,以便可以测试是否设置了AutoSize,然后根据控件的方向调整控件的大小.

However, setting AutoSize to true seems to prevent and/or override any changes to the size of the control. This means that I can't change the width or height when I want to change the Label's orientation. I'm wondering if this behavior can be overridden, so that I can test whether AutoSize is set, and then adjust the size of the control according to it's orientation.

推荐答案

我以前没有做过,我相信您理论上可以覆盖属性声明(通过new关键字)并在继续操作之前检查方向:

I have not done this before, I believe you can theoretically override a property declaration (via the new keyword) and check the orientation before proceeding:

override public bool AutoSize
{
   set 
   {
      if( /* orientation is horizontal */ )
      {
          base.AutoSize = value;
      }
      else
      {
          // do what you need to do
      }    
   }    
}

这篇关于覆盖C#中派生的Label控件的AutoSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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