Winforms 文本框每页行数
我们在 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以试试这个:
You could try this:
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).