Commit 098cffbf kwwwvagaa

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

1 个父辈 859c6bcd
......@@ -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!