当使用具有此控件的鼠标滚轮时,我们想要覆盖DataGridView的默认行为.默认情况下,DataGridView滚动的行数等于SystemInformation.MouseWheelScrollLines设置.我们想要做的是一次只滚动一个项目.
(我们在DataGridView中显示图像,它们有点大.由于这个滚动三行(典型的系统设置)太多,经常导致用户滚动到他们甚至看不到的项目.)
我已经尝试了几件事,到目前为止还没有取得多大成功.以下是我遇到的一些问题:
您可以订阅MouseWheel事件,但无法将事件标记为已处理并执行自己的操作.
您可以覆盖OnMouseWheel,但似乎从未调用过.
您可能能够在基本滚动代码中更正此问题,但这听起来像一个混乱的工作,因为其他类型的滚动(例如使用键盘)来自同一个管道.
有人有个好主意吗?
这是最终的代码,使用给出的精彩答案:
/// <summary>
/// Handle the mouse wheel manually due to the fact that we display
/// images, which don't work well when you scroll by more than one
/// item at a time.
/// </summary>
///
/// <param name="sender">
/// sender
/// </param>
/// <param name="e">
/// the mouse event
/// </param>
private void mImageDataGrid_MouseWheel(object sender, MouseEventArgs e)
{
// Hack alert! Through reflection, we know that the passed
// in event …
Run Code Online (Sandbox Code Playgroud)