为DataGridView增加鼠标滚轮功能

#region 鼠标滚动
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "WindowFromPoint")]
        static extern IntPtr WindowFromPoint(Point pt);

        private void dgv_MouseEnter(object sender, EventArgs e)
        {
            this.MouseWheel += dgv_MouseWheel;
        }

        public void dgv_MouseWheel(object sender, MouseEventArgs e)
        {
            Point p = PointToScreen(e.Location);
            if ((WindowFromPoint(p)) == dgv.Handle)//鼠标指针在框内
            {
                if (e.Delta > 0)
                {
                    if (dgv.FirstDisplayedScrollingRowIndex - 5 < 0)
                    {
                        dgv.FirstDisplayedScrollingRowIndex = 0;
                    }
                    else
                    {
                        dgv.FirstDisplayedScrollingRowIndex = dgv.FirstDisplayedScrollingRowIndex - 5;
                    }
                }
                else
                {
                    dgv.FirstDisplayedScrollingRowIndex = dgv.FirstDisplayedScrollingRowIndex + 5;
                }
            }
        } 
        #endregion

  

posted @ 2018-03-14 10:08  会弹猫的吉他  阅读(321)  评论(0编辑  收藏  举报