由于在WinForm中Panel不能直接响应鼠标的滚动事件,只好采用捕获窗体的滚动事件。
方法如下:
在窗体的Load事件注册滚动事件,并增加对应的方法
private void FormSample_Load(object sender, EventArgs e){//注册事件this.MouseWheel += new MouseEventHandler(FormSample_MouseWheel);}/// /// 滚动方法/// /// /// void FormSample_MouseWheel(object sender, MouseEventArgs e){//获取光标位置Point mousePoint = new Point(e.X,e.Y);//换算成相对本窗体的位置mousePoint.Offset(this.Location.X, this.Location.Y); //判断是否在panel内if (pnlDownload.RectangleToScreen(pnlDownload.DisplayRectangle).Contains(mousePoint)){//滚动pnlDownload.AutoScrollPosition = new Point(0, pnlDownload.VerticalScroll.Value - e.Delta);}}
(摘自互联网,经过使用,确实可行) (完)