我使用C#开发单机版的Windows应用程序。把“大量”的文字介绍“放入”TextBox中进行显示,由于界面中还有许多控件,TextBox不能设置“太大”,因此使用了TextBox中的垂直滚动条,用户可以用鼠标操作滚动条来查看内容。现在,我想实现如下功能:
1、通过编程,而不是鼠标的操作,实现TextBox中的文字内容“自动”向上滚动;
2、当TextBox中的文字内容“自动滚动到底部”时,再一次“从头”开始滚动,即:循环滚动;
3、当鼠标箭头在TextBox“框中”时停止滚动,此时,用户可以通过操作鼠标拖动滚动条来查看信息;鼠标箭头“移出”TextBox后,继续滚动;我现在的进度:
1、使用TextBox.Focus();
SendKeys.Send("{PGDN}");
实现了滚动。但是,这种滚动方式是一页一页滚动,我想实现一行一行的滚动,如何处理?这种思路对吗?如何实现?还有其他更好的方法吗?
2、上面提到的2、3两点功能一直没有头绪,请各位帮忙指导!
1、通过编程,而不是鼠标的操作,实现TextBox中的文字内容“自动”向上滚动;
2、当TextBox中的文字内容“自动滚动到底部”时,再一次“从头”开始滚动,即:循环滚动;
3、当鼠标箭头在TextBox“框中”时停止滚动,此时,用户可以通过操作鼠标拖动滚动条来查看信息;鼠标箭头“移出”TextBox后,继续滚动;我现在的进度:
1、使用TextBox.Focus();
SendKeys.Send("{PGDN}");
实现了滚动。但是,这种滚动方式是一页一页滚动,我想实现一行一行的滚动,如何处理?这种思路对吗?如何实现?还有其他更好的方法吗?
2、上面提到的2、3两点功能一直没有头绪,请各位帮忙指导!
private void ResizeForm()
{
// Enable auto-scrolling for the form.
this.AutoScroll = true; // Resize the form.
Rectangle r = this.ClientRectangle;
// Subtract 100 pixels from each side of the Rectangle.
r.Inflate(-100, -100);
this.Bounds = this.RectangleToScreen(r); // Make sure button2 is visible.
this.ScrollControlIntoView(button2);
}
一个指定滚动位置的 Point。默认为控件的左上角。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication27
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[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 button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
} private void timer1_Tick(object sender, EventArgs e)
{
int i= GetScrollPos(this.textBox1.Handle,1);
SendMessage(this.textBox1.Handle, EM_LINESCROLL, 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();
}
}
}
===============
汶川赈灾:http://finance.sina.com.cn/roll/20080516/15384878376.shtml
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 20; i++)
textBox1.AppendText(string.Format("Zswang 路过:{0}\r\n", i));
timer1.Interval = 1000;
timer1.Enabled = true; Graphics g = textBox1.CreateGraphics();
startIndex = (int)(textBox1.ClientSize.Height / g.MeasureString("|", textBox1.Font).Height);
g.Dispose();
}int index = int.MaxValue;
int startIndex;
private void timer1_Tick(object sender, EventArgs e)
{
if (index >= textBox1.Lines.Length - 1)
{
textBox1.SelectionStart = 0;
textBox1.ScrollToCaret();
index = startIndex;
return;
}
textBox1.SelectionStart = textBox1.GetFirstCharIndexFromLine(++index);
textBox1.ScrollToCaret();
}private void textBox1_MouseEnter(object sender, EventArgs e)
{
timer1.Stop();
}private void textBox1_MouseLeave(object sender, EventArgs e)
{
timer1.Start();
}