c# 2个RichTextBox滚动条位置同步
我的程序中点击RTB1可同步滚动RTB2,使用鼠标滚轮翻页键方向键都可即时同步,就是点击RTB1的滚动条不能使RTB2即时同步,一定要等鼠标松开滚动条才能同步,如何解决这...
我的程序中点击RTB1可同步滚动RTB2,使用鼠标滚轮 翻页键 方向键都可即时同步,就是点击RTB1的滚动条不能使RTB2即时同步,一定要等鼠标松开滚动条才能同步,如何解决这个问题,有人说用定时器 我试了下不行 发现点击滚动条没松开之前好像程序就没法同时做别的事 怎么能解决这个问题?
看你这么辛苦 分就给你了 展开
看你这么辛苦 分就给你了 展开
1个回答
2015-03-11 · 知道合伙人软件行家
关注
展开全部
你可以重写RichTextBox截获它的滚动条消息和鼠标消息发送给另一个RichTextBox,如下
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace SolveQuestion
{
class RichTextBoxEx : RichTextBox
{
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
public RichTextBoxEx()
{
}
public const int WM_HSCROLL = 276;
public const int WM_VSCROLL = 277;
public const int WM_SETCURSOR = 32;
public const int WM_MOUSEWHEEL = 522;
public const int WM_MOUSEMOVE = 512;
public const int WM_MOUSELEAVE = 675;
public const int WM_MOUSELAST = 521;
public const int WM_MOUSEHOVER = 673;
public const int WM_MOUSEFIRST = 512;
public const int WM_MOUSEACTIVATE = 33;
private RichTextBox otherRichTextBox;
public RichTextBox OtherRichTextBox
{
get { return otherRichTextBox; }
set { otherRichTextBox = value; }
}
protected override void WndProc(ref Message m)
{
if ((otherRichTextBox != null) &&
(m.Msg == WM_HSCROLL ||
m.Msg == WM_VSCROLL ||
m.Msg == WM_SETCURSOR ||
m.Msg == WM_MOUSEWHEEL ||
m.Msg == WM_MOUSEMOVE ||
m.Msg == WM_MOUSELEAVE ||
m.Msg == WM_MOUSELAST ||
m.Msg == WM_MOUSEHOVER ||
m.Msg == WM_MOUSEFIRST ||
m.Msg == WM_MOUSEACTIVATE))
{
SendMessage(otherRichTextBox.Handle, m.Msg, m.WParam, m.LParam);
}
base.WndProc(ref m);
}
}
}
调用方法
private void Form1_Load(object sender, EventArgs e)
{
richTextBoxEx1.OtherRichTextBox = richTextBoxEx2;
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace SolveQuestion
{
class RichTextBoxEx : RichTextBox
{
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
public RichTextBoxEx()
{
}
public const int WM_HSCROLL = 276;
public const int WM_VSCROLL = 277;
public const int WM_SETCURSOR = 32;
public const int WM_MOUSEWHEEL = 522;
public const int WM_MOUSEMOVE = 512;
public const int WM_MOUSELEAVE = 675;
public const int WM_MOUSELAST = 521;
public const int WM_MOUSEHOVER = 673;
public const int WM_MOUSEFIRST = 512;
public const int WM_MOUSEACTIVATE = 33;
private RichTextBox otherRichTextBox;
public RichTextBox OtherRichTextBox
{
get { return otherRichTextBox; }
set { otherRichTextBox = value; }
}
protected override void WndProc(ref Message m)
{
if ((otherRichTextBox != null) &&
(m.Msg == WM_HSCROLL ||
m.Msg == WM_VSCROLL ||
m.Msg == WM_SETCURSOR ||
m.Msg == WM_MOUSEWHEEL ||
m.Msg == WM_MOUSEMOVE ||
m.Msg == WM_MOUSELEAVE ||
m.Msg == WM_MOUSELAST ||
m.Msg == WM_MOUSEHOVER ||
m.Msg == WM_MOUSEFIRST ||
m.Msg == WM_MOUSEACTIVATE))
{
SendMessage(otherRichTextBox.Handle, m.Msg, m.WParam, m.LParam);
}
base.WndProc(ref m);
}
}
}
调用方法
private void Form1_Load(object sender, EventArgs e)
{
richTextBoxEx1.OtherRichTextBox = richTextBoxEx2;
}
追问
这个方法能解决这个问题 我已试过 但也有缺点
1 方向键无用
2鼠标滚轮能滚动 但没对齐功能 文件大点 滚滚就不齐了 会错位
3 这个方法还要改进才有用
4 下面这个比你那个性能更好 如能在这个基础上改好了 功能不少 加分
http://www.360doc.com/content/14/0325/11/6889381_363548435.shtml
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询