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();
            }

完!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM