gpt4 book ai didi

C#.NET Winforms : Is it possible to override Label. 自动调整大小?

转载 作者:太空狗 更新时间:2023-10-30 00:59:31 25 4
gpt4 key购买 nike

我不喜欢 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 的默认值吗?

关于C#.NET Winforms : Is it possible to override Label. 自动调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com