C# ScrollableControl 不接收所有的滚动事件

标签 c# events scrollable

我在使用 ScrollableControl(更准确地说是面板)时遇到了问题。当鼠标指针直接在滚动条上滚动时,会正确触发 Scroll 事件。

但是当使用鼠标滚轮滚动时,Panel 会正确滚动但不会触发 Scroll 事件。

此外,当 Panel 内的越界控件获得焦点时,Panel 会正确滚动以将控件置于 View 中,但同样,在这种情况下,不会触发 Scroll 事件。

你们有遇到过同样的事情吗?您找到解决方案了吗?

谢谢!

最佳答案

更新:我现在已经测试过 - 这不起作用!

我还没有测试过,但是根据这个:http://www.codeproject.com/Articles/7452/ScrollableControl-with-Scroll-Events 一种选择是扩展 ScrollableControl 的功能

代码如下:

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;

namespace Foretel.SelectAGrid
{
    /// <summary>
    /// Adds the missing scroll events to the scrollable control!
    /// Written by Martin Randall - Thursday 17th June, 2004
    /// </summary>
    public class ScrollableControlWithScrollEvents : ScrollableControl
    {
        private const int WM_HSCROLL = 0x114;
        private const int WM_VSCROLL = 0x115;

        /// <summary>
        /// Horizontal scroll position has changed event
        /// </summary>
        public event ScrollEventHandler HorzScrollValueChanged;

        /// <summary>
        /// Vertical scroll position has changed event
        /// </summary>
        public event ScrollEventHandler VertScrollValueChanged;

        /// <summary>
        /// Intercept scroll messages to send notifications
        /// </summary>
        /// <param name="m">Message parameters</param>
        protected override void WndProc(ref Message m)
        {
            // Let the control process the message
            base.WndProc (ref m);

            // Was this a horizontal scroll message?
            if ( m.Msg == WM_HSCROLL ) 
            {
                if ( HorzScrollValueChanged != null ) 
                {
                    uint wParam = (uint)m.WParam.ToInt32();
                    HorzScrollValueChanged( this, 
                        new ScrollEventArgs( 
                            GetEventType( wParam & 0xffff), (int)(wParam >> 16) ) );
                }
            } 
            // or a vertical scroll message?
            else if ( m.Msg == WM_VSCROLL )
            {
                if ( VertScrollValueChanged != null )
                {
                    uint wParam = (uint)m.WParam.ToInt32();
                    VertScrollValueChanged( this, 
                        new ScrollEventArgs( 
                        GetEventType( wParam & 0xffff), (int)(wParam >> 16) ) );
                }
            }
        }

        // Based on SB_* constants
        private static ScrollEventType [] _events =
            new ScrollEventType[] {
                                      ScrollEventType.SmallDecrement,
                                      ScrollEventType.SmallIncrement,
                                      ScrollEventType.LargeDecrement,
                                      ScrollEventType.LargeIncrement,
                                      ScrollEventType.ThumbPosition,
                                      ScrollEventType.ThumbTrack,
                                      ScrollEventType.First,
                                      ScrollEventType.Last,
                                      ScrollEventType.EndScroll
                                  };
        /// <summary>
        /// Decode the type of scroll message
        /// </summary>
        /// <param name="wParam">Lower word of scroll notification</param>
        /// <returns></returns>
        private ScrollEventType GetEventType( uint wParam )
        {
            if ( wParam < _events.Length )
                return _events[wParam];
            else
                return ScrollEventType.EndScroll;
        }
    }
}

关于C# ScrollableControl 不接收所有的滚动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30776147/

相关文章:

ios - 如何在顶部标签栏中设置图像和文本?

java - 创建选项卡时出错

c# - c#中的文件传输问题

带有授权和 JSON 数据的 C# HttpClient 发布 - 401 未经授权

c# JWT 将 ES256 PEM 文件加载到 CngKey (jose-jwt)

javascript - 如何使用 javascript/jquery 检查鼠标是否退出浏览器窗口?

android - 使 CustomView 可滚动

c# - Excel 作为 WPF 中的嵌入框架已禁用 ExcelWorksheet

events - 在 ExtJS 中,如何在组件中注入(inject) enableKeyEvents = true?

c++ - 如何获取进程状态(正在运行、已终止)事件?