[.NET]让Panel对Mouse滚轮事件(Wheel)有感觉,而且能触发Scroll事件
Posted petewell
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[.NET]让Panel对Mouse滚轮事件(Wheel)有感觉,而且能触发Scroll事件相关的知识,希望对你有一定的参考价值。
之前有写了一篇“让Panel对Mouse滚轮事件(Wheel)有感觉”,是透过 SendMessage 的方式去叫 Panel Scroll。
但是却不会触发Panel的Scroll事件。那怎么办呢?
在之前有写了一篇“让Panel对Mouse滚轮事件(Wheel)有感觉”,是透过 SendMessage 的方式去叫 Panel Scroll。
但是却不会触发Panel的Scroll事件。那怎么办呢?
所以我们可以透过Control.FromHandle来取到那个控件,然后再触发该控件的Scroll事件,如下,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
public partial class Form1 : Form, IMessageFilter
// P/Invoke declarations
[DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(Point pt);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
public Form1()
InitializeComponent();
Application.AddMessageFilter(this);
public bool PreFilterMessage(ref Message m)
if (m.Msg == 0x20a)
// WM_MOUSEWHEEL, find the control at screen position m.LParam
Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
IntPtr hWnd = WindowFromPoint(pos);
if (hWnd != IntPtr.Zero && hWnd != m.HWnd && Control.FromHandle(hWnd) != null)
//取得控件
ScrollableControl ctrl = Control.FromHandle(hWnd) as ScrollableControl;
int orgValue = 0;
if (ctrl != null)
//取得控件垂直ScrollBar的值
orgValue = ctrl.VerticalScroll.Value;
SendMessage(hWnd, m.Msg, m.WParam, m.LParam);
if (ctrl != null)
//取得控件垂直ScrollBar的值
int newValue = ctrl.VerticalScroll.Value;
var se = new ScrollEventArgs(ScrollEventType.ThumbTrack, orgValue, newValue
, ScrollOrientation.VerticalScroll);
FireScrollEvent(ctrl, se);
return true;
return false;
///
/// 触发控件的Scroll事件
///
///
///
private void FireScrollEvent(ScrollableControl sc, ScrollEventArgs e)
MethodInfo onScroll = sc.GetType().GetMethod("OnScroll",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
onScroll.Invoke(sc, new object[] e );
参考数据
让Panel对Mouse滚轮事件(Wheel)有感觉
Control.FromHandle
WinForms: How to programatically fire an event handler?
原文:大专栏 [.NET]让Panel对Mouse滚轮事件(Wheel)有感觉,而且能触发Scroll事件
以上是关于[.NET]让Panel对Mouse滚轮事件(Wheel)有感觉,而且能触发Scroll事件的主要内容,如果未能解决你的问题,请参考以下文章
WH_MOUSE_LL 钩子,避免得到无用的 WM_MOUSEMOVE 事件
将Window Object上的Mouse Wheel事件更改为{passive:false} - Javascript