csstome 发表于 2013-1-26 14:19:02

c#中WinForm的TextBox循环自动滚动示例

这个问题来自论坛提问,演示代码如下
<div style="padding: 4px 5.4pt; width: 95%;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Collections.Generic;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.ComponentModel;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Data;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Drawing;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Text;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Windows.Forms;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Runtime.InteropServices;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifnamespaceWindowsApplication27
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif/**////<summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif///演示如何在TextBox中让文字循环滚动:
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif///作者jinjazz
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif///作者blog:http://blog.csdn.net/jinjazz
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif///</summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicpartialclassForm1:Form
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicForm1()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifInitializeComponent();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.textBox1.Clear();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giffor(inti=0;i<=20;i++)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.textBox1.Text+=string.Format("{0}:jinjazz__{1} ",i,i);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.timer1.Interval=200;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.timer1.Start();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//发送消息
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif"user32.dll",EntryPoint="SendMessage")]
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicstaticexternintSendMessage(IntPtrhWnd,intwMsg,intwParam,intlParam);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//获取滚动条位置
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif"user32")]
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicstaticexternintGetScrollPos(IntPtrhwnd,intnBar);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//设置滚动条位置
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif"user32.dll")]
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstaticexternintSetScrollPos(IntPtrhWnd,intnBar,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifintnPos,boolbRedraw);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicconstintEM_LINESCROLL=0xb6;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivatevoidtimer1_Tick(objectsender,EventArgse)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifinti=GetScrollPos(this.textBox1.Handle,1);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//向下滚动一行
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSendMessage(this.textBox1.Handle,EM_LINESCROLL,0,1);//0,1代表垂直滚动条向下滚动
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//判断是否有位置变化,如果没有则说明到了底部,返回开始处
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifif(i==GetScrollPos(this.textBox1.Handle,1))
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//回到顶部,这里用SetScrollPos似乎有问题,滚动条和文字不是同步更新
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.textBox1.SelectionStart=0;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.textBox1.SelectionLength=1;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.textBox1.ScrollToCaret();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.textBox1.SelectionLength=0;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifConsole.WriteLine(i);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivatevoidtextBox1_MouseEnter(objectsender,EventArgse)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.timer1.Stop();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivatevoidtextBox1_MouseLeave(objectsender,EventArgse)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.timer1.Start();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
页: [1]
查看完整版本: c#中WinForm的TextBox循环自动滚动示例