WPF RichTextBox滚动条自动滚动实例、文本自动滚动实例
说明:
1.后台代码添加测试 数据
2.使用 richTextBox.ScrollToVerticalOffset()方法,滚动竖直方向滚动条位置
3.使用定时器DispatcherTimer,修改页面显示数据
4.自己计算处理,已经滚动的高度位置
Xaml代码:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="205*"/>
<ColumnDefinition Width="87*"/>
</Grid.ColumnDefinitions>
<Button x:Name="button" Content="开始播放"
HorizontalAlignment="Left" Margin="2,36,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1" Height="29" Click="button_Click"/>
<RichTextBox x:Name="richTextBox"
HorizontalAlignment="Left" Height="209" Margin="10,36,0,0" VerticalAlignment="Top" Width="170">
</RichTextBox>
</Grid>
后台添加测试数据代码:
public text4()
{
InitializeComponent(); richTextBox.Document = doc;
richTextBox.FontSize = ;
//添加内容
appendLine(null, "从你的全世界路过");
appendLine("one", "海上生明月");
appendLine(null, "从你的全世界路过");
appendLine(null, "天涯共此时");
appendLine("two", "张三丰");
appendLine(null, "从你的全世界路过");
appendLine(null, "鲁迅先生");
appendLine(null,null);
}
FlowDocument doc = new FlowDocument();
private void appendLine(string name, string line)
{
Paragraph p = new Paragraph();
if (string.IsNullOrEmpty(name) == false)
doc.RegisterName(name, p);
Run r = new Run(line);
p.TextAlignment = TextAlignment.Center;
p.Inlines.Add(r);
doc.Blocks.Add(p);
}
定时器显示控制代码:
int pIndex = ;
double curTop = ;
private void button_Click(object sender, RoutedEventArgs e)
{
//定时控制内容显示和滚动条位置
DispatcherTimer _timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds();
_timer.Tick += (st, et) =>
{
//获取指定行的内容
BlockCollection col = richTextBox.Document.Blocks;
int index = ;
TextElement prev = null;
foreach (TextElement item in col)
{
//修改当前行的样式
if (index == pIndex)
{
AlterStyle(item, prev);
}
index++;
prev = item;
}
pIndex++;
};
_timer.Start();
}
private void AlterStyle(TextElement item, TextElement prev)
{
//当前行
Paragraph cP = item as Paragraph;
cP.Foreground = Brushes.Red;
TextRange range = new TextRange(cP.ContentStart, cP.ContentEnd);
//滚动位置控制
if (pIndex > && range.Text.Length > )
{
//上一行,样式回调
if (prev != null)
{
prev.Foreground = Brushes.Black;
}
curTop += range.Text.Length > ? : ;
curTop += ;
richTextBox.ScrollToVerticalOffset(curTop);
}
}
运行结果:

WPF RichTextBox滚动条自动滚动实例、文本自动滚动实例的更多相关文章
- Android文字跑马灯控件(文本自动滚动控件)
最近在开发一个应用,需要用到文本的跑马灯效果,图省事,在网上找,但老半天都找不到,后来自己写了一个,很简单,代码如下: import android.content.Context; import a ...
- TextSwitcher实现文本自动垂直滚动
实现功能:用TextSwitcher实现文本自动垂直滚动,类似淘宝首页广告条. 实现效果: 注意:由于网上横向滚动的例子比较多,所以这里通过垂直的例子演示. 实现步骤:1.extends TextSw ...
- AutoCompleteTextView自动补全文本框
AutoCompleteTextView的作用是在输入框中输入我们想要输入的信息,就会出现其他与其相关的提示信息 下面是实例代码: MainActivity.java package com.shao ...
- ANTLR和StringTemplate实例:自动生成单元测试类
ANTLR和StringTemplate实例:自动生成单元测试类 1. ANTLR语法 要想自动生成单元测试,首先第一步就是分析被测试类.这里以Java代码为例,用ANTLR对Java代码进行分析.要 ...
- 页面滚动到底部自动 Ajax 获取文章
页面滚动到底部自动 Ajax 获取文章 代码如下 复制代码 var _timer = {};function delay_till_last(id, fn, wait) { if (_time ...
- [转]单例模式——C++实现自动释放单例类的实例
[转]单例模式——C++实现自动释放单例类的实例 http://www.cnblogs.com/wxxweb/archive/2011/04/15/2017088.html http://blog.s ...
- 添加zabbix自动发现(监控多tomcat实例)
说明 何为自动发现?首先我们监控多tomcat实例,如果一个个实例地添加或许可以完成当前需求.但是日后随着实例的增多,再手动一个个去添加就十分不方便了.这时候需要自动发现这个功能,来帮助我们自动添加监 ...
- kubernetes要实现的目标——随机关掉一台机器,看你的服务能否正常;减少的应用实例能否自动迁移并恢复到其他节点;服务能否随着流量进行自动伸缩
Kubernetes 是来自 Google 云平台的开源容器集群管理系统.基于 Docker 构建一个容器的调度服务.该系统可以自动在一个容器集群中选择一个工作容器供使用.其核心概念是 Contain ...
- 在 Linux 实例上自动安装并运行 VNC Server
原文网址:https://help.aliyun.com/knowledge_detail/41181.html?spm=5176.8208715.110.11.4c184ae8mlC7Yy 您可以使 ...
随机推荐
- matlab中元胞数组(cell)转换为矩阵
matlab中元胞数组(cell)转换为矩阵. cell转换为矩阵函数为:cell2mat(c),其中c为待转换的元胞数组: 转化之后的矩阵可能不满足我们对矩阵维数的要求,那么也许还需要下面两个函数: ...
- Book of Evil
Codeforces Round #196 (Div. 2) D:http://codeforces.com/contest/337/status/D 题意:给你一个树,然后树中有一m个点,求到这m个 ...
- Greg and Array
Codeforces Round #179 (Div. 2) C:http://codeforces.com/problemset/problem/296/C 题意:给你一个序列,然后有两种操作,第一 ...
- 【POJ11855】 Buzzwords (后缀数组)
Description The word “the” is the most commonthree-letter word. It evenshows up inside other words, ...
- IN改写关联注意事项!
SQL> select * from a1; ID NAME ---------- ---------- 1 a 2 a SQL> select * from a2; ID NAME -- ...
- 改进的sqlhelper学习日志
下面就是详细的sqlhelper的代码了 using System; using System.Collections.Generic; using System.Linq; using System ...
- Robot Framework安装
Robot Framework(中文站/社交化知识社区,源码)是一款Python编写的通用开源功能测试自动化框架,以作验收测试和验收测试驱动开发(ATDD),它是一种使用表格测试数据语法的关键字驱动的 ...
- Linux cat命令详解
本文主要内容源自网络,参考资料如下: 华夏名网,linux cat命令详解,http://www.sudu.cn/info/html/edu/20070101/290711.html 命令格式:cat ...
- 装饰模式,制作一个蛋糕java
import java.text.DecimalFormat; //抽象组件组件 interface mkcake { public void cake(); } class Cake impleme ...
- this用法
this是js的一个关键字,随着函数使用场合不同,this的值会发生变化.但是总有一个原则,那就是this指的是调用函数的那个对象. 1.纯粹函数调用. function test() { this. ...