flowLayoutPanel1设置内容随着鼠标滚动而滚动

当flowLayoutPanel1内容过多时,可以设置竖条,当时当鼠标滚动时,里面的内容不会随着鼠标的滚动而滚动,这就要求我们自己写事件了:

宗旨:判断鼠标是不是在flowLayoutPanel1区域内,如果在,设置flowLayoutPanel1的垂直滚动距离

给winform窗体加一个mousewheel监听事件

核心代码:

 private void Form1_MouseWheel(object sender, MouseEventArgs e)
        {
            //e.X e.Y以窗体左上角为原点,aPoint为鼠标滚动时的坐标
            Point aPoint = new Point(e.X,e.Y);

            //this.Location.X,this.Location.Y为窗体左上角相对于screen的坐标,得出的结果是鼠标相对于电脑screen的坐标
            aPoint.Offset(this.Location.X,this.Location.Y);
       
            Rectangle r = new Rectangle(flowLayoutPanel1.Location.X, flowLayoutPanel1.Location.Y, flowLayoutPanel1.Width, flowLayoutPanel1.Height);
            // MessageBox.Show(flowLayoutPanel1.Width+"  "+flowLayoutPanel1.Height);

            //判断鼠标是不是在flowLayoutPanel1区域内
            if (RectangleToScreen(r).Contains(aPoint))
            {
                //设置鼠标滚动幅度的大小
                flowLayoutPanel1.AutoScrollPosition = new Point(0,flowLayoutPanel1.VerticalScroll.Value-e.Delta/2);
            }

        }

 

往flowLayoutPannel1里面添加controls

  for (int i = 0; i < 100; i++)
            {
                Label l = new Label();
                l.Text = i.ToString();
                flowLayoutPanel1.Controls.Add(l);
                flowLayoutPanel1.ScrollControlIntoView(l);
                Application.DoEvents();
            }

完!!

 

转载于:https://www.cnblogs.com/wwz-wwz/p/6770035.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值