码迷,mamicode.com
首页 > Web开发 > 详细

[.NET]让Panel对Mouse滚轮事件(Wheel)有感觉,而且能触发Scroll事件

时间:2019-09-16 14:16:31      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:runtime   int   eve   rac   vertica   als   eth   from   hwnd   

之前有写了一篇“让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事件

标签:runtime   int   eve   rac   vertica   als   eth   from   hwnd   

原文地址:https://www.cnblogs.com/petewell/p/11526647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!