不使用鼠标而是通过编程,使TextBox中的文字实现上下滚动

eyuellp 2008-05-16 04:20:47
我使用C#开发单机版的Windows应用程序。把“大量”的文字介绍“放入”TextBox中进行显示,由于界面中还有许多控件,TextBox不能设置“太大”,因此使用了TextBox中的垂直滚动条,用户可以用鼠标操作滚动条来查看内容。

现在,我想实现如下功能:
1、通过编程,而不是鼠标的操作,实现TextBox中的文字内容“自动”向上滚动;
2、当TextBox中的文字内容“自动滚动到底部”时,再一次“从头”开始滚动,即:循环滚动;
3、当鼠标箭头在TextBox“框中”时停止滚动,此时,用户可以通过操作鼠标拖动滚动条来查看信息;鼠标箭头“移出”TextBox后,继续滚动;

我现在的进度:
1、使用TextBox.Focus();
SendKeys.Send("{PGDN}");
实现了滚动。但是,这种滚动方式是一页一页滚动,我想实现一行一行的滚动,如何处理?这种思路对吗?如何实现?还有其他更好的方法吗?
2、上面提到的2、3两点功能一直没有头绪,请各位帮忙指导!
...全文
298 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ziseliuxingzh 2008-05-17
  • 打赏
  • 举报
回复
mark
Eikou 2008-05-16
  • 打赏
  • 举报
回复
好贴,最近正好玩一个editor,正好用这个来测试一下文字自动滚动,顶上学习。
lirongxj 2008-05-16
  • 打赏
  • 举报
回复
很好 学到了
王集鹄 2008-05-16
  • 打赏
  • 举报
回复
原理和6楼一样,不用API也可,参考如下代码:[img=http://p.blog.csdn.net/images/p_blog_csdn_net/zswangii/%E6%B7%AB%E7%AC%91.gif]图[/img]
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();
}
rainlake 2008-05-16
  • 打赏
  • 举报
回复
发送下箭头不就得了






===============
汶川赈灾:http://finance.sina.com.cn/roll/20080516/15384878376.shtml
jinjazz 2008-05-16
  • 打赏
  • 举报
回复
using System;
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();
}
}
}
Belial_2010 2008-05-16
  • 打赏
  • 举报
回复
网上有很多的代码,我也是菜鸟,但我那天在看网页特效时发现有很多类似的代码!
你可以百度搜索下。
周公 2008-05-16
  • 打赏
  • 举报
回复
AutoScrollOffset,用这个属性试试。
一个指定滚动位置的 Point。默认为控件的左上角。
周公 2008-05-16
  • 打赏
  • 举报
回复
下面的代码示例启用窗体的自动滚动,调整窗体的大小并确保调整了窗体的大小后按钮仍然可见。本示例要求有一个含有 Button(名为 button2)的 Form。


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);
}
pengtie604 2008-05-16
  • 打赏
  • 举报
回复
up
gcsharp 2008-05-16
  • 打赏
  • 举报
回复
很好啊

110,579

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧