1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WinSynchronizeScrollbar
{
public partial class Form1 : Form
{
// Constants and SendMessage function
const int WM_USER = 0x400;
const int EM_GETSCROLLPOS = WM_USER + 221;
const int EM_SETSCROLLPOS = WM_USER + 222;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd,
int msg, int wParam,ref Point lParam );
// Scrollpositions of rtb1 and rtb2
Point ptScrollPos_RTB1, ptScrollPos_RTB2 ;
// Wether the scroll was made by the user (false) or by the synched scrolling function (true)
bool blnScrolled_RTB1, blnScrolled_RTB2;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Set the default scrollposition of RTB's
SendMessage( this.rtb1.Handle, EM_GETSCROLLPOS, 0, ref ptScrollPos_RTB1);
SendMessage(this.rtb2.Handle, EM_GETSCROLLPOS, 0, ref ptScrollPos_RTB2);
}
private void rtb1_VScroll(object sender, EventArgs e)
{
//Do not continue if the scroll was made by this program and not by the user (to prevent endless loops)
if (blnScrolled_RTB2) return;
// Set a flag so RTB2 knows it is scrolled by RTB1 and not by the user
blnScrolled_RTB1 = true;
int diff;
// Check scrollposition after scrolling
Point pt = new Point() ;
SendMessage(this.rtb1.Handle, EM_GETSCROLLPOS, 0,ref pt);
// Compare Y-value (height) with scrollposition before scrolling
diff = pt.Y - ptScrollPos_RTB1.Y;
// Set RTB1 scrollposition variable to new scrollposition
ptScrollPos_RTB1 = pt;
// Only sync scrolling when checkbox is checked
if (this.chk.Checked )
{
// Get RTB2 scrollposition
Point pt2 =new Point();
SendMessage(this.rtb2.Handle, EM_GETSCROLLPOS, 0, ref pt2);
// Set RTB2 scrollpos Y (height) to old scrollposition + the difference in RTB1 scrollposition
Point newPoint=new Point(pt.X, diff + pt2.Y);
SendMessage(this.rtb2.Handle, EM_SETSCROLLPOS, 0,ref newPoint);
}
// Re-Set the flag
blnScrolled_RTB1 = false;
}
private void rtb2_VScroll(object sender, EventArgs e)
{
if (blnScrolled_RTB1) return;
blnScrolled_RTB2 = true;
int diff;
// Check
Point pt =new Point();
SendMessage(this.rtb2.Handle, EM_GETSCROLLPOS, 0, ref pt);
diff = pt.Y - ptScrollPos_RTB2.Y;
ptScrollPos_RTB2 = pt;
if (this.chk.Checked)
{
Point pt2 = new Point();
SendMessage(this.rtb1.Handle, EM_GETSCROLLPOS, 0, ref pt2);
Point newPoint = new Point(pt.X, diff + pt2.Y);
SendMessage(this.rtb1.Handle, EM_SETSCROLLPOS, 0, ref newPoint);
}
blnScrolled_RTB2 = false;
}
}
} |
Partager