C# 标签 AutoSize 添加填充

发布于 2024-09-16 17:45:02 字数 155 浏览 1 评论 0原文

我在 Windows.Form 上有一个标签。我将标签上的 AutoSize 属性设置为 True,我注意到当我这样做时,它会用 ~5px 的白色背景填充右侧。我将 Padding 属性设置为 [0, 0, 0, 0]。有办法摆脱这个吗?

我希望标签的边界尽可能接近标签内的文本。

I have a Label on a Windows.Form. I set the AutoSize property on the label to True and I noticed that when I do that, it pads the right hand side with ~5px of white background. I have the Padding property set to [0, 0, 0, 0]. Is there a way to get rid of this?

I would like to get the bounds of the label as close as possible to the text within the label.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味拾荒者 2024-09-23 17:45:02

当您只使用内边距和边距时,这是没有办法的。这是默认行为。

alt text

在上面的 Window 中,我设置了 Padding并将保证金设置为[0,0,0,0]。那 5 个像素仍然存在。

如果您设置 FlatStyle = SystemAutoSize = False 您可以得到:

alt text

在上面的 Window 中,您不再有这 5 个像素。

There's no way when you use only padding and margin. That's the default behavior.

alt text

In the above Window I've set the Padding and Margin to [0,0,0,0]. Those 5 pixels are still there.

If you set FlatStyle = System and AutoSize = False you can get this:

alt text

In the above Window you don't have those 5 pixels anymore.

べ映画 2024-09-23 17:45:02

好的,所以FlastStyle = System;自动调整大小=假;然后设置一个属性来计算宽度,如下所示:

public string LabelText
{
    set
    {
        _label.Text = value;
        using (Graphics g = CreateGraphics()) {
            _label.Width = (int)g.MeasureString(_label.Text, _label.Font).Width;
        }
    }

}

Ok, so FlastStyle = System; AutoSize = false; and then set up a property that will calculate the width like this:

public string LabelText
{
    set
    {
        _label.Text = value;
        using (Graphics g = CreateGraphics()) {
            _label.Width = (int)g.MeasureString(_label.Text, _label.Font).Width;
        }
    }

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文