【问题标题】:Mouse Tilt horizontal scrolling is not working in DataGridView鼠标倾斜水平滚动在 DataGridView 中不起作用
【发布时间】:2014-05-29 09:55:17
【问题描述】:

我有一个

 System.Windows.Forms.DataGridView 

带有水平滚动条。当我在此 DataGridView 上执行鼠标倾斜时,内容不会水平滚动。但是在

 ListView  

当我执行鼠标倾斜时,内容将水平滚动。那么我应该在 DataGridView 中设置任何其他属性以启用鼠标倾斜吗?还是 DataGridView 中的错误?

【问题讨论】:

  • DataGridView 获取WM_MOUSEHWHEEL(0x020e) 消息。但是在 DataGridView 中没有发生水平滚动。

标签: c# .net winforms datagridview mousewheel


【解决方案1】:

试试这个

 private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
    {
      //get current selected rows
      DataGridViewSelectedRowCollection rc = dataGridView1.SelectedRows;

      if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
      {
        if (e.NewValue > e.OldValue && rc.Count > 0)
        {
          int nextrow = rc[0].Index + 1;
          dataGridView1.Rows[nextrow].Selected = true;
        }        
      }
    }

【讨论】:

  • 在此处倾斜 DGV 上的鼠标滚轮既不会引发 Scroll 也不会引发 MouseWheel 事件 :-(
【解决方案2】:

要实现这个效果,需要创建一个自定义的DataGridView。

    public sealed class MouseTiltableDataGridView : DataGridView
    {
        private const int WM_MOUSEHWHEEL = 0x020E;

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_MOUSEHWHEEL)
            {
                short w = (short)((m.WParam.ToInt32() >> 16) & 0xFFFF);
                if (HorizontalScrollingOffset + w >= 0)
                {
                    HorizontalScrollingOffset += w;
                }
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-08
    • 2021-11-24
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多