如何检测带有自动省略号的 System.Windows.Forms.Label 是否确实显示省略号?

发布于 2024-09-05 06:10:12 字数 1580 浏览 5 评论 0原文

我有一个 Windows 窗体应用程序,在其中在标签中显示一些客户端数据。 我已设置 label.AutoEllipsis = true。
如果文本比标签长,它看起来像这样:

Some Text
Some longe... // label.Text is actually "Some longer Text"
              // Full text is displayed in a tooltip

这就是我想要的。

但现在我想知道标签是否在运行时使用自动省略功能。 我该如何实现这一目标?

解决方案

感谢最大。现在我能够创建一个控件,尝试将整个文本放在一行中。如果有人感兴趣,这里是代码:

Public Class AutosizeLabel
    Inherits System.Windows.Forms.Label

    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value

            ResetFontToDefault()
            CheckFontsizeToBig()
        End Set
    End Property

    Public Overrides Property Font() As System.Drawing.Font
        Get
            Return MyBase.Font
        End Get
        Set(ByVal value As System.Drawing.Font)
            MyBase.Font = value

            currentFont = value

            CheckFontsizeToBig()
        End Set
    End Property


    Private currentFont As Font = Me.Font
    Private Sub CheckFontsizeToBig()

        If Me.PreferredWidth > Me.Width AndAlso Me.Font.SizeInPoints > 0.25! Then
            MyBase.Font = New Font(currentFont.FontFamily, Me.Font.SizeInPoints - 0.25!, currentFont.Style, currentFont.Unit)
            CheckFontsizeToBig()
        End If

    End Sub

    Private Sub ResetFontToDefault()
        MyBase.Font = currentFont
    End Sub

End Class

可能需要一些微调(使步长和最小值可通过设计器可见的属性进行配置),但目前效果很好。

I have a Windows Forms Application where I display some client data in a Label.
I have set label.AutoEllipsis = true.
If the text is longer than the label, it looks like this:

Some Text
Some longe... // label.Text is actually "Some longer Text"
              // Full text is displayed in a tooltip

which is what I want.

But now I want to know if the label makes use of the AutoEllipsis feature at runtime.
How do I achive that?

Solution

Thanks to max. Now I was able to create a control that try to fit the whole text in one line. If someone is interested, here's the code:

Public Class AutosizeLabel
    Inherits System.Windows.Forms.Label

    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value

            ResetFontToDefault()
            CheckFontsizeToBig()
        End Set
    End Property

    Public Overrides Property Font() As System.Drawing.Font
        Get
            Return MyBase.Font
        End Get
        Set(ByVal value As System.Drawing.Font)
            MyBase.Font = value

            currentFont = value

            CheckFontsizeToBig()
        End Set
    End Property


    Private currentFont As Font = Me.Font
    Private Sub CheckFontsizeToBig()

        If Me.PreferredWidth > Me.Width AndAlso Me.Font.SizeInPoints > 0.25! Then
            MyBase.Font = New Font(currentFont.FontFamily, Me.Font.SizeInPoints - 0.25!, currentFont.Style, currentFont.Unit)
            CheckFontsizeToBig()
        End If

    End Sub

    Private Sub ResetFontToDefault()
        MyBase.Font = currentFont
    End Sub

End Class

Could need some fine tuning (make the step size and the minimum value configurable with designer visible Properties) but it works pretty well for the moment.

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

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

发布评论

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

评论(2

﹏半生如梦愿梦如真 2024-09-12 06:10:30

事实上,你的标签可以是多行的。在这种情况下, label.PreferredWidth 没有帮助。但你可以使用:

    internal static bool IsElipsisShown(this Label @this)
    {
        Size sz = TextRenderer.MeasureText(@this.Text, @this.Font, @this.Size, TextFormatFlags.WordBreak);
        return (sz.Width > @this.Size.Width || sz.Height > @this.Size.Height);
    }

In fact, your Lable could be multiline. In this case, label.PreferredWidth wouldn't help. But you can use:

    internal static bool IsElipsisShown(this Label @this)
    {
        Size sz = TextRenderer.MeasureText(@this.Text, @this.Font, @this.Size, TextFormatFlags.WordBreak);
        return (sz.Width > @this.Size.Width || sz.Height > @this.Size.Height);
    }
坚持沉默 2024-09-12 06:10:24
private static bool IsShowingEllipsis(Label label)
{
    return label.PreferredWidth > label.Width;
}
private static bool IsShowingEllipsis(Label label)
{
    return label.PreferredWidth > label.Width;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文