Commit 098cffbf kwwwvagaa

优化滚动条,支持Panel、treeview、textbox

1 个父辈 859c6bcd
......@@ -48,6 +48,13 @@ namespace HZH_Controls.Controls
return true;
}
}
else if (extendee is RichTextBox)
{
// RichTextBox control = (RichTextBox)extendee;
return true;
}
return false;
}
......@@ -64,29 +71,63 @@ namespace HZH_Controls.Controls
m_controlCache[control] = blnUserCustomScrollbar;
if (!blnUserCustomScrollbar)
return;
control_SizeChanged(control, null);
control.VisibleChanged += control_VisibleChanged;
control.SizeChanged += control_SizeChanged;
control.LocationChanged += control_LocationChanged;
control.Disposed += control_Disposed;
control.MouseWheel += Control_MouseWheel;
if (control is TreeView)
{
TreeView tv = (TreeView)control;
tv.MouseWheel += tv_MouseWheel;
tv.AfterSelect += tv_AfterSelect;
//tv.MouseWheel += tv_MouseWheel;
//tv.AfterSelect += tv_AfterSelect;
tv.AfterExpand += tv_AfterExpand;
tv.AfterCollapse += tv_AfterCollapse;
}
else if (control is TextBox)
{
TextBox txt = (TextBox)control;
txt.MouseWheel += txt_MouseWheel;
//txt.MouseWheel += txt_MouseWheel;
txt.TextChanged += txt_TextChanged;
txt.KeyDown += txt_KeyDown;
}
control_SizeChanged(control, null);
}
private void Control_MouseWheel(object sender, MouseEventArgs e)
{
Control c = (Control)sender;
if (m_lstVCache.ContainsKey(c))
{
if (e.Delta > 5)
{
ControlHelper.ScrollUp(c.Handle);
}
else if (e.Delta < -5)
{
ControlHelper.ScrollDown(c.Handle);
}
SetVMaxNum(c);
}
else if (m_lstHCache.ContainsKey(c))
{
if (e.Delta > 5)
{
ControlHelper.ScrollLeft(c.Handle);
}
else if (e.Delta < -5)
{
ControlHelper.ScrollRight(c.Handle);
}
SetHMaxNum(c);
}
}
void control_Disposed(object sender, EventArgs e)
{
......@@ -115,6 +156,30 @@ namespace HZH_Controls.Controls
bool blnHasVScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & VSCROLL) != 0;
bool blnHasHScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & HSCROLL) != 0;
if (control is TextBox)
{
var txt = (TextBox)control;
if (txt.ScrollBars == ScrollBars.Both)
{
blnHasVScrollbar = true;
blnHasHScrollbar = true;
}
else if (txt.ScrollBars == ScrollBars.Vertical)
{
blnHasVScrollbar = true;
blnHasHScrollbar = false;
}
else if (txt.ScrollBars == ScrollBars.Horizontal)
{
blnHasVScrollbar = false;
blnHasHScrollbar = true;
}
else
{
blnHasVScrollbar = false;
blnHasHScrollbar = false;
}
}
if (blnHasVScrollbar)
{
if (!m_lstVCache.ContainsKey(control))
......@@ -122,20 +187,20 @@ namespace HZH_Controls.Controls
if (control.Parent != null)
{
UCVScrollbar barV = new UCVScrollbar();
barV.Width = SystemInformation.VerticalScrollBarWidth;
barV.SmallChange = 5;
barV.Width = SystemInformation.VerticalScrollBarWidth + 1;
barV.Scroll += barV_Scroll;
m_lstVCache[control] = barV;
if (blnHasHScrollbar)
{
barV.Height = control.Height - barV.Width - 2;
barV.Height = control.Height - barV.Width;
}
else
{
barV.Height = control.Height - 2;
barV.Height = control.Height;
}
SetVMaxNum(control);
barV.Location = new System.Drawing.Point(control.Right - barV.Width - 1, control.Top + 1);
barV.Location = new System.Drawing.Point(control.Right - barV.Width, control.Top);
control.Parent.Controls.Add(barV);
int intControlIndex = control.Parent.Controls.GetChildIndex(control);
control.Parent.Controls.SetChildIndex(barV, intControlIndex);
......@@ -150,8 +215,7 @@ namespace HZH_Controls.Controls
{
if (m_lstVCache.ContainsKey(control) && m_lstVCache[control].Parent != null)
{
m_lstVCache[control].Parent.Controls.Remove(m_lstVCache[control]);
m_lstVCache.Remove(control);
m_lstVCache[control].Visible = false;
}
}
......@@ -162,20 +226,21 @@ namespace HZH_Controls.Controls
if (control.Parent != null)
{
UCHScrollbar barH = new UCHScrollbar();
barH.Height = SystemInformation.HorizontalScrollBarHeight;
barH.Height = SystemInformation.HorizontalScrollBarHeight + 1;
barH.SmallChange = 5;
barH.Scroll += barH_Scroll;
m_lstHCache[control] = barH;
if (blnHasHScrollbar)
{
barH.Width = control.Width - barH.Height - 2;
barH.Width = control.Width - barH.Height;
}
else
{
barH.Width = control.Width - 2;
barH.Width = control.Width;
}
SetHMaxNum(control);
barH.Location = new System.Drawing.Point(control.Left + 1, control.Bottom - barH.Height - 1);
barH.Location = new System.Drawing.Point(control.Left, control.Bottom - barH.Height);
control.Parent.Controls.Add(barH);
int intControlIndex = control.Parent.Controls.GetChildIndex(control);
control.Parent.Controls.SetChildIndex(barH, intControlIndex);
......@@ -192,7 +257,7 @@ namespace HZH_Controls.Controls
{
if (m_lstHCache[control].Visible && m_lstHCache[control].Parent != null)
{
m_lstHCache[control].Parent.Controls.Remove(m_lstHCache[control]);
m_lstHCache[control].Visible = false;
}
}
}
......@@ -200,64 +265,108 @@ namespace HZH_Controls.Controls
ResetScrollLocation(sender);
}
private void SetVMaxNum(Control control)
{
if (!m_lstVCache.ContainsKey(control))
return;
var into = ControlHelper.GetVScrollBarInfo(control.Handle);
var intoH = ControlHelper.GetHScrollBarInfo(control.Handle);
var intoV = ControlHelper.GetVScrollBarInfo(control.Handle);
UCVScrollbar barV = m_lstVCache[control];
if (control is ScrollableControl)
if (control is ScrollableControl )
{
barV.Maximum = (control as ScrollableControl).VerticalScroll.Maximum;
barV.Value = (control as ScrollableControl).VerticalScroll.Value;
barV.Maximum = intoV.ScrollMax;
barV.Visible = intoV.ScrollMax > 0 && intoV.nMax > 0 && intoV.nPage > 0;
barV.Value = intoV.nPos;
barV.BringToFront();
// barV.Maximum = (control as ScrollableControl).VerticalScroll.Maximum;
// barV.Value = (control as ScrollableControl).VerticalScroll.Value;
}
else if (control is TreeView)
{
barV.Maximum = GetTreeNodeMaxY(control as TreeView);
barV.Value = (control as TreeView).AutoScrollOffset.Y;
var tv = control as TreeView;
barV.Maximum = intoV.ScrollMax * tv.ItemHeight;
barV.Visible = intoV.ScrollMax > 0 && intoV.nMax > 0 && intoV.nPage > 0;
barV.Value = intoV.nPos * tv.ItemHeight;
barV.BringToFront();
//barV.Maximum = GetTreeNodeMaxY(control as TreeView) - control.Height;
//barV.Value = (control as TreeView).AutoScrollOffset.Y;
}
else if (control is TextBox)
{
TextBox txt = (TextBox)control;
int intTxtMaxHeight = 0;
int intTextHeight = 0;
using (var g = txt.CreateGraphics())
barV.Maximum = intoV.ScrollMax * txt.PreferredHeight;
if (txt.ScrollBars == ScrollBars.Both || txt.ScrollBars == ScrollBars.Vertical)
{
intTxtMaxHeight = (int)g.MeasureString(txt.Text, txt.Font).Height;
intTextHeight = (int)g.MeasureString(txt.Text.Substring(0, txt.SelectionStart), txt.Font).Height;
barV.Visible = true;
}
barV.Maximum = intTxtMaxHeight;
barV.Value = (control as TextBox).AutoScrollOffset.Y;
else
{
barV.Visible = false;
}
barV.Value = intoV.nPos * txt.PreferredHeight;
barV.BringToFront();
}
else if (control is RichTextBox)
{
bool blnHasVScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & VSCROLL) != 0;
barV.Maximum = intoV.ScrollMax;
barV.Visible = blnHasVScrollbar;
barV.Value = intoV.nPos;
barV.BringToFront();
}
}
private void SetHMaxNum(Control control)
{
if (!m_lstHCache.ContainsKey(control))
return;
var intoH = ControlHelper.GetHScrollBarInfo(control.Handle);
UCHScrollbar barH = m_lstHCache[control];
if (control is ScrollableControl)
{
barH.Maximum = (control as ScrollableControl).HorizontalScroll.Maximum;
barH.Value = (control as ScrollableControl).HorizontalScroll.Value;
barH.Maximum = intoH.ScrollMax;
barH.Visible = intoH.ScrollMax > 0 && intoH.nMax > 0 && intoH.nPage > 0;
barH.Value = intoH.nPos;
barH.BringToFront();
//barH.Maximum = (control as ScrollableControl).HorizontalScroll.Maximum;
//barH.Value = (control as ScrollableControl).HorizontalScroll.Value;
}
else if (control is TreeView)
{
barH.Maximum = GetTreeNodeMaxX(control as TreeView);
barH.Value = (control as TreeView).AutoScrollOffset.X;
var tv = control as TreeView;
barH.Maximum = intoH.ScrollMax;
barH.Visible = intoH.ScrollMax > 0 && intoH.nMax > 0 && intoH.nPage > 0;
barH.Value = intoH.nPos;
barH.BringToFront();
//barH.Maximum = GetTreeNodeMaxX(control as TreeView);
//barH.Value = (control as TreeView).AutoScrollOffset.X;
}
else if (control is TextBox)
else if (control is TextBoxBase)
{
TextBox txt = (TextBox)control;
int intTxtMaxWidth = 0;
int intTextWidth = 0;
using (var g = txt.CreateGraphics())
barH.Maximum = intoH.ScrollMax;
if (txt.ScrollBars == ScrollBars.Both || txt.ScrollBars == ScrollBars.Horizontal)
{
intTxtMaxWidth = (int)g.MeasureString(txt.Text, txt.Font).Width;
intTextWidth = (int)g.MeasureString(txt.Text.Substring(0, txt.SelectionStart), txt.Font).Width;
barH.Visible = true;
}
barH.Maximum = intTxtMaxWidth;
barH.Value = (control as TextBox).AutoScrollOffset.Y;
else
{
barH.Visible = false;
}
barH.Value = intoH.nPos;
barH.BringToFront();
}
else if (control is RichTextBox)
{
bool blnHasHScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & HSCROLL) != 0;
barH.Maximum = intoH.ScrollMax;
barH.Visible = blnHasHScrollbar;
barH.Value = intoH.nPos;
barH.BringToFront();
}
}
/// <summary>
......@@ -269,32 +378,56 @@ namespace HZH_Controls.Controls
Control control = (Control)sender;
bool blnHasVScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & VSCROLL) != 0;
bool blnHasHScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & HSCROLL) != 0;
if (control is TextBox)
{
var txt = (TextBox)control;
if (txt.ScrollBars == ScrollBars.Both)
{
blnHasVScrollbar = true;
blnHasHScrollbar = true;
}
else if (txt.ScrollBars == ScrollBars.Vertical)
{
blnHasVScrollbar = true;
blnHasHScrollbar = false;
}
else if (txt.ScrollBars == ScrollBars.Horizontal)
{
blnHasVScrollbar = false;
blnHasHScrollbar = true;
}
else
{
blnHasVScrollbar = false;
blnHasHScrollbar = false;
}
}
if (control.Visible)
{
if (m_lstVCache.ContainsKey(control))
{
m_lstVCache[control].Location = new System.Drawing.Point(control.Right - m_lstVCache[control].Width - 1, control.Top + 1);
m_lstVCache[control].Location = new System.Drawing.Point(control.Right - m_lstVCache[control].Width, control.Top);
if (blnHasHScrollbar)
{
m_lstVCache[control].Height = control.Height - m_lstVCache[control].Width - 2;
m_lstVCache[control].Height = control.Height - m_lstHCache[control].Height;
}
else
{
m_lstVCache[control].Height = control.Height - 2;
m_lstVCache[control].Height = control.Height;
}
}
if (m_lstHCache.ContainsKey(control))
{
m_lstHCache[control].Location = new System.Drawing.Point(control.Left + 1, control.Bottom - m_lstHCache[control].Height - 1);
if (blnHasHScrollbar)
{
m_lstHCache[control].Width = control.Width - m_lstHCache[control].Height - 2;
}
else
{
m_lstHCache[control].Width = control.Width - 2;
}
m_lstHCache[control].Location = new System.Drawing.Point(control.Left, control.Bottom - m_lstHCache[control].Height);
//if (blnHasVScrollbar)
//{
// m_lstHCache[control].Width = control.Width - m_lstVCache[control].Width;
//}
//else
//{
m_lstHCache[control].Width = control.Width;
//}
}
}
}
......@@ -307,20 +440,17 @@ namespace HZH_Controls.Controls
void control_VisibleChanged(object sender, EventArgs e)
{
Control control = (Control)sender;
if (!control.Visible)
if (m_lstVCache.ContainsKey(control) && m_lstVCache[control].Parent != null)
{
if (m_lstVCache.ContainsKey(control) && m_lstVCache[control].Parent != null)
{
m_lstVCache[control].Parent.Controls.Remove(m_lstVCache[control]);
m_lstVCache.Remove(control);
}
m_lstVCache[control].Visible = control.Visible;
}
if (m_lstHCache.ContainsKey(control) && m_lstHCache[control].Parent != null)
{
m_lstHCache[control].Parent.Controls.Remove(m_lstHCache[control]);
m_lstHCache.Remove(control);
}
if (m_lstHCache.ContainsKey(control) && m_lstHCache[control].Parent != null)
{
m_lstHCache[control].Visible = control.Visible;
}
}
private const int HSCROLL = 0x100000;
......@@ -336,19 +466,23 @@ namespace HZH_Controls.Controls
if (m_lstVCache.ContainsValue(bar))
{
Control c = m_lstVCache.FirstOrDefault(p => p.Value == bar).Key;
//ControlHelper.SetVScrollValue(c.Handle, bar.Value);
if (c is ScrollableControl)
{
(c as ScrollableControl).AutoScrollPosition = new Point((c as ScrollableControl).AutoScrollPosition.X, bar.Value);
}
else if (c is TreeView)
{
TreeView tv = (c as TreeView);
SetTreeViewVScrollLocation(tv, tv.Nodes, bar.Value);
ControlHelper.SetVScrollValue(c.Handle, bar.Value / ((c as TreeView).ItemHeight));
}
else if (c is TextBox)
{
TextBox txt = (c as TextBox);
SetTextBoxVScrollLocation(txt, bar.Value);
ControlHelper.SetVScrollValue(c.Handle, bar.Value / ((c as TextBox).PreferredHeight));
}
else if (c is RichTextBox)
{
ControlHelper.SetVScrollValue(c.Handle, bar.Value );
}
}
}
......@@ -365,14 +499,20 @@ namespace HZH_Controls.Controls
}
else if (c is TreeView)
{
ControlHelper.SetHScrollValue(c.Handle, bar.Value);
//TreeView tv = (c as TreeView);
//SetTreeViewVScrollLocation(tv, tv.Nodes, bar.Value);
}
else if (c is TextBox)
{
ControlHelper.SetHScrollValue(c.Handle, bar.Value);
//TextBox txt = (c as TextBox);
//SetTextBoxVScrollLocation(txt, bar.Value);
}
else if (c is RichTextBox)
{
ControlHelper.SetHScrollValue(c.Handle, bar.Value);
}
}
}
......@@ -386,70 +526,41 @@ namespace HZH_Controls.Controls
{
control_SizeChanged(sender as Control, null);
}
/// <summary>
/// Gets the tree node 最大高度
/// </summary>
/// <param name="tv">The tv.</param>
/// <returns>System.Int32.</returns>
private int GetTreeNodeMaxY(TreeView tv)
{
TreeNode tnLast = tv.Nodes[tv.Nodes.Count - 1];
begin:
if (tnLast.IsExpanded && tnLast.Nodes.Count > 0)
{
tnLast = tnLast.LastNode;
goto begin;
}
return tnLast.Bounds.Bottom;
}
private int GetTreeNodeMaxX(TreeView tv)
{
return tv.Nodes.Count != 0 ? tv.Nodes[0].Bounds.Right : 0;
}
void tv_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeView tv = (TreeView)sender;
if (m_lstVCache.ContainsKey(tv))
{
m_lstVCache[tv].Value = tv.Nodes.Count > 0 ? Math.Abs(tv.Nodes[0].Bounds.Top) : 0;
}
}
void tv_MouseWheel(object sender, MouseEventArgs e)
{
TreeView tv = (TreeView)sender;
if (m_lstVCache.ContainsKey(tv))
{
m_lstVCache[tv].Value = tv.Nodes.Count > 0 ? Math.Abs(tv.Nodes[0].Bounds.Top) : 0;
}
}
/// <summary>
/// Sets the TreeView scroll location.
/// </summary>
/// <param name="tv">The tv.</param>
/// <param name="tns">The TNS.</param>
/// <param name="intY">The int y.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
private bool SetTreeViewVScrollLocation(TreeView tv, TreeNodeCollection tns, int intY)
{
for (int i = 0; i < tns.Count; i++)
{
if (intY >= tns[i].Bounds.Top - tv.Nodes[0].Bounds.Top - 3 && intY <= tns[i].Bounds.Bottom - tv.Nodes[0].Bounds.Top + 3)
{
tns[i].EnsureVisible();
return true;
}
else if (tns[i].IsExpanded && tns[i].Nodes.Count > 0)
{
bool bln = SetTreeViewVScrollLocation(tv, tns[i].Nodes, intY);
if (bln)
return true;
}
}
return false;
}
// void tv_AfterSelect(object sender, TreeViewEventArgs e)
// {
// TreeView tv = (TreeView)sender;
// if (m_lstVCache.ContainsKey(tv))
// {
// m_lstVCache[tv].Value = tv.Nodes.Count > 0 ? Math.Abs(tv.Nodes[0].Bounds.Top) : 0;
// }
// }
///// <summary>
///// Sets the TreeView scroll location.
///// </summary>
///// <param name="tv">The tv.</param>
///// <param name="tns">The TNS.</param>
///// <param name="intY">The int y.</param>
///// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
//private bool SetTreeViewVScrollLocation(TreeView tv, TreeNodeCollection tns, int intY)
//{
// for (int i = 0; i < tns.Count; i++)
// {
// if (intY >= tns[i].Bounds.Top - tv.Nodes[0].Bounds.Top - 3 && intY <= tns[i].Bounds.Bottom - tv.Nodes[0].Bounds.Top + 3)
// {
// tns[i].EnsureVisible();
// return true;
// }
// else if (tns[i].IsExpanded && tns[i].Nodes.Count > 0)
// {
// bool bln = SetTreeViewVScrollLocation(tv, tns[i].Nodes, intY);
// if (bln)
// return true;
// }
// }
// return false;
//}
#endregion
#region TextBox处理 English:TextBox Processing
......@@ -458,80 +569,82 @@ namespace HZH_Controls.Controls
{
TextBox txt = sender as TextBox;
control_SizeChanged(txt, null);
SetVMaxNum(txt);
if (m_lstVCache.ContainsKey(txt))
{
using (var g = txt.CreateGraphics())
{
var size = g.MeasureString(txt.Text.Substring(0, txt.SelectionStart), txt.Font);
m_lstVCache[txt].Value = (int)size.Height;
}
}
}
private void SetTextBoxVScrollLocation(TextBox txt, int intY)
{
using (var g = txt.CreateGraphics())
{
for (int i = 0; i < txt.Lines.Length; i++)
{
string str = string.Join("\n", txt.Lines.Take(i + 1));
var size = g.MeasureString(str, txt.Font);
if (size.Height >= intY)
{
txt.SelectionStart = str.Length;
txt.ScrollToCaret();
return;
}
}
}
//if (m_lstVCache.ContainsKey(txt))
//{
// using (var g = txt.CreateGraphics())
// {
// var size = g.MeasureString(txt.Text.Substring(0, txt.SelectionStart), txt.Font);
// m_lstVCache[txt].Value = (int)size.Height;
// }
//}
}
//private void SetTextBoxVScrollLocation(TextBox txt, int intY)
//{
// using (var g = txt.CreateGraphics())
// {
// for (int i = 0; i < txt.Lines.Length; i++)
// {
// string str = string.Join("\n", txt.Lines.Take(i + 1));
// var size = g.MeasureString(str, txt.Font);
// if (size.Height >= intY)
// {
// txt.SelectionStart = str.Length;
// txt.ScrollToCaret();
// return;
// }
// }
// }
//}
void txt_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
{
TextBox txt = (TextBox)sender;
if (m_lstVCache.ContainsKey(txt))
{
using (var g = txt.CreateGraphics())
{
var size = g.MeasureString(txt.Text.Substring(0, txt.SelectionStart), txt.Font);
m_lstVCache[txt].Value = (int)size.Height;
}
}
}
TextBox txt = sender as TextBox;
control_SizeChanged(txt, null);
//if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
//{
// TextBox txt = (TextBox)sender;
// if (m_lstVCache.ContainsKey(txt))
// {
// using (var g = txt.CreateGraphics())
// {
// var size = g.MeasureString(txt.Text.Substring(0, txt.SelectionStart), txt.Font);
// m_lstVCache[txt].Value = (int)size.Height;
// }
// }
//}
}
void txt_MouseWheel(object sender, MouseEventArgs e)
{
TextBox txt = (TextBox)sender;
if (m_lstVCache.ContainsKey(txt))
{
using (var g = txt.CreateGraphics())
{
StringBuilder str = new StringBuilder();
for (int i = 0; i < System.Windows.Forms.SystemInformation.MouseWheelScrollLines; i++)
{
str.AppendLine("A");
}
var height = (int)g.MeasureString(str.ToString(), txt.Font).Height;
if (e.Delta < 0)
{
if (height + m_lstVCache[txt].Value > m_lstVCache[txt].Maximum)
m_lstVCache[txt].Value = m_lstVCache[txt].Maximum;
else
m_lstVCache[txt].Value += height;
}
else
{
if (m_lstVCache[txt].Value - height < 0)
m_lstVCache[txt].Value = 0;
else
m_lstVCache[txt].Value -= height;
}
}
}
}
//void txt_MouseWheel(object sender, MouseEventArgs e)
//{
// TextBox txt = (TextBox)sender;
// if (m_lstVCache.ContainsKey(txt))
// {
// using (var g = txt.CreateGraphics())
// {
// StringBuilder str = new StringBuilder();
// for (int i = 0; i < System.Windows.Forms.SystemInformation.MouseWheelScrollLines; i++)
// {
// str.AppendLine("A");
// }
// var height = (int)g.MeasureString(str.ToString(), txt.Font).Height;
// if (e.Delta < 0)
// {
// if (height + m_lstVCache[txt].Value > m_lstVCache[txt].Maximum)
// m_lstVCache[txt].Value = m_lstVCache[txt].Maximum;
// else
// m_lstVCache[txt].Value += height;
// }
// else
// {
// if (m_lstVCache[txt].Value - height < 0)
// m_lstVCache[txt].Value = 0;
// else
// m_lstVCache[txt].Value -= height;
// }
// }
// }
//}
#endregion
}
}
......@@ -151,9 +151,11 @@ namespace HZH_Controls.Controls
set
{
moValue = value;
if (moValue > moMaximum)
moValue = moMaximum;
int nTrackWidth = (this.Width - btnWidth * 2);
float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
float fThumbWidth = nTrackWidth - Maximum;
//float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
int nThumbWidth = (int)fThumbWidth;
if (nThumbWidth > nTrackWidth)
......@@ -260,9 +262,14 @@ namespace HZH_Controls.Controls
/// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
private void CustomScrollbar_MouseDown(object sender, MouseEventArgs e)
{
if (Maximum <= 0)
{
return;
}
Point ptPoint = this.PointToClient(Cursor.Position);
int nTrackWidth = (this.Width - btnWidth * 2);
float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
float fThumbWidth = nTrackWidth - Maximum;
//float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
int nThumbWidth = (int)fThumbWidth;
if (nThumbWidth > nTrackWidth)
......@@ -366,6 +373,7 @@ namespace HZH_Controls.Controls
{
this.moThumbMouseDown = false;
this.moThumbMouseDragging = false;
Application.DoEvents();
}
/// <summary>
......@@ -376,7 +384,8 @@ namespace HZH_Controls.Controls
{
int nRealRange = Maximum - Minimum;
int nTrackWidth = (this.Width - btnWidth * 2);
float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
// float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
float fThumbWidth = nTrackWidth - Maximum;
int nThumbWidth = (int)fThumbWidth;
if (nThumbWidth > nTrackWidth)
......@@ -414,11 +423,13 @@ namespace HZH_Controls.Controls
float fPerc = (float)moThumbLeft / (float)nPixelRange;
float fValue = fPerc * (Maximum - LargeChange);
float fValue = fPerc * (Maximum - (nNewThumbLeft == nPixelRange ? 0 : LargeChange));
//float fValue = fPerc * (Maximum - LargeChange);
if (Math.Abs(moValue - fValue) >= 1)
Application.DoEvents();
else
return;
moValue = (int)fValue;
Application.DoEvents();
Invalidate();
}
}
......@@ -456,26 +467,30 @@ namespace HZH_Controls.Controls
{
base.OnPaint(e);
e.Graphics.SetGDIHigh();
//draw thumb
int nTrackWidth = (this.Width - btnWidth * 2);
float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
int nThumbWidth = (int)fThumbWidth;
if (nThumbWidth > nTrackWidth)
if (Maximum > 0)
{
nThumbWidth = nTrackWidth;
fThumbWidth = nTrackWidth;
}
if (nThumbWidth < m_intThumbMinWidth)
{
nThumbWidth = m_intThumbMinWidth;
fThumbWidth = m_intThumbMinWidth;
}
int nLeft = moThumbLeft;
nLeft += btnWidth;
e.Graphics.FillPath(new SolidBrush(thumbColor), new Rectangle(nLeft, 1, nThumbWidth, this.Height - 3).CreateRoundedRectanglePath(this.ConerRadius));
//draw thumb
int nTrackWidth = (this.Width - btnWidth * 2);
//float fThumbWidth = ((float)LargeChange / (float)Maximum) * nTrackWidth;
float fThumbWidth = nTrackWidth - Maximum;
int nThumbWidth = (int)fThumbWidth;
if (nThumbWidth > nTrackWidth)
{
nThumbWidth = nTrackWidth;
fThumbWidth = nTrackWidth;
}
if (nThumbWidth < m_intThumbMinWidth)
{
nThumbWidth = m_intThumbMinWidth;
fThumbWidth = m_intThumbMinWidth;
}
int nLeft = moThumbLeft;
nLeft += btnWidth;
if (nLeft + nThumbWidth > this.Width - btnWidth)
nLeft = this.Width - btnWidth - nThumbWidth;
e.Graphics.FillPath(new SolidBrush(thumbColor), new Rectangle(nLeft, 1, nThumbWidth, this.Height - 3).CreateRoundedRectanglePath(this.ConerRadius));
}
ControlHelper.PaintTriangle(e.Graphics, new SolidBrush(thumbColor), new Point(btnWidth - Math.Min(5, this.Height / 2), this.Height / 2), Math.Min(5, this.Height / 2), GraphDirection.Leftward);
ControlHelper.PaintTriangle(e.Graphics, new SolidBrush(thumbColor), new Point(this.Width - (btnWidth - Math.Min(5, this.Height / 2)), this.Height / 2), Math.Min(5, this.Height / 2), GraphDirection.Rightward);
......
......@@ -35,7 +35,7 @@ namespace HZH_Controls.Controls
[Designer(typeof(ScrollbarControlDesigner))]
[DefaultEvent("Scroll")]
public class UCVScrollbar : UCControlBase
{
{
/// <summary>
/// The mo large change
/// </summary>
......@@ -43,7 +43,7 @@ namespace HZH_Controls.Controls
/// <summary>
/// The mo small change
/// </summary>
protected int moSmallChange = 1;
protected int moSmallChange = 5;
/// <summary>
/// The mo minimum
/// </summary>
......@@ -92,7 +92,7 @@ namespace HZH_Controls.Controls
/// <summary>
/// The m int thumb minimum height
/// </summary>
private int m_intThumbMinHeight = 15;
private int m_intThumbMinHeight = 30;
/// <summary>
/// Gets or sets the height of the BTN.
......@@ -176,7 +176,8 @@ namespace HZH_Controls.Controls
moValue = value;
int nTrackHeight = (this.Height - btnHeight * 2);
float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
//float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
float fThumbHeight = nTrackHeight - Maximum;
int nThumbHeight = (int)fThumbHeight;
if (nThumbHeight > nTrackHeight)
......@@ -289,33 +290,35 @@ namespace HZH_Controls.Controls
{
base.OnPaint(e);
e.Graphics.SetGDIHigh();
//draw thumb
int nTrackHeight = (this.Height - btnHeight * 2);
float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
int nThumbHeight = (int)fThumbHeight;
if (nThumbHeight > nTrackHeight)
{
nThumbHeight = nTrackHeight;
fThumbHeight = nTrackHeight;
}
if (nThumbHeight < m_intThumbMinHeight)
if (Maximum > 0)
{
nThumbHeight = m_intThumbMinHeight;
fThumbHeight = m_intThumbMinHeight;
}
int nTop = moThumbTop;
nTop += btnHeight;
e.Graphics.FillPath(new SolidBrush(thumbColor), new Rectangle(1, nTop, this.Width - 3, nThumbHeight).CreateRoundedRectanglePath(this.ConerRadius));
//draw thumb
int nTrackHeight = (this.Height - btnHeight * 2);
//float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
float fThumbHeight = nTrackHeight - Maximum;
int nThumbHeight = (int)fThumbHeight;
if (nThumbHeight > nTrackHeight)
{
nThumbHeight = nTrackHeight;
fThumbHeight = nTrackHeight;
}
if (nThumbHeight < m_intThumbMinHeight)
{
nThumbHeight = m_intThumbMinHeight;
fThumbHeight = m_intThumbMinHeight;
}
int nTop = moThumbTop;
nTop += btnHeight;
if (nTop + nThumbHeight > this.Height - btnHeight)
nTop = this.Height - btnHeight - nThumbHeight;
e.Graphics.FillPath(new SolidBrush(thumbColor), new Rectangle(1, nTop, this.Width - 3, nThumbHeight).CreateRoundedRectanglePath(this.ConerRadius));
}
ControlHelper.PaintTriangle(e.Graphics, new SolidBrush(thumbColor), new Point(this.Width / 2, btnHeight - Math.Min(5, this.Width / 2)), Math.Min(5, this.Width / 2), GraphDirection.Upward);
ControlHelper.PaintTriangle(e.Graphics, new SolidBrush(thumbColor), new Point(this.Width / 2, this.Height - (btnHeight - Math.Min(5, this.Width / 2))), Math.Min(5, this.Width / 2), GraphDirection.Downward);
}
/// <summary>
/// Handles the MouseDown event of the CustomScrollbar control.
/// </summary>
......@@ -323,9 +326,12 @@ namespace HZH_Controls.Controls
/// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
private void CustomScrollbar_MouseDown(object sender, MouseEventArgs e)
{
if (Maximum <= 0)
return;
Point ptPoint = this.PointToClient(Cursor.Position);
int nTrackHeight = (this.Height - btnHeight * 2);
float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
//float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
float fThumbHeight = nTrackHeight - Maximum;
int nThumbHeight = (int)fThumbHeight;
if (nThumbHeight > nTrackHeight)
......@@ -419,7 +425,7 @@ namespace HZH_Controls.Controls
}
}
}
}
/// <summary>
......@@ -441,7 +447,8 @@ namespace HZH_Controls.Controls
{
int nRealRange = Maximum - Minimum;
int nTrackHeight = (this.Height - btnHeight * 2);
float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
//float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
float fThumbHeight = nTrackHeight - Maximum;
int nThumbHeight = (int)fThumbHeight;
if (nThumbHeight > nTrackHeight)
......@@ -479,11 +486,19 @@ namespace HZH_Controls.Controls
float fPerc = (float)moThumbTop / (float)nPixelRange;
float fValue = fPerc * (Maximum - LargeChange);
float fValue = fPerc * (Maximum - (nNewThumbTop == nPixelRange ? 0 : LargeChange));
try
{
if (Math.Abs(moValue - fValue) >= 1)
Application.DoEvents();
else
{
return;
}
}
catch
{ }
moValue = (int)fValue;
Application.DoEvents();
Invalidate();
}
}
......@@ -516,5 +531,5 @@ namespace HZH_Controls.Controls
Scroll(this, new EventArgs());
}
}
}
}
\ No newline at end of file
......@@ -209,7 +209,7 @@ namespace HZH_Controls
}
SetControlEnableds(lstCs.ToArray(), false);
}
ThreadPool.QueueUserWorkItem(delegate(object a)
ThreadPool.QueueUserWorkItem(delegate (object a)
{
try
{
......@@ -912,13 +912,13 @@ namespace HZH_Controls
public static Point[] GetRhombusFromRectangle(Rectangle rect)
{
return new Point[5]
{
new Point(rect.X, rect.Y + rect.Height / 2),
new Point(rect.X + rect.Width / 2, rect.Y + rect.Height - 1),
new Point(rect.X + rect.Width - 1, rect.Y + rect.Height / 2),
new Point(rect.X + rect.Width / 2, rect.Y),
new Point(rect.X, rect.Y + rect.Height / 2)
};
{
new Point(rect.X, rect.Y + rect.Height / 2),
new Point(rect.X + rect.Width / 2, rect.Y + rect.Height - 1),
new Point(rect.X + rect.Width - 1, rect.Y + rect.Height / 2),
new Point(rect.X + rect.Width / 2, rect.Y),
new Point(rect.X, rect.Y + rect.Height / 2)
};
}
/// <summary>
......@@ -1298,9 +1298,9 @@ namespace HZH_Controls
public static PointF[] GetPointsFrom(string points, float soureWidth, float sourceHeight, float width, float height, float dx = 0f, float dy = 0f)
{
string[] array = points.Split(new char[1]
{
' '
}, StringSplitOptions.RemoveEmptyEntries);
{
' '
}, StringSplitOptions.RemoveEmptyEntries);
PointF[] array2 = new PointF[array.Length];
for (int i = 0; i < array.Length; i++)
{
......@@ -1334,8 +1334,24 @@ namespace HZH_Controls
static uint SB_CTL = 0x2;
static uint SB_BOTH = 0x3;
[DllImport("user32.dll", SetLastError = true, EntryPoint = "GetScrollInfo")]
private static extern int GetScrollInfo(IntPtr hWnd, uint idObject, ref SCROLLINFO psbi);
private static extern int GetScrollInfo(IntPtr hWnd, uint fnBar, ref SCROLLINFO psbi);
[DllImport("user32.dll")]//[return: MarshalAs(UnmanagedType.Bool)]
private static extern int SetScrollInfo(IntPtr handle, uint fnBar, ref SCROLLINFO si, bool fRedraw);
[DllImport("user32.dll", EntryPoint = "PostMessage")]
private static extern bool PostMessage(IntPtr handle, int msg, uint wParam, uint lParam);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
/// <summary>
/// ShowScrollBar
/// </summary>
/// <param name="hWnd">hWnd</param>
/// <param name="wBar">0:horizontal,1:vertical,3:both</param>
/// <param name="bShow">bShow</param>
/// <returns></returns>
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);
/// <summary>
///获取水平滚动条信息
/// </summary>
......@@ -1344,8 +1360,8 @@ namespace HZH_Controls
public static SCROLLINFO GetHScrollBarInfo(IntPtr hWnd)
{
SCROLLINFO info = new SCROLLINFO();
info.cbSize = (uint)Marshal.SizeOf(info);
info.fMask = (int)ScrollInfoMask.SIF_ALL;
info.cbSize = (int)Marshal.SizeOf(info);
info.fMask = (int)ScrollInfoMask.SIF_DISABLENOSCROLL | (int)ScrollInfoMask.SIF_ALL;
int intRef = GetScrollInfo(hWnd, SB_HORZ, ref info);
return info;
}
......@@ -1357,20 +1373,21 @@ namespace HZH_Controls
public static SCROLLINFO GetVScrollBarInfo(IntPtr hWnd)
{
SCROLLINFO info = new SCROLLINFO();
info.cbSize = (uint)Marshal.SizeOf(info);
info.fMask = (int)ScrollInfoMask.SIF_ALL;
info.cbSize = (int)Marshal.SizeOf(info);
info.fMask = (int)ScrollInfoMask.SIF_DISABLENOSCROLL | (int)ScrollInfoMask.SIF_ALL;
int intRef = GetScrollInfo(hWnd, SB_VERT, ref info);
return info;
}
public struct SCROLLINFO
{
public uint cbSize;
public uint fMask;
public int cbSize;
public int fMask;
public int nMin;
public int nMax;
public uint nPage;
public int nPage;
public int nPos;
public int nTrackPos;
public int ScrollMax { get { return nMax + 1 - nPage; } }
}
public enum ScrollInfoMask : uint
{
......@@ -1380,6 +1397,66 @@ namespace HZH_Controls
SIF_DISABLENOSCROLL = 0x8,
SIF_TRACKPOS = 0x10,
SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS),
SB_THUMBTRACK = 5,
WM_HSCROLL = 0x0114,
WM_VSCROLL = 0x0115,
SB_LINEUP = 0,
SB_LINEDOWN = 1,
SB_LINELEFT = 0,
SB_LINERIGHT = 1,
}
public static void SetVScrollValue(IntPtr handle, int value)
{
var info = GetVScrollBarInfo(handle);
info.nPos = value;
SetScrollInfo(handle, SB_VERT, ref info, true);
PostMessage(handle, (int)ScrollInfoMask.WM_VSCROLL, MakeLong((short)ScrollInfoMask.SB_THUMBTRACK, highPart: (short)info.nPos), 0);
}
public static void SetHScrollValue(IntPtr handle, int value)
{
var info = GetHScrollBarInfo(handle);
info.nPos = value;
SetScrollInfo(handle, SB_HORZ, ref info, true);
PostMessage(handle, (int)ScrollInfoMask.WM_HSCROLL, MakeLong((short)ScrollInfoMask.SB_THUMBTRACK, highPart: (short)info.nPos), 0);
}
private static uint MakeLong(short lowPart, short highPart)
{
return (ushort)lowPart | (uint)(highPart << 16);
}
/// <summary>
/// 控件向上滚动一个单位
/// </summary>
/// <param name="handle">控件句柄</param>
public static void ScrollUp(IntPtr handle)
{
SendMessage(handle, (int)ScrollInfoMask.WM_VSCROLL, (int)ScrollInfoMask.SB_LINEUP, 0);
}
/// <summary>
/// 控件向下滚动一个单位
/// </summary>
/// <param name="handle">控件句柄</param>
public static void ScrollDown(IntPtr handle)
{
SendMessage(handle, (int)ScrollInfoMask.WM_VSCROLL, (int)ScrollInfoMask.SB_LINEDOWN, 0);
}
/// <summary>
/// 控件向左滚动一个单位
/// </summary>
/// <param name="handle">控件句柄</param>
public static void ScrollLeft(IntPtr handle)
{
SendMessage(handle, (int)ScrollInfoMask.WM_HSCROLL, (int)ScrollInfoMask.SB_LINELEFT, 0);
}
/// <summary>
/// 控件向右滚动一个单位
/// </summary>
/// <param name="handle">控件句柄</param>
public static void ScrollRight(IntPtr handle)
{
SendMessage(handle, (int)ScrollInfoMask.WM_VSCROLL, (int)ScrollInfoMask.SB_LINERIGHT, 0);
}
#endregion
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!