查了很多資料
老習慣
注意
下面簡單介紹下重繪系統滾動條的詳細步驟:
uparrow
ThumbBottom
ThumbMiddle
downarrow
Code
using System;
using System
using System
using System
using System
using System
using System
using System
using System
namespace Winamp
{
[Designer(typeof(ScrollbarControlDesigner))]
public partial class CustomScrollbar : UserControl
{
protected Color moChannelColor = Color
protected Image moUpArrowImage = null;//上箭頭
//protected Image moUpArrowImage_Over = null;
//protected Image moUpArrowImage_Down = null;
protected Image moDownArrowImage = null;//下箭頭
//protected Image moDownArrowImage_Over = null;
//protected Image moDownArrowImage_Down = null;
protected Image moThumbArrowImage = null;
protected Image moThumbTopImage = null;
protected Image moThumbTopSpanImage = null;
protected Image moThumbBottomImage = null;
protected Image moThumbBottomSpanImage = null;
protected Image moThumbMiddleImage = null;
protected int moLargeChange =
protected int moSmallChange =
protected int moMinimum =
protected int moMaximum =
protected int moValue =
private int nClickPoint;
protected int moThumbTop =
protected bool moAutoSize = false;
private bool moThumbDown = false;
private bool moThumbDragging = false;
public new event EventHandler Scroll = null;
public event EventHandler ValueChanged = null;
private int GetThumbHeight()
{
int nTrackHeight = (this
float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
int nThumbHeight = (int)fThumbHeight;
if (nThumbHeight > nTrackHeight)
{
nThumbHeight = nTrackHeight;
fThumbHeight = nTrackHeight;
}
if (nThumbHeight <
{
nThumbHeight =
fThumbHeight =
}
return nThumbHeight;
}
public CustomScrollbar()
{
InitializeComponent();
SetStyle(ControlStyles
SetStyle(ControlStyles
SetStyle(ControlStyles
moChannelColor = Color
UpArrowImage = BASSSkin
DownArrowImage = BASSSkin
ThumbBottomImage = BASSSkin
ThumbMiddleImage = BASSSkin
this
base
}
[EditorBrowsable(EditorBrowsableState
public int LargeChange
{
get { return moLargeChange; }
set
{
moLargeChange = value;
Invalidate();
}
}
[EditorBrowsable(EditorBrowsableState
public int SmallChange
{
get { return moSmallChange; }
set
{
moSmallChange = value;
Invalidate();
}
}
[EditorBrowsable(EditorBrowsableState
public int Minimum
{
get { return moMinimum; }
set
{
moMinimum = value;
Invalidate();
}
}
[EditorBrowsable(EditorBrowsableState
public int Maximum
{
get { return moMaximum; }
set
{
moMaximum = value;
Invalidate();
}
}
[EditorBrowsable(EditorBrowsableState
public int Value
{
get { return moValue; }
set
{
moValue = value;
int nTrackHeight = (this
float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
int nThumbHeight = (int)fThumbHeight;
if (nThumbHeight > nTrackHeight)
{
nThumbHeight = nTrackHeight;
fThumbHeight = nTrackHeight;
}
if (nThumbHeight <
{
nThumbHeight =
fThumbHeight =
}
//figure out value
int nPixelRange = nTrackHeight
int nRealRange = (Maximum
float fPerc =
if (nRealRange !=
{
fPerc = (float)moValue / (float)nRealRange;
}
float fTop = fPerc * nPixelRange;
moThumbTop = (int)fTop;
Invalidate();
}
}
[EditorBrowsable(EditorBrowsableState
public Color ChannelColor
{
get { return moChannelColor; }
set { moChannelColor = value; }
}
[EditorBrowsable(EditorBrowsableState
public Image UpArrowImage
{
get { return moUpArrowImage; }
set { moUpArrowImage = value; }
}
[EditorBrowsable(EditorBrowsableState
public Image DownArrowImage
{
get { return moDownArrowImage; }
set { moDownArrowImage = value; }
}
[EditorBrowsable(EditorBrowsableState
public Image ThumbBottomImage
{
get { return moThumbBottomImage; }
set { moThumbBottomImage = value; }
}
[EditorBrowsable(EditorBrowsableState
public Image ThumbMiddleImage
{
get { return moThumbMiddleImage; }
set { moThumbMiddleImage = value; }
}
protected override void OnPaint(PaintEventArgs e)
{
e
if (UpArrowImage != null)
{
e
}
Brush oBrush = new SolidBrush(moChannelColor);
Brush oWhiteBrush = new SolidBrush(Color
// 函數名: rectangle
//功 能: 畫一個矩形
//用 法: void far rectangle(int left
//draw channel left and right border colors
e
e
//draw channel
//e
e
//draw thumb
int nTrackHeight = (this
float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
int nThumbHeight = (int)fThumbHeight;
if (nThumbHeight > nTrackHeight)
{
nThumbHeight = nTrackHeight;
fThumbHeight = nTrackHeight;
}
//MessageBox
if (nThumbHeight <
{
nThumbHeight =
fThumbHeight =
}
//Debug
//float fSpanHeight = (fThumbHeight
//int nSpanHeight = (int)fSpanHeight;
int nTop = moThumbTop;//
nTop += UpArrowImage
//draw top畫上面的按鈕
//e
//nTop += ThumbTopImage
//draw top span
//Rectangle rect = new Rectangle(
//e
// nTop += nSpanHeight;//
//draw middle
e
//nTop += ThumbMiddleImage
//draw top span
//rect = new Rectangle(
//e
//nTop += nSpanHeight;
//draw bottom
//e
if (DownArrowImage != null)
{
e
}
}
public override bool AutoSize
{
get
{
return base
}
set
{
base
if (base
{
this
}
}
}
private void InitializeComponent()
{
this
//
// CustomScrollbar
//
this
this
this
this
this
}
private void CustomScrollbar_MouseDown(object sender
{
Point ptPoint = this
int nTrackHeight = (this
float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
int nThumbHeight = (int)fThumbHeight;
if (nThumbHeight > nTrackHeight)
{
nThumbHeight = nTrackHeight;
fThumbHeight = nTrackHeight;
}
if (nThumbHeight <
{
nThumbHeight =
fThumbHeight =
}
int nTop = moThumbTop;
nTop += UpArrowImage
Rectangle thumbrect = new Rectangle(new Point(
if (thumbrect
{
//hit the thumb
nClickPoint = (ptPoint
//MessageBox
this
}
Rectangle uparrowrect = new Rectangle(new Point(
if (uparrowrect
{
int nRealRange = (Maximum
int nPixelRange = (nTrackHeight
if (nRealRange >
{
if (nPixelRange >
{
if ((moThumbTop
moThumbTop =
else
moThumbTop
//figure out value
float fPerc = (float)moThumbTop / (float)nPixelRange;
float fValue = fPerc * (Maximum
moValue = (int)fValue;
Debug
if (ValueChanged != null)
ValueChanged(this
if (Scroll != null)
Scroll(this
Invalidate();
}
}
}
Rectangle downarrowrect = new Rectangle(new Point(
if (downarrowrect
{
int nRealRange = (Maximum
int nPixelRange = (nTrackHeight
if (nRealRange >
{
if (nPixelRange >
{
if ((moThumbTop + SmallChange) > nPixelRange)
moThumbTop = nPixelRange;
else
moThumbTop += SmallChange;
//figure out value
float fPerc = (float)moThumbTop / (float)nPixelRange;
float fValue = fPerc * (Maximum
moValue = (int)fValue;
Debug
if (ValueChanged != null)
ValueChanged(this
if (Scroll != null)
Scroll(this
Invalidate();
}
}
}
}
private void CustomScrollbar_MouseUp(object sender
{
this
this
}
private void MoveThumb(int y)
{
int nRealRange = Maximum
int nTrackHeight = (this
float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight;
int nThumbHeight = (int)fThumbHeight;
if (nThumbHeight > nTrackHeight)
{
nThumbHeight = nTrackHeight;
fThumbHeight = nTrackHeight;
}
if (nThumbHeight <
{
nThumbHeight =
fThumbHeight =
}
int nSpot = nClickPoint;
int nPixelRange = (nTrackHeight
if (moThumbDown && nRealRange >
{
if (nPixelRange >
{
int nNewThumbTop = y
if (nNewThumbTop <
{
moThumbTop = nNewThumbTop =
}
else if (nNewThumbTop > nPixelRange)
{
moThumbTop = nNewThumbTop = nPixelRange;
}
else
{
moThumbTop = y
}
//figure out value
float fPerc = (float)moThumbTop / (float)nPixelRange;
float fValue = fPerc * (Maximum
moValue = (int)fValue;
Debug
Application
Invalidate();
}
}
}
private void CustomScrollbar_MouseMove(object sender
MouseEventArgs e)
{
if (moThumbDown == true)
{
this
}
if (this
{
MoveThumb(e
}
if (ValueChanged != null)
ValueChanged(this
if (Scroll != null)
Scroll(this
}
}
internal class ScrollbarControlDesigner : System
{
public override SelectionRules SelectionRules
{
get
{
SelectionRules selectionRules = base
PropertyDescriptor propDescriptor = TypeDescriptor
if (propDescriptor != null)
{
bool autoSize = (bool)propDescriptor
if (autoSize)
{
selectionRules = SelectionRules
}
else
{
selectionRules = SelectionRules
}
}
return selectionRules;
}
}
}
}
目前只想簡單實現滾動條中上箭頭/下箭頭/滑道/拖動塊的重寫
接下來就是怎麼用它來控制ListBox的內容滾動的問題了
在你的窗體裡拖一個listbox控件和一個上邊我們制作好的用戶控件
然後往listBox中隨便多弄寫內容
然後我們定義一下Win
Code
public class Win
{
[StructLayout(LayoutKind
public struct tagSCROLLINFO
{
public uint cbSize;
public uint fMask;
public int nMin;
public int nMax;
public uint nPage;
public int nPos;
public int nTrackPos;
}
public enum fnBar
{
SB_HORZ =
SB_VERT =
SB_CTL =
}
public enum fMask
{
SIF_ALL
SIF_DISABLENOSCROLL =
SIF_PAGE =
SIF_POS =
SIF_RANGE =
SIF_TRACKPOS =
}
public static int MakeLong(short lowPart
{
return (int)(((ushort)lowPart) | (uint)(highPart <<
}
public const int SB_THUMBTRACK =
public const int WM_HSCROLL =
public const int WM_VSCROLL =
[DllImport(
public static extern bool GetScrollInfo(IntPtr hwnd
[DllImport(
public static extern int SetScrollInfo(IntPtr hwnd
[DllImport(
static extern IntPtr SendMessage(IntPtr hWnd
[DllImport(
public static extern bool PostMessage(IntPtr hWnd
}
public struct SCROLLINFO
{
public uint cbSize;
public uint fMask;
public int nMin;
public int nMax;
public uint nPage;
public int nPos;
public int nTrackPos;
}
enum ScrollInfoMask
{
SIF_RANGE =
SIF_PAGE =
SIF_POS =
SIF_DISABLENOSCROLL =
SIF_TRACKPOS =
SIF_ALL = SIF_RANGE + SIF_PAGE + SIF_POS + SIF_TRACKPOS
}
enum ScrollBarDirection
{
SB_HORZ =
SB_VERT =
SB_CTL =
SB_BOTH =
}
public SCROLLINFO tvImageListScrollInfo
{
get
{
SCROLLINFO si = new SCROLLINFO();
si
si
Win
return si;
}
}
//當鼠標滾動時
private void SetImageListScroll()
{
SCROLLINFO info = tvImageListScrollInfo;
if (info
{
int pos = info
if (pos >=
{
customScrollbar
}
}
}
定義customScrollbar
Code
private void customScrollbar
{
//當滾動條滾動時
SCROLLINFO info = tvImageListScrollInfo;
info
Win
Win
}
From:http://tw.wingwit.com/Article/program/net/201311/12416.html