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

Scroll text in a MaterialMultiLineTextBox2 #330

Merged
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
97 changes: 70 additions & 27 deletions MaterialSkin/Controls/MaterialMultiLineTextBox2.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

namespace MaterialSkin.Controls
{
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using MaterialSkin.Animations;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using MaterialSkin.Animations;

public class MaterialMultiLineTextBox2 : Control, IMaterialControl
{
Expand All @@ -15,7 +15,7 @@ public class MaterialMultiLineTextBox2 : Control, IMaterialControl
ContextMenuStrip _lastContextMenuStrip = new ContextMenuStrip();

//Properties for managing the material design properties

[Browsable(false)]
public int Depth { get; set; }

Expand All @@ -24,9 +24,9 @@ public class MaterialMultiLineTextBox2 : Control, IMaterialControl

[Browsable(false)]
public MouseState MouseState { get; set; }
//Unused properties

//Unused properties

[Browsable(false)]
public override System.Drawing.Image BackgroundImage { get; set; }

Expand All @@ -47,14 +47,14 @@ public class MaterialMultiLineTextBox2 : Control, IMaterialControl
public override System.Drawing.Color ForeColor { get; set; }

//Material Skin properties


[Category("Material Skin"), DefaultValue(""), Localizable(true)]
public string Hint
{
get { return baseTextBox.Hint; }
set
{
public string Hint
{
get { return baseTextBox.Hint; }
set
{
baseTextBox.Hint = value;
hasHint = !String.IsNullOrEmpty(baseTextBox.Hint);
Invalidate();
Expand All @@ -65,6 +65,13 @@ public string Hint
public bool UseAccent { get; set; }



[Browsable(true)]
[Category("Material Skin"), DefaultValue(true), Description("Defines whether MaterialMultiLineTextBox allows scrolling of text. This property is independent of the ScrollBars property")]
public bool AllowScroll { get; set; }



//TextBox properties

public override ContextMenuStrip ContextMenuStrip
Expand Down Expand Up @@ -110,12 +117,12 @@ public override ContextMenuStrip ContextMenuStrip
public char PasswordChar { get { return baseTextBox.PasswordChar; } set { baseTextBox.PasswordChar = value; } }

[Category("Behavior")]
public bool ShortcutsEnabled
{
get
{ return baseTextBox.ShortcutsEnabled; }
set
{
public bool ShortcutsEnabled
{
get
{ return baseTextBox.ShortcutsEnabled; }
set
{
baseTextBox.ShortcutsEnabled = value;
if (value == false)
{
Expand Down Expand Up @@ -199,8 +206,8 @@ public bool LeaveOnEnterKey
public void Paste() { baseTextBox.Paste(); }


# region Forwarding events to baseTextBox
#region Forwarding events to baseTextBox

public event EventHandler AcceptsTabChanged
{
add
Expand Down Expand Up @@ -321,7 +328,7 @@ public event EventHandler BorderStyleChanged
}
}

#if NETFRAMEWORK
#if NETFRAMEWORK
public new event EventHandler ContextMenuChanged
{
add
Expand All @@ -333,7 +340,7 @@ public event EventHandler BorderStyleChanged
baseTextBox.ContextMenuChanged -= value;
}
}
#endif
#endif

public new event EventHandler ContextMenuStripChanged
{
Expand Down Expand Up @@ -1043,7 +1050,7 @@ public event EventHandler ReadOnlyChanged
}
}

public event EventHandler TextAlignChanged
public event EventHandler TextAlignChanged
{
add
{
Expand Down Expand Up @@ -1117,10 +1124,15 @@ public event EventHandler TextAlignChanged
private const int RIGHT_PADDING = 12;
private int LINE_Y;
private bool hasHint;
private readonly int SB_LINEUP = 0;
private readonly int SB_LINEDOWN = 1;
private readonly uint WM_VSCROLL = 277;
private readonly IntPtr ptrLparam = new IntPtr(0);

protected readonly BaseTextBox baseTextBox;
public MaterialMultiLineTextBox2()
{
AllowScroll = true;
// Material Properties
UseAccent = true;
MouseState = MouseState.OUT;
Expand Down Expand Up @@ -1180,6 +1192,7 @@ public MaterialMultiLineTextBox2()
cms.Opening += ContextMenuStripOnOpening;
cms.OnItemClickStart += ContextMenuStripOnItemClickStart;
ContextMenuStrip = cms;
this.MouseWheel += OnMouseWheel;
}

private void Redraw(object sencer, EventArgs e)
Expand Down Expand Up @@ -1236,6 +1249,36 @@ protected override void OnPaint(PaintEventArgs pevent)
}
}


[DllImport("User32.dll", CharSet = CharSet.Auto, EntryPoint = "SendMessage")]
protected static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

protected void OnMouseWheel(object sender, MouseEventArgs e)
{
if (AllowScroll)
{
if (DesignMode)
return;
//Calculate number of notches mouse wheel moved
int v = e.Delta / 120;
//Down Movement
if (v < 0)
{
var ptrWparam = new IntPtr(SB_LINEDOWN);
SendMessage(baseTextBox.Handle, WM_VSCROLL, ptrWparam, ptrLparam);
}
//Up Movement
else if (v > 0)
{
var ptrWparam = new IntPtr(SB_LINEUP);
SendMessage(baseTextBox.Handle, WM_VSCROLL, ptrWparam, ptrLparam);
}

baseTextBox?.Focus();
base.OnMouseDown(e);
}
}

protected override void OnMouseMove(MouseEventArgs e)
{
if (DesignMode)
Expand Down