zoukankan      html  css  js  c++  java
  • 实现RichTextBox内容自动滚动(WPF)

    要实现RichTextBox内容的自滚动,首先要建立一个WPF Application 的工程,这里工程命名为FreeScroll。

    1.应用软件:VS2010

    2. 程序完成的功能:

          2.1  在richtextBox中载入Xaml文件

          2.2  设置滚动速度

          2.3  滚动

          2.4  鼠标双击停止滚动

    3. 程序用到的控件:RichTextBox,Button,Label,TextBox

    4.程序的实现

         4.1 控件布局           

     WPF中布局好的控件图如下:

         

         4.2  实现控件的响应函数

         

    代码
    namespace FreeScroll {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
    public MainWindow() {
    InitializeComponent();
    textBox1.Text
    ="";
    }

    //载入文件
    private void Load_Click(object sender, RoutedEventArgs e) {
    tmp
    =0;
    if (this.timer !=null) { timer.Tick -= new EventHandler(timer_Tick); }
    OpenFileDialog openDialog
    =new OpenFileDialog ();
    if (openDialog.ShowDialog()==true )
    {
    using(FileStream fs=new FileStream (openDialog.FileName,FileMode.Open))
    {
    richTextBox1.Document
    =XamlReader.Load(fs) as FlowDocument;
    richTextBox1.Background
    =richTextBox1.Document.Background;
    }

    }

    }

    //实现滚动
    private void Scroll_Click(object sender, RoutedEventArgs e) {
    if (timer!=null)
    {
    timer.Tick
    -=new EventHandler (timer_Tick);
    }
    int timeInterval;
    if (textBox1.Text != "") {
    timeInterval
    = System.Int32.Parse(textBox1.Text);

    if (timeInterval >= 0) {
    timer.Interval
    = new TimeSpan(0, 0, 0, 0, timeInterval);

    timer.Tick
    += new EventHandler(timer_Tick);
    timer.Start();

    }
    else {
    MessageBox.Show(
    "请输入合适的时间间隔!");
    }
    }
    else {
    MessageBox.Show(
    "请输入合适的时间间隔!");
    }
    }

    void timer_Tick(object sender, EventArgs e) {
    this.richTextBox1.ScrollToVerticalOffset(tmp++);
    }

    //鼠标双击停止滚动
    private void richTextBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
    timer.Tick
    -=new EventHandler(timer_Tick);
    }
    double tmp = 0;
    DispatcherTimer timer
    = new DispatcherTimer();
    }
    }
    
    
  • 相关阅读:
    DataGridView重绘painting简单实例
    C#实现万年历(农历、节气、节日、星座、属相、生肖、闰年等)
    《开源框架那点事儿11》:软件开发杂谈
    半年总结——欲戴王冠,必承其重
    三天学会HTML5 之第一天
    读书笔记 -《高效程序猿的45个习惯-敏捷开发修炼之道》
    Opengl ES 1.x NDK实例开发之七:旋转的纹理立方体
    我与小娜(08):人工智能的伟大胜利
    阿里云 oss 小文件上传进度显示
    模仿猫眼电影App一个动画效果
  • 原文地址:https://www.cnblogs.com/greenteaone/p/1895474.html
Copyright © 2011-2022 走看看