C#在win10和非Win10上处理鼠标滚动有一些区别,建一个Form1,放置一个FlowLayoutPanel ,类型的Panel1

Panel.MouseWheel += PanelOnMouseWheel;

private void PanelOnMouseWheel(object sender, MouseEventArgs mouseEventArgs)
{
if (mouseEventArgs.Delta < )
ScrollBar.Value = ScrollBar.Value == ScrollBar.Maximum ? ScrollBar.Maximum : ++ScrollBar.Value;
else
ScrollBar.Value = ScrollBar.Value == ScrollBar.Minimum ? ScrollBar.Minimum : --ScrollBar.Value; }

以上代码在win10上,只要鼠标在Panel1客户区范围内,那么滚动鼠标滚轮时,就能触发滚动事件,但是在非win10上,如果焦点不在Panel上,比如在Form1窗体一个Button上,那么就不能触发滚动事件。

解决办法

        [DllImport("user32.dll")]
public static extern IntPtr GetFocus();

需要判断焦点按钮是否是本窗口子控件,使用IMessageFilter。

const int WM_MOUSEWHEEL = 0x020A;
[DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
public bool PreFilterMessage(ref Message msg)
{
if (msg.Msg == WM_MOUSEWHEEL)
{
IntPtr hWnd = WindowFromPoint(MousePosition.X, MousePosition.Y);
if ((CheckControl(this, hWnd)))
{
int wpara = (int)msg.WParam;
//this.Parent.Parent.Text = "wparam:" + wpara.ToString() + ", msg:" + msg.Msg + ",hwnd:" + msg.HWnd.ToString();
// MessageBox.Show("123");
if ((wpara & 0x80000000) == 0x80000000)//向下
ScrollBar.Value = ScrollBar.Value == ScrollBar.Maximum ? ScrollBar.Maximum : ++ScrollBar.Value;
else
ScrollBar.Value = ScrollBar.Value == ScrollBar.Minimum ? ScrollBar.Minimum : --ScrollBar.Value; }
return false;
}
return false;
}
public bool CheckControl(Control control, IntPtr handle)
{
if (control == null)
return false;
try
{
for (int i = ; i < control.Controls.Count; i++)
{
var v = control.Controls[i];
if (handle == v.Handle)
{
return true;
}
else
{
if (v.Controls.Count > )
{
if (CheckControl(v, handle))
{
return true;
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}
return false;
}

如果有更好的解决办法请指点。

为了提示查找效率,可以使用下面函数

public static bool CheckIsParentControl(Control parentControl, Control childControl)
{
if(childControl==null)return false;
if (childControl.Parent == null)
{
return false; }
if (childControl.Parent == parentControl)
{
return true; }
return CheckIsParentControl(parentControl, childControl.Parent); }

最新文章

  1. 获取 windows 商店内的 aapx 安装包 并 安装(旁加载)
  2. HashMap、HashTable、LinkedHashMap和TreeMap用法和区别
  3. An Introduction to Interactive Programming in Python (Part 1) -- Week 2_1 练习
  4. POJ 3041 匈牙利算法模板题
  5. CLR via C#笔记
  6. ckeditor增加上传图片的功能
  7. 不用char*作为hash_map的key
  8. python3.4 安装 scrapy 报错 VS2010
  9. 整合 ucenter 注册自动激活
  10. REST API设计指导——译自Microsoft REST API Guidelines(一)
  11. php7配置多线程
  12. 使用 SQLServer On Linux 作为开发数据库遇到的编码问题
  13. 使用Builder模式造车
  14. 第二阶段Sprint3
  15. 第184天:js创建对象的几种方式总结
  16. String和stringbuffer和stringbuilder的区别
  17. Rabbit简单队列模式
  18. js+CSS 实现可以编辑的下拉列表框
  19. centos6.5安装Apache+MySQL+PHP
  20. ASIHTTPRequest中文入门教程全集 http://www.zpluz.com/thread-3284-1-1.html

热门文章

  1. MyBatis是如何解决Sql注入的
  2. php常用扩展安装
  3. python学习笔记十——模块与函数
  4. Ideal test 不执行main方法了
  5. js簡介
  6. C# TreeView 连续点击 不触发AfterCheck事件
  7. [转]GDB
  8. hdu 3038(扩展并查集)
  9. Java -- JDBC 学习--PreparedStatement
  10. 【洛谷P1429】平面最近点对