Winforms 文本框每页行数

发布于 2024-07-29 20:38:04 字数 447 浏览 1 评论 0原文

我们在 .NET 2.0 中有许多多行、可滚动的 Winforms 文本框控件,它们需要实现分页,以便允许用户一次仅通过移动一个页面(一个完整控件的文本)来导航其内容。

作为其中的一部分,我需要获取屏幕上随时可见的完整文本行数,以计算出文本框包含多少页。

我相当确定没有 Windows 消息可以让我直接获取此信息,我也无法为此问题找到可接受的解决方案。

以下是我们当前实施的解决方案,这往往会偶尔产生至少一行的舍入误差,因此不太成功。

public int LinesPerPage
{
    get 
    {
        return (int)(this.Height / this.Font.Height); 
    }
}

理想情况下,我想知道将文本绘制到控件中所使用的算法,但如果这不可用,并且人们提出的其他建议将不胜感激。

We have a number of multiline, scrollable Winforms textbox controls in .NET 2.0 that need to implement pagination in order to allow the user to navigate their contents only by moving a single page (a full control's worth of text) at a time.

As part of this I need to get the number of full lines of text that can be visible on screen at any point to work out how many pages the textbox contains.

I'm fairly sure there isn't a Windows message that will allow me to get this information directly, nor have I been able to work out an acceptable solution to this issue.

Below is the current solution we have implemented, this tends to occasionally give a rounding error of at least one line so it has been less than successful.

public int LinesPerPage
{
    get 
    {
        return (int)(this.Height / this.Font.Height); 
    }
}

Ideally I would like to know the algorithm used in drawing text to the control but if this isn't available and other suggestions people have would be greatly appreciated.

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

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

发布评论

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

评论(2

虐人心 2024-08-05 20:38:05

你可以试试这个:

public int LinesPerPage    
{     
   get   
   {      
      return (int)(this.ClientSize.Height / this.Font.Height);
   }
}

You could try this:

public int LinesPerPage    
{     
   get   
   {      
      return (int)(this.ClientSize.Height / this.Font.Height);
   }
}
心安伴我暖 2024-08-05 20:38:05

System.Drawing.Graphics 类有一个名为 MeasureString 的方法。 该方法的重载之一返回拟合的线(根据传入的其他参数可见的线)。

The System.Drawing.Graphics class has a method called MeasureString. One of the overloads to the method returns the lines fitted (what would be visible based on the other parameters passed in).

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