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

Fix RichTextBox mouse flickering #13

Closed
wants to merge 2 commits into from
Closed
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
41 changes: 41 additions & 0 deletions AngelLoader/CustomControls/RichTextBoxCustom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,26 @@ private enum ScrollInfoMask
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int index);

[DllImport("user32.dll")]
private static extern IntPtr SetCursor(IntPtr hCursor);

private const int WM_SETCURSOR = 0x20;

private static bool VerticalScrollBarVisible(Control ctl)
{
int style = GetWindowLong(ctl.Handle, -16);
return (style & 0x200000) != 0;
}

private bool CursorOverText(Control control, bool fullArea = false)
{
if (!control.Visible || !control.Enabled) return false;
var rpt = PointToClient(control.PointToScreen(new System.Drawing.Point(0, 0)));
var rcs = fullArea ? control.Size : control.ClientSize;
var ptc = PointToClient(Cursor.Position);
return ptc.X >= rpt.X && ptc.X < rpt.X + rcs.Width && ptc.Y >= rpt.Y && ptc.Y < rpt.Y + rcs.Height;
}

private static void BetterScroll(IntPtr handle, int pixels)
{
var si = GetCurrentScrollInfo(handle);
Expand Down Expand Up @@ -375,13 +389,40 @@ private void InterceptMousewheel(ref Message m)
}
}

// Intercept mouse cursor in order to keep it from flickering
private void InterceptCursor(ref Message m)
{
if (!CursorOverText(this, false))
{
if (Cursor != Cursors.Arrow)
{
Cursor = Cursors.Arrow;
}
}
else
{
if ((Cursor != Cursors.IBeam) && (Cursor != Cursors.Hand))
{
Cursor = Cursors.IBeam;
}
}
SetCursor(Cursor.Handle);
}

protected override void WndProc(ref Message m)
{
switch ((uint)m.Msg)
{
case InteropMisc.WM_MOUSEWHEEL:
InterceptMousewheel(ref m);
break;
case InteropMisc.WM_MBUTTONDOWN:
break;
case InteropMisc.WM_MBUTTONDBLCLK:
break;
case WM_SETCURSOR:
InterceptCursor(ref m);
break;
default:
base.WndProc(ref m);
break;
Expand Down