Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented horizontal scrolling sync; plus mousewheel scrolling #3

Merged
merged 1 commit into from
Apr 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 53 additions & 10 deletions ScintillaDiff/ScrollSyncScintilla.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ScintillaNET;

Expand All @@ -40,7 +37,7 @@ namespace ScintillaDiff
/// Implements the <see cref="ScintillaNET.Scintilla" />
/// </summary>
/// <seealso cref="ScintillaNET.Scintilla" />
public class ScrollSyncScintilla: Scintilla
public class ScrollSyncScintilla : Scintilla
{
// ReSharper disable once CommentTypo
/// <summary>
Expand All @@ -50,25 +47,71 @@ public class ScrollSyncScintilla: Scintilla
/// https://docs.microsoft.com/en-us/windows/desktop/controls/wm-vscroll
/// </remarks>
/// </summary>
const int WM_VSCROLL = 0x115;
// ReSharper disable once InconsistentNaming
private const int WM_VSCROLL = 0x115;
// ReSharper disable once CommentTypo
/// <summary>
/// The WM_HSCROLL message is sent to a window when a scroll event occurs in the window's standard horizontal scroll bar.
/// This message is also sent to the owner of a horizontal scroll bar control when a scroll event occurs in the control.
/// <remarks>
/// https://docs.microsoft.com/en-us/windows/desktop/controls/wm-hscroll
/// </remarks>
/// </summary>
// ReSharper disable once InconsistentNaming
private const int WM_HSCROLL = 0x114;
/// <summary>
/// The WM_MOUSEWHEEL message is Sent to the focus window when the mouse wheel is rotated.
/// <remarks>
/// https://docs.microsoft.com/en-us/windows/win32/inputdev/wm-mousewheel
/// </remarks>
/// </summary>
// ReSharper disable once InconsistentNaming
private const int WM_MOUSEWHEEL = 0x20a;

/// <summary>
/// The SendMessage Win32 function is used to send a specified message to a window or windows.
/// <remarks>
/// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessage
/// </remarks>
/// </summary>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

/// <summary>
/// Processes Windows messages.
/// </summary>
/// <param name="m">The Windows Message to process.</param>
protected override void WndProc(ref Message m)
{
// ReSharper disable once CommentTypo
// capture the WM_VSCROLL message..
if (m.Msg == WM_MOUSEWHEEL)
{
// two casts to make this work with 32 bit and 64 bit params
var wp = new IntPtr((int)(long)m.WParam < 0 ? 1 : 0);
SendMessage(this.Handle, WM_VSCROLL, wp, new IntPtr(0));
return;
}

base.WndProc(ref m);

// vertical scrolling
if (m.Msg == WM_VSCROLL && ScrollSync != null)
{
// get the first visible line of this control..
int scrollTop = FirstVisibleLine;

// set the value to the another Scintilla control..
// set the value to the other Scintilla control..
ScrollSync.FirstVisibleLine = scrollTop;
}
base.WndProc(ref m);

// horizontal scrolling
if (m.Msg == WM_HSCROLL && ScrollSync != null)
{
// get the leftmost pixel
int scrollLeft = XOffset;

// set the value to the other Scintilla control..
ScrollSync.XOffset = scrollLeft;
}
}

/// <summary>
Expand Down