English 中文(简体)
Winforms RichTextBox: How can I determine how many lines of text are visible?
原标题:

I have a Winforms app containing a RichTextBox.

How can I determine how many lines of text are displayed, currently visible?

Reason: I want to scroll the caret to the middle of the RichTextBox. I can use RichTextBox.ScrollToCaret(), but that puts the caret at the top of the RichTextBox. I figure, If I know how many lines are displayed, I could move the caret "back" n/2 lines, then call ScrollToCaret(), then restore the original caret position.

EDIT:

I found EM_GETLINECOUNT, which I thought was the answer, except the doc says: The EM_GETLINECOUNT message retrieves the total number of text lines, not just the number of lines that are currently visible.

Tantalizingly, there is also EM_GETFIRSTVISIBLELINE, which gets the first visible line, but I couldn t find a GETLASTVISIBLELINE. ??


Related:
How can I scroll the caret to the middle of the RichTextBox?

最佳回答

Well, this isn t pretty, but it works for me. Basically I m checking a point just inside the upper-left corner of the richtextbox and a point just inside the lower left corner of the textbox. You may have to adjust the point coordinates depending on how your richtextbox is displayed. Then I get the character index that is closest to each of those two points, and retrieve the line that it is on.

Dim topIndex As Integer = RichTextBox1.GetCharIndexFromPosition(New Point(1, 1))
Dim bottomIndex As Integer = RichTextBox1.GetCharIndexFromPosition(New Point(1, RichTextBox1.Height - 1))

Dim topLine As Integer = RichTextBox1.GetLineFromCharIndex(topIndex)
Dim bottomLine As Integer = RichTextBox1.GetLineFromCharIndex(bottomIndex)

Dim numLinesDisplayed As Integer = bottomLine - topLine

I tested it for richtextboxes with multiple sizes of fonts displayed, and it seems to work. I suspect that the answer that is returned will be off (too small) by one line if the last line of displayed text has a lot of white space under it and the next line is almost ready to be displayed. If you have a very tall richtextbox, with many lines, this shouldn t be a problem.

问题回答

To display the number of lines that a RichTextBox is capable of displaying, even if there is no text there currently, try:

Dim s As SizeF = TextRenderer.MeasureText("A", rtb.Font, rtb.Size, TextFormatFlags.WordBreak)
Dim letterHeight As Integer = CInt(s.Height)
Dim displayableLines As Integer = rtb.Height / letterHeight

This uses the height of a test letter A in the current font to find the line height in pixels.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签