我不喜欢Label控件的AutoSize属性。我有一个自定义标签,绘制一个花哨的圆形边框等。我在构造函数中放置了 AutoSize = false ,但是,当我将它置于设计模式时,属性始终为True。

我已经成功地覆盖了其他属性但是这个很高兴忽略了我。如果这是“通过MS设计”,是否有人有线索?

以下是我感兴趣的标签的完整源代码。

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

namespace Dentactil.UI.WinControls
{
    [DefaultProperty("TextString")]
    [DefaultEvent("TextClick")]
    public partial class RoundedLabel : UserControl
    {
        private static readonly Color DEFAULT_BORDER_COLOR = Color.FromArgb( 132, 100, 161 );
        private const float DEFAULT_BORDER_WIDTH = 2.0F;
        private const int DEFAULT_ROUNDED_WIDTH = 16;
        private const int DEFAULT_ROUNDED_HEIGHT = 12;

        private Color mBorderColor = DEFAULT_BORDER_COLOR;
        private float mBorderWidth = DEFAULT_BORDER_WIDTH;
        private int mRoundedWidth = DEFAULT_ROUNDED_WIDTH;
        private int mRoundedHeight = DEFAULT_ROUNDED_HEIGHT;

        public event EventHandler TextClick;

        private Padding mPadding = new Padding(8);

        public RoundedLabel()
        {
            InitializeComponent();
        }

        public Cursor TextCursor
        {
            get { return lblText.Cursor; }
            set { lblText.Cursor = value; }
        }

        public Padding TextPadding
        {
            get { return mPadding; }
            set
            {
                mPadding = value;
                UpdateInternalBounds();
            }
        }

        public ContentAlignment TextAlign
        {
            get { return lblText.TextAlign; }
            set { lblText.TextAlign = value; }
        }

        public string TextString
        {
            get { return lblText.Text; }
            set { lblText.Text = value; }
        }

        public override Font Font
        {
            get { return base.Font; }
            set
            {
                base.Font = value;
                lblText.Font = value;
            }
        }

        public override Color ForeColor
        {
            get { return base.ForeColor; }
            set
            {
                base.ForeColor = value;
                lblText.ForeColor = value;
            }
        }

        public Color BorderColor
        {
            get { return mBorderColor; }
            set
            {
                mBorderColor = value;
                Invalidate();
            }
        }

        [DefaultValue(DEFAULT_BORDER_WIDTH)]
        public float BorderWidth
        {
            get { return mBorderWidth; }
            set
            {
                mBorderWidth = value;
                Invalidate();
            }
        }

        [DefaultValue(DEFAULT_ROUNDED_WIDTH)]
        public int RoundedWidth
        {
            get { return mRoundedWidth; }
            set
            {
                mRoundedWidth = value;
                Invalidate();
            }
        }

        [DefaultValue(DEFAULT_ROUNDED_HEIGHT)]
        public int RoundedHeight
        {
            get { return mRoundedHeight; }
            set
            {
                mRoundedHeight = value;
                Invalidate();
            }
        }

        private void UpdateInternalBounds()
        {
            lblText.Left = mPadding.Left;
            lblText.Top = mPadding.Top;

            int width = Width - mPadding.Right - mPadding.Left;
            lblText.Width = width > 0 ? width : 0;

            int heigth = Height - mPadding.Bottom - mPadding.Top;
            lblText.Height = heigth > 0 ? heigth : 0;
        }

        protected override void OnLoad(EventArgs e)
        {
            UpdateInternalBounds();
            base.OnLoad(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            SmoothingMode smoothingMode = e.Graphics.SmoothingMode;
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            int roundedWidth = RoundedWidth > (Width - 1)/2 ? (Width - 1)/2 : RoundedWidth;
            int roundedHeight = RoundedHeight > (Height - 1)/2 ? (Height - 1)/2 : RoundedHeight;

            GraphicsPath path = new GraphicsPath();
            path.AddLine(0, roundedHeight, 0, Height - 1 - roundedHeight);
            path.AddArc(new RectangleF(0, Height - 1 - 2*roundedHeight, 2*roundedWidth, 2*roundedHeight), 180, -90);
            path.AddLine(roundedWidth, Height - 1, Width - 1 - 2*roundedWidth, Height - 1);
            path.AddArc(new RectangleF(Width - 1 - 2*roundedWidth, Height - 1 - 2*roundedHeight, 2*roundedWidth, 2*roundedHeight), 90, -90);
            path.AddLine(Width - 1, Height - 1 - roundedHeight, Width - 1, roundedHeight);
            path.AddArc(new RectangleF(Width - 1 - 2*roundedWidth, 0, 2*roundedWidth, 2*roundedHeight), 0, -90);
            path.AddLine(Width - 1 - roundedWidth, 0, roundedWidth, 0);
            path.AddArc(new RectangleF(0, 0, 2*roundedWidth, 2*roundedHeight), -90, -90);

            e.Graphics.DrawPath(new Pen(new SolidBrush(BorderColor), BorderWidth), path);

            e.Graphics.SmoothingMode = smoothingMode;
            base.OnPaint(e);
        }

        protected override void OnResize(EventArgs e)
        {
            UpdateInternalBounds();
            base.OnResize(e);
        }

        private void lblText_Click(object sender, EventArgs e)
        {
            if (TextClick != null)
            {
                TextClick(this, e);
            }
        }
    }
}

(Stack Overflow的标记和Underscore存在一些问题,但是很容易遵循代码)。


在我看到它无法正常工作的时候,我实际上删除了那个覆盖。我现在再添加它并测试。基本上我想用一些名为IWillNotAutoSizeLabel的新标签替换Label;)

我基本上讨厌自动调整属性“默认情况下启用”。

有帮助吗?

解决方案

在表单的构造函数中设置控件的某些属性时,我看到过类似的行为。他们似乎又恢复了他们的设计时默认值。

我注意到你已经覆盖了OnLoad方法。你试过在那里设置AutoSize = false吗?或者您主要关注提供默认值为false?

其他提示

我花了很多时间用它,这终于有效了! (我的代码是vb.net,但很容易转换它)

Private _Autosize As Boolean 

Public Sub New()
    _Autosize=False
End Sub

Public Overrides Property AutoSize() As Boolean
    Get
        Return MyBase.AutoSize
    End Get

    Set(ByVal Value As Boolean)
        If _Autosize <> Value And _Autosize = False Then
            MyBase.AutoSize = False
            _Autosize = Value
        Else
            MyBase.AutoSize = Value
        End If
    End Set
End Property

您的问题可能是您实际上没有覆盖代码中的自动调整大小(即,与覆盖Font或ForeColor的方式相同)。

我没有在构造函数中看到 this.AutoSize = false 。您的类被标记为部分 - 也许您在该行的另一个文件中有一个构造函数。视觉工作室设计师将调用你那里的无参数构造函数。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top