Csharp/C#教程:如何计算在文本框的查看区域中索引垂直居中的VerticalOffset?分享


如何计算在文本框的查看区域中索引垂直居中的VerticalOffset?

我正在努力将查找和替换function添加到我正在构建的文本编辑器中,并且我希望能够滚动文本框,以便所选匹配在屏幕上垂直居中。

您可以使用GetRectFromCharacterIndex将字符索引转换为屏幕上的矩形。 这将考虑滚动,因此您需要添加当前的VerticalOffset:

var start = textBox.GetRectFromCharacterIndex(textBox.SelectionStart); var end = textBox.GetRectFromCharacterIndex(textBox.SelectionStart + textBox.SelectionLength); textBox.ScrollToVerticalOffset((start.Top + end.Bottom - textBox.ViewportHeight) / 2 + textBox.VerticalOffset); 

如果您有RichTextBox,则可以使用TextPointer.GetCharacterRect :

上述就是C#学习教程:如何计算在文本框的查看区域中索引垂直居中的VerticalOffset?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 var start = textBox.Selection.Start.GetCharacterRect(LogicalDirection.Forward); var end = textBox.Selection.End.GetCharacterRect(LogicalDirection.Forward); textBox.ScrollToVerticalOffset((start.Top + end.Bottom - textBox.ViewportHeight) / 2 + textBox.VerticalOffset); 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1023182.html

(0)
上一篇 2022年1月7日
下一篇 2022年1月7日

精彩推荐