Commit 098cffbf kwwwvagaa

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

1 个父辈 859c6bcd
...@@ -151,9 +151,11 @@ namespace HZH_Controls.Controls ...@@ -151,9 +151,11 @@ namespace HZH_Controls.Controls
set set
{ {
moValue = value; moValue = value;
if (moValue > moMaximum)
moValue = moMaximum;
int nTrackWidth = (this.Width - btnWidth * 2); 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; int nThumbWidth = (int)fThumbWidth;
if (nThumbWidth > nTrackWidth) if (nThumbWidth > nTrackWidth)
...@@ -260,9 +262,14 @@ namespace HZH_Controls.Controls ...@@ -260,9 +262,14 @@ namespace HZH_Controls.Controls
/// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param> /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
private void CustomScrollbar_MouseDown(object sender, MouseEventArgs e) private void CustomScrollbar_MouseDown(object sender, MouseEventArgs e)
{ {
if (Maximum <= 0)
{
return;
}
Point ptPoint = this.PointToClient(Cursor.Position); Point ptPoint = this.PointToClient(Cursor.Position);
int nTrackWidth = (this.Width - btnWidth * 2); 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; int nThumbWidth = (int)fThumbWidth;
if (nThumbWidth > nTrackWidth) if (nThumbWidth > nTrackWidth)
...@@ -366,6 +373,7 @@ namespace HZH_Controls.Controls ...@@ -366,6 +373,7 @@ namespace HZH_Controls.Controls
{ {
this.moThumbMouseDown = false; this.moThumbMouseDown = false;
this.moThumbMouseDragging = false; this.moThumbMouseDragging = false;
Application.DoEvents();
} }
/// <summary> /// <summary>
...@@ -376,7 +384,8 @@ namespace HZH_Controls.Controls ...@@ -376,7 +384,8 @@ namespace HZH_Controls.Controls
{ {
int nRealRange = Maximum - Minimum; int nRealRange = Maximum - Minimum;
int nTrackWidth = (this.Width - btnWidth * 2); 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; int nThumbWidth = (int)fThumbWidth;
if (nThumbWidth > nTrackWidth) if (nThumbWidth > nTrackWidth)
...@@ -414,11 +423,13 @@ namespace HZH_Controls.Controls ...@@ -414,11 +423,13 @@ namespace HZH_Controls.Controls
float fPerc = (float)moThumbLeft / (float)nPixelRange; float fPerc = (float)moThumbLeft / (float)nPixelRange;
float fValue = fPerc * (Maximum - LargeChange); float fValue = fPerc * (Maximum - (nNewThumbLeft == nPixelRange ? 0 : LargeChange));
moValue = (int)fValue; //float fValue = fPerc * (Maximum - LargeChange);
if (Math.Abs(moValue - fValue) >= 1)
Application.DoEvents(); Application.DoEvents();
else
return;
moValue = (int)fValue;
Invalidate(); Invalidate();
} }
} }
...@@ -456,10 +467,12 @@ namespace HZH_Controls.Controls ...@@ -456,10 +467,12 @@ namespace HZH_Controls.Controls
{ {
base.OnPaint(e); base.OnPaint(e);
e.Graphics.SetGDIHigh(); e.Graphics.SetGDIHigh();
if (Maximum > 0)
{
//draw thumb //draw thumb
int nTrackWidth = (this.Width - btnWidth * 2); 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; int nThumbWidth = (int)fThumbWidth;
if (nThumbWidth > nTrackWidth) if (nThumbWidth > nTrackWidth)
...@@ -474,8 +487,10 @@ namespace HZH_Controls.Controls ...@@ -474,8 +487,10 @@ namespace HZH_Controls.Controls
} }
int nLeft = moThumbLeft; int nLeft = moThumbLeft;
nLeft += btnWidth; 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)); 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(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); 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);
......
...@@ -43,7 +43,7 @@ namespace HZH_Controls.Controls ...@@ -43,7 +43,7 @@ namespace HZH_Controls.Controls
/// <summary> /// <summary>
/// The mo small change /// The mo small change
/// </summary> /// </summary>
protected int moSmallChange = 1; protected int moSmallChange = 5;
/// <summary> /// <summary>
/// The mo minimum /// The mo minimum
/// </summary> /// </summary>
...@@ -92,7 +92,7 @@ namespace HZH_Controls.Controls ...@@ -92,7 +92,7 @@ namespace HZH_Controls.Controls
/// <summary> /// <summary>
/// The m int thumb minimum height /// The m int thumb minimum height
/// </summary> /// </summary>
private int m_intThumbMinHeight = 15; private int m_intThumbMinHeight = 30;
/// <summary> /// <summary>
/// Gets or sets the height of the BTN. /// Gets or sets the height of the BTN.
...@@ -176,7 +176,8 @@ namespace HZH_Controls.Controls ...@@ -176,7 +176,8 @@ namespace HZH_Controls.Controls
moValue = value; moValue = value;
int nTrackHeight = (this.Height - btnHeight * 2); 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; int nThumbHeight = (int)fThumbHeight;
if (nThumbHeight > nTrackHeight) if (nThumbHeight > nTrackHeight)
...@@ -289,10 +290,12 @@ namespace HZH_Controls.Controls ...@@ -289,10 +290,12 @@ namespace HZH_Controls.Controls
{ {
base.OnPaint(e); base.OnPaint(e);
e.Graphics.SetGDIHigh(); e.Graphics.SetGDIHigh();
if (Maximum > 0)
{
//draw thumb //draw thumb
int nTrackHeight = (this.Height - btnHeight * 2); 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; int nThumbHeight = (int)fThumbHeight;
if (nThumbHeight > nTrackHeight) if (nThumbHeight > nTrackHeight)
...@@ -307,15 +310,15 @@ namespace HZH_Controls.Controls ...@@ -307,15 +310,15 @@ namespace HZH_Controls.Controls
} }
int nTop = moThumbTop; int nTop = moThumbTop;
nTop += btnHeight; 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)); 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, 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); 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> /// <summary>
/// Handles the MouseDown event of the CustomScrollbar control. /// Handles the MouseDown event of the CustomScrollbar control.
/// </summary> /// </summary>
...@@ -323,9 +326,12 @@ namespace HZH_Controls.Controls ...@@ -323,9 +326,12 @@ namespace HZH_Controls.Controls
/// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param> /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
private void CustomScrollbar_MouseDown(object sender, MouseEventArgs e) private void CustomScrollbar_MouseDown(object sender, MouseEventArgs e)
{ {
if (Maximum <= 0)
return;
Point ptPoint = this.PointToClient(Cursor.Position); Point ptPoint = this.PointToClient(Cursor.Position);
int nTrackHeight = (this.Height - btnHeight * 2); 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; int nThumbHeight = (int)fThumbHeight;
if (nThumbHeight > nTrackHeight) if (nThumbHeight > nTrackHeight)
...@@ -441,7 +447,8 @@ namespace HZH_Controls.Controls ...@@ -441,7 +447,8 @@ namespace HZH_Controls.Controls
{ {
int nRealRange = Maximum - Minimum; int nRealRange = Maximum - Minimum;
int nTrackHeight = (this.Height - btnHeight * 2); 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; int nThumbHeight = (int)fThumbHeight;
if (nThumbHeight > nTrackHeight) if (nThumbHeight > nTrackHeight)
...@@ -479,11 +486,19 @@ namespace HZH_Controls.Controls ...@@ -479,11 +486,19 @@ namespace HZH_Controls.Controls
float fPerc = (float)moThumbTop / (float)nPixelRange; float fPerc = (float)moThumbTop / (float)nPixelRange;
float fValue = fPerc * (Maximum - LargeChange); float fValue = fPerc * (Maximum - (nNewThumbTop == nPixelRange ? 0 : LargeChange));
moValue = (int)fValue; try
{
if (Math.Abs(moValue - fValue) >= 1)
Application.DoEvents(); Application.DoEvents();
else
{
return;
}
}
catch
{ }
moValue = (int)fValue;
Invalidate(); Invalidate();
} }
} }
......
...@@ -209,7 +209,7 @@ namespace HZH_Controls ...@@ -209,7 +209,7 @@ namespace HZH_Controls
} }
SetControlEnableds(lstCs.ToArray(), false); SetControlEnableds(lstCs.ToArray(), false);
} }
ThreadPool.QueueUserWorkItem(delegate(object a) ThreadPool.QueueUserWorkItem(delegate (object a)
{ {
try try
{ {
...@@ -1334,8 +1334,24 @@ namespace HZH_Controls ...@@ -1334,8 +1334,24 @@ namespace HZH_Controls
static uint SB_CTL = 0x2; static uint SB_CTL = 0x2;
static uint SB_BOTH = 0x3; static uint SB_BOTH = 0x3;
[DllImport("user32.dll", SetLastError = true, EntryPoint = "GetScrollInfo")] [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>
///获取水平滚动条信息 ///获取水平滚动条信息
/// </summary> /// </summary>
...@@ -1344,8 +1360,8 @@ namespace HZH_Controls ...@@ -1344,8 +1360,8 @@ namespace HZH_Controls
public static SCROLLINFO GetHScrollBarInfo(IntPtr hWnd) public static SCROLLINFO GetHScrollBarInfo(IntPtr hWnd)
{ {
SCROLLINFO info = new SCROLLINFO(); SCROLLINFO info = new SCROLLINFO();
info.cbSize = (uint)Marshal.SizeOf(info); info.cbSize = (int)Marshal.SizeOf(info);
info.fMask = (int)ScrollInfoMask.SIF_ALL; info.fMask = (int)ScrollInfoMask.SIF_DISABLENOSCROLL | (int)ScrollInfoMask.SIF_ALL;
int intRef = GetScrollInfo(hWnd, SB_HORZ, ref info); int intRef = GetScrollInfo(hWnd, SB_HORZ, ref info);
return info; return info;
} }
...@@ -1357,20 +1373,21 @@ namespace HZH_Controls ...@@ -1357,20 +1373,21 @@ namespace HZH_Controls
public static SCROLLINFO GetVScrollBarInfo(IntPtr hWnd) public static SCROLLINFO GetVScrollBarInfo(IntPtr hWnd)
{ {
SCROLLINFO info = new SCROLLINFO(); SCROLLINFO info = new SCROLLINFO();
info.cbSize = (uint)Marshal.SizeOf(info); info.cbSize = (int)Marshal.SizeOf(info);
info.fMask = (int)ScrollInfoMask.SIF_ALL; info.fMask = (int)ScrollInfoMask.SIF_DISABLENOSCROLL | (int)ScrollInfoMask.SIF_ALL;
int intRef = GetScrollInfo(hWnd, SB_VERT, ref info); int intRef = GetScrollInfo(hWnd, SB_VERT, ref info);
return info; return info;
} }
public struct SCROLLINFO public struct SCROLLINFO
{ {
public uint cbSize; public int cbSize;
public uint fMask; public int fMask;
public int nMin; public int nMin;
public int nMax; public int nMax;
public uint nPage; public int nPage;
public int nPos; public int nPos;
public int nTrackPos; public int nTrackPos;
public int ScrollMax { get { return nMax + 1 - nPage; } }
} }
public enum ScrollInfoMask : uint public enum ScrollInfoMask : uint
{ {
...@@ -1380,6 +1397,66 @@ namespace HZH_Controls ...@@ -1380,6 +1397,66 @@ namespace HZH_Controls
SIF_DISABLENOSCROLL = 0x8, SIF_DISABLENOSCROLL = 0x8,
SIF_TRACKPOS = 0x10, SIF_TRACKPOS = 0x10,
SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS), 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 #endregion
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!