连问几篇,都没有人回答。请高手帮忙。(思归,班门斧)请进

zhehui 2003-07-30 11:45:02
请问二个richTextBox的竖直滚动条如何同步的滚动。
请写下一段代码
...全文
47 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
qddmha 2003-08-22
  • 打赏
  • 举报
回复
这样的问题解决思路不难想到
chenxy2002 2003-08-14
  • 打赏
  • 举报
回复
MARK!
snowflake310 2003-08-12
  • 打赏
  • 举报
回复
UP
TheAres 2003-08-12
  • 打赏
  • 举报
回复
还有什么问题吗?
zhehui 2003-08-07
  • 打赏
  • 举报
回复
果然是问题多多,还得继续研究。
zhehui 2003-08-04
  • 打赏
  • 举报
回复
看来要达到到班门斧大哥的水平,看来还得需要二年的努力。
wangzs79 2003-08-04
  • 打赏
  • 举报
回复
班门斧大哥的程序中

1:RichTextBox2 和 RichTextBox1保持同步,但是 RichTextBox1 不能和 RichTextBox2 同步
2:如果使用键盘的上下键或者鼠标在 RichTextBox1 里移动(或者选择),RichTextBox2不做任何滚动
sunking 2003-08-04
  • 打赏
  • 举报
回复
呵呵,建议多学API,没有难事能难倒
TheAres 2003-08-03
  • 打赏
  • 举报
回复
来报个到,回去研究一下在回帖。
shixueli 2003-08-03
  • 打赏
  • 举报
回复
richTextBox没用过,帮你up
TheAres 2003-08-03
  • 打赏
  • 举报
回复
RichTextBox中并没有提供对滚动事件的支持,所以后面的那个MyRichTextBox是继承了一个RichTextBox,在WndProc中截获WM_VSCROLL(垂直滚动)消息,然后调用GetScrollPos来得到滚动条的位置,通过VScroll事件发送出去。

然后在VScroll事件中,通过SendMessage发送EM_SETSCROLLPOS消息,告诉另一个相应滚动。

zhehui 2003-08-03
  • 打赏
  • 举报
回复
请将你的程序原理说明一下。效果运行得还不错。
zhehui 2003-08-03
  • 打赏
  • 举报
回复
有很多的API函数我还是有点不好理解。
zhehui 2003-08-03
  • 打赏
  • 举报
回复
高手出动,果然不一样。看来我也要加入这一行列。
jianglinchun 2003-08-03
  • 打赏
  • 举报
回复
嗬嗬,班门斧果然是班门斧哦。
TheAres 2003-08-03
  • 打赏
  • 举报
回复
完整代码如下,注释比较少,没有写清楚的地方可以在这里发贴问。


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
/// <summary>
/// Form7 摘要说明。
/// </summary>
public class Form7 : System.Windows.Forms.Form
{
private MyRichTextBox richTextBox1;
private RichTextBox richTextBox2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;


public Form7()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.richTextBox1 = new WindowsApplication1.MyRichTextBox();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(96, 48);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(144, 232);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "richTextBox1";
this.richTextBox1.VScroll += new WindowsApplication1.MyRichTextBox.ScrollEventHandler(this.richTextBox1_VScroll);
//
// richTextBox2
//
this.richTextBox2.Location = new System.Drawing.Point(312, 48);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(144, 232);
this.richTextBox2.TabIndex = 1;
this.richTextBox2.Text = "richTextBox2";
//
// Form7
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(624, 342);
this.Controls.Add(this.richTextBox2);
this.Controls.Add(this.richTextBox1);
this.Name = "Form7";
this.Text = "Form7";
this.Load += new System.EventHandler(this.Form7_Load);
this.ResumeLayout(false);

}
#endregion

private void Form7_Load(object sender, System.EventArgs e)
{
this.richTextBox1.Clear();
this.richTextBox2.Clear();
for( int i = 0; i < 100; i++ )
{
this.richTextBox1.AppendText( i.ToString() + Environment.NewLine );
this.richTextBox2.AppendText( i.ToString() + Environment.NewLine );
}
}

[DllImport("user32", CharSet=CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, POINT lParam);

private const Int32 SB_VERT = 0x00000001;
const int EM_SETSCROLLPOS = 0x0400 + 222;

private void richTextBox1_VScroll(object sender, int position)
{
SendMessage(richTextBox2.Handle, EM_SETSCROLLPOS, 0, new POINT(0, position));

}

public static void Main()
{
Application.Run( new Form7() );
}



}

[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;

public POINT()
{
}

public POINT(int x, int y)
{
this.x = x;
this.y = y;
}
}


public class MyRichTextBox: RichTextBox
{
[DllImport("user32")]
private static extern int GetScrollPos(IntPtr hWnd, Int32 nBar);

private const Int32 WM_VSCROLL = 0x00000115;
private const Int32 SB_VERT = 0x00000001;

public delegate void ScrollEventHandler(object sender, Int32 position);

public event ScrollEventHandler VScroll;


protected override void WndProc(ref Message m)
{
if (m.Msg == WM_VSCROLL)
{
int position = GetScrollPos(m.HWnd, SB_VERT);
VScroll(this, position);
}

base.WndProc(ref m);
}
}




}
zhehui 2003-08-01
  • 打赏
  • 举报
回复
我的问题就是,一个richTextBox1的滚动条在滚动时,另一个richTextbox2的滚动条也在同步滚动。二个richTextBox的高度一样。
看来高手都是深藏不露。
维她奶 2003-07-31
  • 打赏
  • 举报
回复
en......这样功能的程序没有做过,最近也比较忙经常要该bug-_-没时间写个程序来测试一下,还是帮楼主友情up!!!!
einsteincao 2003-07-31
  • 打赏
  • 举报
回复
very easy
zhehui 2003-07-31
  • 打赏
  • 举报
回复
我的问题就是,一个richTextBox1的滚动条在滚动时,另一个richTextbox2的滚动条也在同步滚动。二个richTextBox的高度一样。
加载更多回复(3)

110,567

社区成员

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

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

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