首页 > 编程 > C# > 正文

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

2020-01-24 03:31:43
字体:
来源:转载
供稿:网友

方法如下:
在窗体的Load事件注册滚动事件,并增加对应的方法

复制代码 代码如下:

private void FormSample_Load(object sender, EventArgs e)
        {

            //注册事件
            this.MouseWheel += new MouseEventHandler(FormSample_MouseWheel);
        }

        /// <summary>
        /// 滚动方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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(
, pnlDownload.VerticalScroll.Value - e.Delta);
            }
        }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表