用重绘的滚动条控制ListBox的滚动_第1页
用重绘的滚动条控制ListBox的滚动_第2页
用重绘的滚动条控制ListBox的滚动_第3页
用重绘的滚动条控制ListBox的滚动_第4页
用重绘的滚动条控制ListBox的滚动_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

用重绘的滚动条控制ListBox的滚动查了很多资料,都找不到直接重写ListBox滚动条的方法,只能曲线救国,先自己重绘一个带皮肤的滚动条,然后让它取代ListBox现有的滚动条.老习惯,先传个效果图,你觉得感兴趣就继续看下去,不喜欢的话就此打住,懒得耽误你宝贵的时间,嘿嘿注意,此图中的滚动条宽度明显小于ListBox本身滚动条的宽度,我目前只顾着实现功能了,毕竟,宽度调整相当简单哈。下面简单介绍下重绘系统滚动条的详细步骤:在项目中添加新项--用户控件,我们命名为CustomScrollbar.cs准备几张图片添加进项目资源作为滚动条重绘时要用的背景我用的图片如下:uparrow.png资源名称为uparrow ,滚动条的上箭头ThumbBottom.png资源名称为ThumbBottom,滚动条中间滑道的背景ThumbMiddle.png资源名称为ThumbMiddle,滚动条的中间的拖动块downarrow.png资源名称为downarrow ,滚动条的下箭头3.然后就是利用上面图片做背景重画滚动条背景了,直接给出CustomScrollbar.cs的代码吧CodeusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Data;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Windows.Forms.Design;usingSystem.Diagnostics;namespaceWinamp{[Designer(typeof(ScrollbarControlDesigner))]publicpartialclassCustomScrollbar:UserControl{protectedColormoChannelColor=Color.Empty;protectedImagemoUpArrowImage=null;//上箭头//protectedImagemoUpArrowImage_Over=null;//protectedImagemoUpArrowImage_Down=null;protectedImagemoDownArrowImage=null;//T箭头//protectedImagemoDownArrowImage_Over=null;//protectedImagemoDownArrowImage_Down=null;protectedImagemoThumbArrowImage=null;protectedImagemoThumbTopImage=null;protectedImagemoThumbTopSpanImage=null;protectedImagemoThumbBottomImage=null;protectedImagemoThumbBottomSpanImage=null;protectedImagemoThumbMiddleImage=null;protectedintmoLargeChange=10;protectedintmoSmallChange=1;protectedintmoMinimum=0;protectedintmoMaximum=100;protectedintmoValue=0;privateintnClickPoint;protectedintmoThumbTop=0;protectedboolmoAutoSize=false;privateboolmoThumbDown=false;privateboolmoThumbDragging=false;publicneweventEventHandlerScroll=null;publiceventEventHandlerValueChanged=null;privateintGetThumbHeight(){intnTrackHeight=(this.Height-(UpArrowImage.Height+DownArrowImage.Height));floatfThumbHeight=((float)LargeChange/(float)Maximum)*nTrackHeight;intnThumbHeight=(int)fThumbHeight;if(nThumbHeight>nTrackHeight){nThumbHeight=nTrackHeight;fThumbHeight=nTrackHeight;}if(nThumbHeight<56){nThumbHeight=56;fThumbHeight=56;}returnnThumbHeight;}publicCustomScrollbar(){InitializeComponent();SetStyle(ControlStyles.ResizeRedraw,true);SetStyle(ControlStyles.AllPaintingInWmPaint,true);SetStyle(ControlStyles.DoubleBuffer,true);moChannelColor=Color.FromArgb(51,166,3);UpArrowImage=BASSSkin.uparrow;//上箭头DownArrowImage=BASSSkin.downarrow;//T肩头ThumbBottomImage=BASSSkin.ThumbBottom;ThumbMiddleImage=BASSSkin.ThumbMiddle;this.Width=UpArrowImage.Width;//18pxbase.MinimumSize=newSize(UpArrowImage.Width,UpArrowImage.Height+DownArrowImage.Height+GetThumbHeight());}[EditorBrowsable(EditorBrowsableState.Always),Browsable(true),DefaultValue(false),Category("Behavior"),Description("LargeChange")]publicintLargeChange{get{returnmoLargeChange;}set{moLargeChange=value;Invalidate();}}[EditorBrowsable(EditorBrowsableState.Always),Browsable(true),DefaultValue(false),Category("Behavior"),Description("SmallChange")]publicintSmallChange{get{returnmoSmallChange;}set{moSmallChange=value;Invalidate();}}[EditorBrowsable(EditorBrowsableState.Always),Browsable(true),DefaultValue(false),Category("Behavior"),Description("Minimum")]publicintMinimum{get{returnmoMinimum;}set{moMinimum=value;Invalidate();}}[EditorBrowsable(EditorBrowsableState.Always),Browsable(true),DefaultValue(false),Category("Behavior"),Description("Maximum")]publicintMaximum{get{returnmoMaximum;}set{moMaximum=value;Invalidate();}}[EditorBrowsable(EditorBrowsableState.Always),Browsable(true),DefaultValue(false),Category("Behavior"),Description("Value")]publicintValue{get{returnmoValue;}set{moValue=value;intnTrackHeight=(this.Height-(UpArrowImage.Height+DownArrowImage.Height));floatfThumbHeight=((float)LargeChange/(float)Maximum)*nTrackHeight;intnThumbHeight=(int)fThumbHeight;if(nThumbHeight>nTrackHeight){nThumbHeight=nTrackHeight;fThumbHeight=nTrackHeight;}if(nThumbHeight<56){nThumbHeight=56;fThumbHeight=56;}//figureoutvalueintnPixelRange=nTrackHeight-nThumbHeight;intnRealRange=(Maximum-Minimum)-LargeChange;floatfPerc=0.0f;if(nRealRange!=0){fPerc=(float)moValue/(float)nRealRange;floatfTop=fPerc*nPixelRange;moThumbTop=(int)fTop;Invalidate();}}[EditorBrowsable(EditorBrowsableState.Always),Browsable(true),DefaultValue(false),Category("Skin"),Description("ChannelColor")]publicColorChannelColor{get{returnmoChannelColor;}set{moChannelColor=value;}}[EditorBrowsable(EditorBrowsableState.Always),Browsable(true),DefaultValue(false),Category("Skin"),Description("UpArrowGraphic")]publicImageUpArrowImage{get{returnmoUpArrowImage;}set{moUpArrowImage=value;}}[EditorBrowsable(EditorBrowsableState.Always),Browsable(true),DefaultValue(false),Category("Skin"),Description("UpArrowGraphic")]publicImageDownArrowImage{get{returnmoDownArrowImage;}set{moDownArrowImage=value;}}[EditorBrowsable(EditorBrowsableState.Always),Browsable(true),DefaultValue(false),Category("Skin"),Description("UpArrowGraphic")]publicImageThumbBottomImage{get{returnmoThumbBottomImage;}set{moThumbBottomImage=value;}}[EditorBrowsable(EditorBrowsableState.Always),Browsable(true),DefaultValue(false),Category("Skin"),Description("UpArrowGraphic")]publicImageThumbMiddleImage{get{returnmoThumbMiddleImage;}set{moThumbMiddleImage=value;}}protectedoverridevoidOnPaint(PaintEventArgse){e.Graphics.InterpolationMode =System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;if(UpArrowImage!=null){e.Graphics.DrawImage(UpArrowImage,newRectangle(newPoint(0,0),newSize(this.Width,UpArrowImage.Height)));}BrushoBrush=newSolidBrush(moChannelColor);BrushoWhiteBrush=newSolidBrush(Color.FromArgb(255,255,255));// 函数名:rectangle〃功能:画一个矩形//用法:voidfarrectangle(intleft,inttop,intright,intbottom);//drawchannelleftandrightbordercolorse.Graphics.FillRectangle(oWhiteBrush,newRectangle(0,UpArrowImage.Height,1,(this.Height-DownArrowImage.Height)));e.Graphics.FillRectangle(oWhiteBrush,newRectangle(this.Width- 1,UpArrowImage.Height,1,(this.Height-DownArrowImage.Height)));//drawchannel//e.Graphics.FillRectangle(oBrush,newRectangle(1,UpArrowImage.Height,this.Width-2,(this.Height-DownArrowImage.Height)));e.Graphics.DrawImage(ThumbBottomImage, new Rectangle(0,UpArrowImage.Height,this.Width,(this.Height-DownArrowImage.Height)));//drawthumbintnTrackHeight=(this.Height-(UpArrowImage.Height+DownArrowImage.Height));floatfThumbHeight=((float)LargeChange/(float)Maximum)*nTrackHeight;intnThumbHeight=(int)fThumbHeight;if(nThumbHeight>nTrackHeight){nThumbHeight=nTrackHeight;fThumbHeight=nTrackHeight;}//MessageBox.Show(nThumbHeight.ToString());if(nThumbHeight<56){nThumbHeight=56;fThumbHeight=56;}//Debug.WriteLine(nThumbHeight.ToString());//floatfSpanHeight=(fThumbHeight-(ThumbMiddleImage.Height+ThumbTopImage.Height+ThumbBottomImage.Height))/2.0f;//intnSpanHeight=(int)fSpanHeight;intnTop=moThumbTop;//0nTop+=UpArrowImage.Height;//9px//drawtop画上面的按钮//e.Graphics.DrawImage(ThumbTopImage,newRectangle(0,nTop,this.Width,ThumbTopImage.Height));//nTop+=ThumbTopImage.Height;//10px//drawtopspan//Rectanglerect=newRectangle(1,nTop,this.Width-2,nSpanHeight);//e.Graphics.DrawImage(ThumbTopSpanImage, 1.0f,(float)nTop,(float)this.Width-2.0f,(float)fSpanHeight*2);//nTop+=nSpanHeight;//11px//drawmiddlee.Graphics.DrawImage(ThumbMiddleImage,newRectangle(0,nTop,this.Width,ThumbMiddleImage.Height));//nTop+=ThumbMiddleImage.Height;//drawtopspan//rect=newRectangle(1,nTop,this.Width-2,nSpanHeight*2);//e.Graphics.DrawImage(ThumbBottomSpanImage,rect);//nTop+=nSpanHeight;//drawbottom//e.Graphics.DrawImage(ThumbBottomImage,newRectangle(1,nTop,this.Width-2,nSpanHeight));if(DownArrowImage!=null){e.Graphics.DrawImage(DownArrowImage,newRectangle(newPoint(0,(this.Height-DownArrowImage.Height)),newSize(this.Width,DownArrowImage.Height)));}}publicoverrideboolAutoSize{get{returnbase.AutoSize;}set{base.AutoSize=value;if(base.AutoSize){this.Width=moUpArrowImage.Width;}}}privatevoidInitializeComponent(){this.SuspendLayout();////CustomScrollbar//this.Name="CustomScrollbar";this.MouseDown += newSystem.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseDown);this.MouseMove += newSystem.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseMove);this.MouseUp += newSystem.Windows.Forms.MouseEventHandler(this.CustomScrollbar_MouseUp);this.ResumeLayout(false);privatevoidCustomScrollbar_MouseDown(objectsender,MouseEventArgse){PointptPoint=this.PointToClient(Cursor.Position);intnTrackHeight=(this.Height-(UpArrowImage.Height+DownArrowImage.Height));floatfThumbHeight=((float)LargeChange/(float)Maximum)*nTrackHeight;intnThumbHeight=(int)fThumbHeight;if(nThumbHeight>nTrackHeight){nThumbHeight=nTrackHeight;fThumbHeight=nTrackHeight;}if(nThumbHeight<56){nThumbHeight=56;fThumbHeight=56;}intnTop=moThumbTop;nTop+=UpArrowImage.Height;Rectanglethumbrect=newRectangle(newPoint(1,nTop),newSize(ThumbMiddleImage.Width,nThumbHeight));if(thumbrect.Contains(ptPoint)){//hitthethumbnClickPoint=(ptPoint.Y-nTop);//MessageBox.Show(Convert.ToString((ptPoint.Y-nTop)));this.moThumbDown=true;}Rectangleuparrowrect=newRectangle(newPoint(1,0),newSize(UpArrowImage.Width,UpArrowImage.Height));if(uparrowrect.Contains(ptPoint)){intnRealRange=(Maximum-Minimum)-LargeChange;intnPixelRange=(nTrackHeight-nThumbHeight);if(nRealRange>0){if(nPixelRange>0){if((moThumbTop-SmallChange)<0)moThumbTop=0;elsemoThumbTop-=SmallChange;//figureoutvaluefloatfPerc=(float)moThumbTop/(float)nPixelRange;floatfValue=fPerc*(Maximum-LargeChange);moValue=(int)fValue;Debug.WriteLine(moValue.ToString());if(ValueChanged!=null)ValueChanged(this,newEventArgs());if(Scroll!=null)Scroll(this,newEventArgs());Invalidate();}}}Rectangledownarrowrect=newRectangle(newPoint(1,UpArrowImage.Height+nTrackHeight),newSize(UpArrowImage.Width,UpArrowImage.Height));if(downarrowrect.Contains(ptPoint)){intnRealRange=(Maximum-Minimum)-LargeChange;intnPixelRange=(nTrackHeight-nThumbHeight);if(nRealRange>0){if(nPixelRange>0){if((moThumbTop+SmallChange)>nPixelRange)moThumbTop=nPixelRange;elsemoThumbTop+=SmallChange;//figureoutvaluefloatfPerc=(float)moThumbTop/(float)nPixelRange;floatfValue=fPerc*(Maximum-LargeChange);moValue=(int)fValue;Debug.WriteLine(moValue.ToString());if(ValueChanged!=null)ValueChanged(this,newEventArgs());$$$$$$$$$$$$$$$$4为ListBox控件添加水平滚动条下面我来为这个列表控件来添加水平滚动条,首先需要在资源的设计中为这个列表控件设置水平滚动条,然后就可以通过向列表控件发送一条LB_SETHORIZONTALEXTENT消息来向它添加水平滚动条了。在这条消息的附加参数中,wParam参数就是以像素为单位的水平滚动条长度,IParam不使用。那么,可以为这个滚动条设置一个足够的长度(假设为500),有以下的代码:caseWM_INITDIALOG:(HDChdc;inti;TCHARstr[100];for(i=0;i<100;i++)(wsprintf(str,"Thisisaveryveryveryveryverylongsentence-line%d",i+1);SendDlgItemMessage(hDlg,IDC_LIST,LB_ADDSTRING,0,(LPARAM)str);}SendDlgItemMessage(hDlg,IDC_LIST,LB_SETHORIZONTALEXTENT,500,0);//设置长度为500像素的水平滚动

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论