VB.NET-自动调整字体大小以适合多行文本框 [英] VB.NET - Autosize font to fit multiline textbox

查看:446
本文介绍了VB.NET-自动调整字体大小以适合多行文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候-长期以来一直在寻找答案,但没有运气.

VB.NET 2010-我希望从数据库中获取字符串(没有换行符,但将来可能会出现),并在多行文本框中使用文字换行显示文本,并调整文本的字体大小以确保所有文本被展示.字符串的长度在50到300个字符之间,因此字体大小可能会有很大差异.

可行吗?

谢谢!

Greetings -- Have been looking around for an answer for a long time without luck.

VB.NET 2010 - I am looking to take a string from a database (No newlines, but may in the future) and display the text in a multiline textbox with wordwrapping and have the text''s font size adjust to ensure all the text is displayed. Strings are between 50 to 300 characters in length, so the font size can vary wildly.

Doable?

Thanks!

推荐答案

我从来没有完全按照您的要求做,但是我想指出它不是很漂亮 [
I''ve never done exactly what you are requesting, but I wanted to point out the ZoomFactor[^] property of the RichTextBox. Perhaps that can be used. And I vaguely remember something about being able to determine if scrollbars are needed in a textbox...I had trouble finding it again though...and it''s not pretty[^]. Maybe you can google and find something better. Hope this helps.


知道了! Kschuler,您为我指明了正确的方向!
这是我创建的一个快速草率示例,当单击按钮"时,将调整富文本格式"框以填充整个框.

Got it! Kschuler, you pointed me in the right direction!
Here is a quick sloppy sample I created, when the Button is clicked, the Rich Text Box is adjusted to fill the entire box.

Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer) As Integer
    Public Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Integer) As Integer
    Public Const GWL_STYLE As Integer = (-16)
    Public Const WS_VSCROLL As Integer = &H200000
    Public Const WS_HSCROLL As Integer = &H100000
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Center the text in the RichBox
        RichTextBox1.SelectAll()
        RichTextBox1.SelectionAlignment = HorizontalAlignment.Center
        'Is scrollbar visible?
        Dim bVScrollBar As Boolean
        bVScrollBar = ((GetWindowLong(Me.RichTextBox1.Handle, GWL_STYLE) And WS_VSCROLL) = WS_VSCROLL)
        Select Case bVScrollBar
            Case True
                'Scrollbar is visible - Make it smaller
                Do
                    RichTextBox1.ZoomFactor = RichTextBox1.ZoomFactor - 0.01
                    bVScrollBar = ((GetWindowLong(Me.RichTextBox1.Handle, GWL_STYLE) And WS_VSCROLL) = WS_VSCROLL)
                    'If the scrollbar is no longer visible we are done!
                    If bVScrollBar = False Then Exit Do
                Loop
            Case False
                'Scrollbar is not visible - Make it bigger
                Do
                    RichTextBox1.ZoomFactor = RichTextBox1.ZoomFactor + 0.01
                    bVScrollBar = ((GetWindowLong(Me.RichTextBox1.Handle, GWL_STYLE) And WS_VSCROLL) = WS_VSCROLL)
                    If bVScrollBar = True Then
                        Do
                            'Found the scrollbar, make smaller until bar is not visible
                            RichTextBox1.ZoomFactor = RichTextBox1.ZoomFactor - 0.01
                            bVScrollBar = ((GetWindowLong(Me.RichTextBox1.Handle, GWL_STYLE) And WS_VSCROLL) = WS_VSCROLL)
                            'If the scrollbar is no longer visible we are done!
                            If bVScrollBar = False Then Exit Do
                        Loop
                        Exit Do
                    End If
                Loop
        End Select
    End Sub


没有功能告诉您应该使所有内容适合的字体大小.

我必须做类似的事情,但是不需要编辑框中的文本.我要做的是创建一个自定义控件,该控件显示面板上绘制的文本.但是,为了使文本正确适合而又不不断猜测正确的字体,我在超出渲染区域实际大小的屏幕外缓冲区中绘制了超大尺寸的文本.然后我只是按比例缩小了图片以适合渲染区域的宽度或高度,具体取决于哪个更大.
There is no function to tell you what the font size should be to get everything to fit.

I had to do something similar, but without the requirements of editing the text in the box. What I did was make a custom control that displayed the text painted in a panel. But, in order to get the text to fit properly without constantly guessing at the right font, I painted the text oversized in an offscreen buffer scaled up from teh actual size of my rendering area. Then I just painted the picture scaled down to fit the width or height of the render area, depending on which was greater.


这篇关于VB.NET-自动调整字体大小以适合多行文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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