c#中WinForm的TextBox迴圈自動滾動示例
阿新 • • 發佈:2019-02-04
{
public Form1()
{
InitializeComponent();
this .textBox1.Clear();
for ( int i = 0 ; i <= 20 ;i ++ )
{
this .textBox1.Text += string .Format( " {0}:jinjazz__{1} " , i,i);
}
this
.timer1.Interval
=
200
;
this .timer1.Start();
}
// 傳送訊息
[DllImport( " user32.dll " , EntryPoint = " SendMessage " )]
public static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
// 獲取滾動條位置
[DllImport(
"
user32
"
)]
public static extern int GetScrollPos(IntPtr hwnd, int nBar);
// 設定滾動條位置
[DllImport( " user32.dll " )]
static extern int SetScrollPos(IntPtr hWnd, int nBar,
int nPos, bool bRedraw);
public
const
int
EM_LINESCROLL
=
0xb6
;
private void timer1_Tick( object sender, EventArgs e)
{
int i = GetScrollPos( this .textBox1.Handle, 1 );
// 向下滾動一行
SendMessage( this .textBox1.Handle, EM_LINESCROLL, 0 , 1 ); // 0,1代表垂直滾動條向下滾動
// 判斷是否有位置變化,如果沒有則說明到了底部,返回開始處
if (i == GetScrollPos( this .textBox1.Handle, 1 ))
{
// 回到頂部,這裡用SetScrollPos似乎有問題,滾動條和文字不是同步更新
this .textBox1.SelectionStart = 0 ;
this .textBox1.SelectionLength = 1 ;
this .textBox1.ScrollToCaret();
this .textBox1.SelectionLength = 0 ;
}
Console.WriteLine(i);
}
private void textBox1_MouseEnter( object sender, EventArgs e)
{
this .timer1.Stop();
}
private void textBox1_MouseLeave( object sender, EventArgs e)
{
this .timer1.Start();
}
}
public Form1()
{
InitializeComponent();
this .textBox1.Clear();
for ( int i = 0 ; i <= 20 ;i ++ )
{
this .textBox1.Text += string .Format( " {0}:jinjazz__{1} " , i,i);
}
this .timer1.Start();
}
// 傳送訊息
[DllImport( " user32.dll " , EntryPoint = " SendMessage " )]
public static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
// 獲取滾動條位置
[DllImport(
public static extern int GetScrollPos(IntPtr hwnd, int nBar);
// 設定滾動條位置
[DllImport( " user32.dll " )]
static extern int SetScrollPos(IntPtr hWnd, int nBar,
int nPos, bool bRedraw);
public
private void timer1_Tick( object sender, EventArgs e)
{
int i = GetScrollPos( this .textBox1.Handle, 1 );
// 向下滾動一行
SendMessage( this .textBox1.Handle, EM_LINESCROLL, 0 , 1 ); // 0,1代表垂直滾動條向下滾動
// 判斷是否有位置變化,如果沒有則說明到了底部,返回開始處
if (i == GetScrollPos( this .textBox1.Handle, 1 ))
{
// 回到頂部,這裡用SetScrollPos似乎有問題,滾動條和文字不是同步更新
this .textBox1.SelectionStart = 0 ;
this .textBox1.SelectionLength = 1 ;
this .textBox1.ScrollToCaret();
this .textBox1.SelectionLength = 0 ;
}
Console.WriteLine(i);
}
private void textBox1_MouseEnter( object sender, EventArgs e)
{
this .timer1.Stop();
}
private void textBox1_MouseLeave( object sender, EventArgs e)
{
this .timer1.Start();
}
}