實現RichTextBox內容自動滾動(WPF)

來源:互聯網
上載者:User

要實現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();
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.