知识大全 重绘的滚动条控制ListBox的滚动
Posted 知
篇首语:天不生无用之人,地不长无名之草。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 重绘的滚动条控制ListBox的滚动相关的知识,希望对你有一定的参考价值。
重绘的滚动条控制ListBox的滚动 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
查了很多资料 都找不到直接重写ListBox滚动条的方法 只能曲线救国 先自己重绘一个带皮肤的滚动条 然后让它取代ListBox现有的滚动条 老习惯 先传个效果图 你觉得感兴趣就继续看下去 不喜欢的话就此打住 懒得耽误你宝贵的时间 嘿嘿
注意 此图中的滚动条宽度明显小于ListBox本身滚动条的宽度 我目前只顾着实现功能了 毕竟 宽度调整相当简单哈
下面简单介绍下重绘系统滚动条的详细步骤:
在项目中添加新项 用户控件 我们命名为CustomScrollbar cs
准备几张图片添加进项目资源作为滚动条重绘时要用的背景 我用的图片如下:
uparrow png资源名称为uparrow 滚动条的上箭头
ThumbBottom png资源名称为ThumbBottom 滚动条中间滑道的背景
ThumbMiddle png资源名称为ThumbMiddle 滚动条的中间的拖动块
downarrow png资源名称为downarrow 滚动条的下箭头
然后就是利用上面图片做背景重画滚动条背景了 直接给出CustomScrollbar cs的代码吧Codeusing System;using System Collections Generic;using System ComponentModel;using System Drawing;using System Data;using System Text;using System Windows Forms;using System Windows Forms Design;using System Diagnostics;namespace Winamp [Designer(typeof(ScrollbarControlDesigner))] public partial class CustomScrollbar : UserControl
protected Color moChannelColor = Color Empty; 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 Height (UpArrowImage Height + DownArrowImage Height)); 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 ResizeRedraw true); SetStyle(ControlStyles AllPaintingInWmPaint true); SetStyle(ControlStyles DoubleBuffer true);
moChannelColor = Color FromArgb( ); UpArrowImage = BASSSkin uparrow;//上箭头 DownArrowImage = BASSSkin downarrow;//下肩头
ThumbBottomImage = BASSSkin ThumbBottom;
ThumbMiddleImage = BASSSkin ThumbMiddle;
this Width = UpArrowImage Width;// px base MinimumSize = new Size(UpArrowImage Width UpArrowImage Height + DownArrowImage Height + GetThumbHeight());
[EditorBrowsable(EditorBrowsableState Always) Browsable(true) DefaultValue(false) Category( Behavior ) Description( LargeChange )] public int LargeChange get return moLargeChange; set moLargeChange = value; Invalidate();
[EditorBrowsable(EditorBrowsableState Always) Browsable(true) DefaultValue(false) Category( Behavior ) Description( SmallChange )] public int SmallChange get return moSmallChange; set moSmallChange = value; Invalidate();
[EditorBrowsable(EditorBrowsableState Always) Browsable(true) DefaultValue(false) Category( Behavior ) Description( Minimum )] public int Minimum get return moMinimum; set moMinimum = value; Invalidate();
[EditorBrowsable(EditorBrowsableState Always) Browsable(true) DefaultValue(false) Category( Behavior ) Description( Maximum )] public int Maximum get return moMaximum; set moMaximum = value; Invalidate();
[EditorBrowsable(EditorBrowsableState Always) Browsable(true) DefaultValue(false) Category( Behavior ) Description( Value )] public int Value get return moValue; set moValue = value;
int nTrackHeight = (this Height (UpArrowImage Height + DownArrowImage Height)); 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 nThumbHeight; int nRealRange = (Maximum Minimum) LargeChange; float fPerc = f; if (nRealRange != ) fPerc = (float)moValue / (float)nRealRange;
float fTop = fPerc * nPixelRange; moThumbTop = (int)fTop;
Invalidate();
[EditorBrowsable(EditorBrowsableState Always) Browsable(true) DefaultValue(false) Category( Skin ) Description( Channel Color )] public Color ChannelColor get return moChannelColor; set moChannelColor = value;
[EditorBrowsable(EditorBrowsableState Always) Browsable(true) DefaultValue(false) Category( Skin ) Description( Up Arrow Graphic )] public Image UpArrowImage get return moUpArrowImage; set moUpArrowImage = value;
[EditorBrowsable(EditorBrowsableState Always) Browsable(true) DefaultValue(false) Category( Skin ) Description( Up Arrow Graphic )] public Image DownArrowImage get return moDownArrowImage; set moDownArrowImage = value;
[EditorBrowsable(EditorBrowsableState Always) Browsable(true) DefaultValue(false) Category( Skin ) Description( Up Arrow Graphic )] public Image ThumbBottomImage get return moThumbBottomImage; set moThumbBottomImage = value;
[EditorBrowsable(EditorBrowsableState Always) Browsable(true) DefaultValue(false) Category( Skin ) Description( Up Arrow Graphic )] public Image ThumbMiddleImage get return moThumbMiddleImage; set moThumbMiddleImage = value;
protected override void OnPaint(PaintEventArgs e)
e Graphics InterpolationMode = System Drawing Drawing D InterpolationMode NearestNeighbor;
if (UpArrowImage != null) e Graphics DrawImage(UpArrowImage new Rectangle(new Point( ) new Size(this Width UpArrowImage Height)));
Brush oBrush = new SolidBrush(moChannelColor); Brush oWhiteBrush = new SolidBrush(Color FromArgb( )); // 函数名: rectangle //功 能: 画一个矩形 //用 法: void far rectangle(int left int top int right int bottom);
//draw channel left and right border colors e Graphics FillRectangle(oWhiteBrush new Rectangle( UpArrowImage Height (this Height DownArrowImage Height))); e Graphics FillRectangle(oWhiteBrush new Rectangle(this Width UpArrowImage Height (this Height DownArrowImage Height)));
//draw channel //e Graphics FillRectangle(oBrush new Rectangle( UpArrowImage Height this Width (this Height DownArrowImage Height))); e Graphics DrawImage(ThumbBottomImage new Rectangle( UpArrowImage Height this Width (this Height DownArrowImage Height))); //draw thumb int nTrackHeight = (this Height (UpArrowImage Height + DownArrowImage Height)); float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight; int nThumbHeight = (int)fThumbHeight;
if (nThumbHeight > nTrackHeight) nThumbHeight = nTrackHeight; fThumbHeight = nTrackHeight; //MessageBox Show(nThumbHeight ToString()); if (nThumbHeight < ) nThumbHeight = ; fThumbHeight = ;
//Debug WriteLine(nThumbHeight ToString());
//float fSpanHeight = (fThumbHeight (ThumbMiddleImage Height + ThumbTopImage Height + ThumbBottomImage Height)) / f; //int nSpanHeight = (int)fSpanHeight;
int nTop = moThumbTop;// nTop += UpArrowImage Height;// px
//draw top画上面的按钮 //e Graphics DrawImage(ThumbTopImage new Rectangle( nTop this Width ThumbTopImage Height));
//nTop += ThumbTopImage Height;// px //draw top span //Rectangle rect = new Rectangle( nTop this Width nSpanHeight);
//e Graphics DrawImage(ThumbTopSpanImage f (float)nTop (float)this Width f (float) fSpanHeight* );
// nTop += nSpanHeight;// px //draw middle e Graphics DrawImage(ThumbMiddleImage new Rectangle( nTop this Width ThumbMiddleImage Height));
//nTop += ThumbMiddleImage Height; //draw top span //rect = new Rectangle( nTop this Width nSpanHeight* ); //e Graphics DrawImage(ThumbBottomSpanImage rect);
//nTop += nSpanHeight; //draw bottom //e Graphics DrawImage(ThumbBottomImage new Rectangle( nTop this Width nSpanHeight));
if (DownArrowImage != null) e Graphics DrawImage(DownArrowImage new Rectangle(new Point( (this Height DownArrowImage Height)) new Size(this Width DownArrowImage Height)));
public override bool AutoSize get return base AutoSize; set base AutoSize = value; if (base AutoSize) this Width = moUpArrowImage Width;
private void InitializeComponent() this SuspendLayout(); // // CustomScrollbar // this Name = CustomScrollbar ; this MouseDown += new System Windows Forms MouseEventHandler(this CustomScrollbar_MouseDown); this MouseMove += new System Windows Forms MouseEventHandler(this CustomScrollbar_MouseMove); this MouseUp += new System Windows Forms MouseEventHandler(this CustomScrollbar_MouseUp); this ResumeLayout(false);
private void CustomScrollbar_MouseDown(object sender MouseEventArgs e) Point ptPoint = this PointToClient(Cursor Position); int nTrackHeight = (this Height (UpArrowImage Height + DownArrowImage Height)); 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 Height;
Rectangle thumbrect = new Rectangle(new Point( nTop) new Size(ThumbMiddleImage Width nThumbHeight)); if (thumbrect Contains(ptPoint))
//hit the thumb nClickPoint = (ptPoint Y nTop); //MessageBox Show(Convert ToString((ptPoint Y nTop))); this moThumbDown = true;
Rectangle uparrowrect = new Rectangle(new Point( ) new Size(UpArrowImage Width UpArrowImage Height)); if (uparrowrect Contains(ptPoint))
int nRealRange = (Maximum Minimum) LargeChange; int nPixelRange = (nTrackHeight nThumbHeight); if (nRealRange > ) if (nPixelRange > ) if ((moThumbTop SmallChange) < ) moThumbTop = ; else moThumbTop = SmallChange;
//figure out value float fPerc = (float)moThumbTop / (float)nPixelRange; float fValue = fPerc * (Maximum LargeChange);
moValue = (int)fValue; Debug WriteLine(moValue ToString());
if (ValueChanged != null) ValueChanged(this new EventArgs());
if (Scroll != null) Scroll(this new EventArgs());
Invalidate();
Rectangle downarrowrect = new Rectangle(new Point( UpArrowImage Height + nTrackHeight) new Size(UpArrowImage Width UpArrowImage Height)); if (downarrowrect Contains(ptPoint)) int nRealRange = (Maximum Minimum) LargeChange; int nPixelRange = (nTrackHeight nThumbHeight); 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 LargeChange);
moValue = (int)fValue; Debug WriteLine(moValue ToString());
if (ValueChanged != null) ValueChanged(this new EventArgs());
if (Scroll != null) Scroll(this new EventArgs());
Invalidate();
private void CustomScrollbar_MouseUp(object sender MouseEventArgs e) this moThumbDown = false; this moThumbDragging = false;
private void MoveThumb(int y) int nRealRange = Maximum Minimum; int nTrackHeight = (this Height (UpArrowImage Height + DownArrowImage Height)); 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 nThumbHeight); if (moThumbDown && nRealRange > ) if (nPixelRange > ) int nNewThumbTop = y (UpArrowImage Height + nSpot);
if (nNewThumbTop < ) moThumbTop = nNewThumbTop = ; else if (nNewThumbTop > nPixelRange) moThumbTop = nNewThumbTop = nPixelRange; else moThumbTop = y (UpArrowImage Height + nSpot);
//figure out value float fPerc = (float)moThumbTop / (float)nPixelRange; float fValue = fPerc * (Maximum LargeChange); moValue = (int)fValue; Debug WriteLine(moValue ToString());
Application DoEvents();
Invalidate();
private void CustomScrollbar_MouseMove(object sender
MouseEventArgs e) if (moThumbDown == true) this moThumbDragging = true;
if (this moThumbDragging)
MoveThumb(e Y);
if (ValueChanged != null) ValueChanged(this new EventArgs());
if (Scroll != null) Scroll(this new EventArgs());
internal class ScrollbarControlDesigner : System Windows Forms Design ControlDesigner
public override SelectionRules SelectionRules get SelectionRules selectionRules = base SelectionRules; PropertyDescriptor propDescriptor = TypeDescriptor GetProperties(this Component)[ AutoSize ]; if (propDescriptor != null) bool autoSize = (bool)propDescriptor GetValue(this Component); if (autoSize) selectionRules = SelectionRules Visible | SelectionRules Moveable | SelectionRules BottomSizeable | SelectionRules TopSizeable; else selectionRules = SelectionRules Visible | SelectionRules AllSizeable | SelectionRules Moveable; return selectionRules; 目前只想简单实现滚动条中上箭头/下箭头/滑道/拖动块的重写 所以以上代码中OnPaint函数里的部分内容被我注释了 好了 这个滚动条控件已经做好了 一个控件而已 你应该会使用它 我就不罗嗦了
接下来就是怎么用它来控制ListBox的内容滚动的问题了 这需要调用API函数来实现 同时又不能设置ListBox无滚动条 因为ListBox没有滚动条也就没有滚动的事件可捕获 那就达不到滚动的效果了
在你的窗体里拖一个listbox控件和一个上边我们制作好的用户控件 分明命名为listBox和customScrollbar
然后往listBox中随便多弄写内容 使之出现滚动条即可 调整customScrollbar 的位置使之覆蓋在listBox的滚动条上 呵呵 这方法不错吧?
然后我们定义一下Win API 代码如下
Codepublic class Win API [StructLayout(LayoutKind Sequential)] 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 = X SIF_PAGE = X SIF_POS = X SIF_RANGE = X SIF_TRACKPOS = X
public static int MakeLong(short lowPart short highPart) return (int)(((ushort)lowPart) | (uint)(highPart << )); public const int SB_THUMBTRACK = ; public const int WM_HSCROLL = x ; public const int WM_VSCROLL = x ; [DllImport( user dll EntryPoint = GetScrollInfo )] public static extern bool GetScrollInfo(IntPtr hwnd int fnBar ref SCROLLINFO lpsi); [DllImport( user dll EntryPoint = SetScrollInfo )] public static extern int SetScrollInfo(IntPtr hwnd int fnBar [In] ref SCROLLINFO lpsi bool fRedraw);
[DllImport( User dll CharSet = CharSet Auto EntryPoint = SendMessage )] static extern IntPtr SendMessage(IntPtr hWnd uint Msg IntPtr wParam IntPtr lParam); [DllImport( user dll SetLastError = true)] public static extern bool PostMessage(IntPtr hWnd uint Msg long wParam int lParam);
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 = x SIF_PAGE = x SIF_POS = x SIF_DISABLENOSCROLL = x SIF_TRACKPOS = x 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 cbSize = (uint)Marshal SizeOf(si);
si fMask = (int)(ScrollInfoMask SIF_DISABLENOSCROLL | ScrollInfoMask SIF_ALL);
Win API GetScrollInfo(listBox Handle (int)ScrollBarDirection SB_VERT ref si);
return si;
//当鼠标滚动时 设置该滚动条
private void SetImageListScroll()
SCROLLINFO info = tvImageListScrollInfo;
if (info nMax > )
int pos = info nPos ;
if (pos >= )
customScrollbar Value = pos;
定义customScrollbar 的滚动事件函数customScrollbar _Scroll 实现在滚动条滚动的同时发消息给listBox使之同步滚动
Codeprivate void customScrollbar _Scroll(object sender EventArgs m) //当滚动条滚动时 通知控件也跟着滚动吧
SCROLLINFO info = tvImageListScrollInfo;
info nPos = customScrollbar Value;
Win API SetScrollInfo(listBox Handle (int)ScrollBarDirection SB_VERT ref info true);
Win API PostMessage(listBox Handle Win API WM_VSCROLL Win API MakeLong((short)Win API SB_THUMBTRACK (short)(info nPos)) );
cha138/Article/program/net/201311/12416相关参考
当我们中使用iframe时正常情况下当出现上下方向的滚动条时横向的滚动条也就出现了 为此我们如果是想控制在横向上不出现滚动条的话可以采取以下解决方案: 在aspx页面中的<><
知识大全 .net 2.0中iframe并且控制滚动条的方法
当我们在net中使用iframe时正常情况下当出现上下方向的滚动条时横向的滚动条也就出现了 为此我们如果是想控制在横向上不出现滚动条的话可以采取以下解决方案: 在aspx页面中的<>
scrollLeft实现按钮控制滚动条相册 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 我想大
用DIV和DIV自身的滚动条相互控制内容的滚动DIV自身的滚动条样式可以用DIV层覆蓋重写滚动条样式 JQuery计算滚动条长度和位置代码如下 javascript复制代码代码如下:&
cha138/Article/program/Java/JSP/201311/20304
cha138/Article/program/Java/Javascript/201311/25350
<head><title>网页特效|wangqi|模拟滚动条</title><metaequiv="ContentType"content="text/;ch
这篇文章介绍了jquery滚动条事件的简单实例有需要的朋友可以参考一下 复制代码代码如下:cha138/Article/program/Java/JSP/201311/20441
知识大全 启动时一次突然停电,导致XP系统不能启动,但滚动条一直滚动,什么模式都进不去,重装系统也一样
启动时一次突然停电,导致XP系统不能启动,但滚动条一直滚动,什么模式都进不去,重装系统也一样 以下文字资料是由(本站网www.cha138.com)小编为大家搜集整理后
知识大全 利用div+jquery自定义滚动条样式的2种方法
可以设置左边菜单项div的overflowx:auto;overlfowy:auto;这样就会自动生成了滚动条但是大家都知道自带的不好看接下来就是重点了如何修改滚动条的样式呢?感兴趣的朋友可以了解下本