Mouse wheel event to work with hovered control

前端 未结 4 1143
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-04 11:37

In my C# 3.5 Windows Forms application, I have a few SplitContainers. There is a list control inside each (dock fill). When the focus is on one of these controls and I move mous

4条回答
  •  感动是毒
    2021-02-04 12:20

    I had similar question and found this thread... so posting my belated answer for others who might find this thread. In my case, I just want the mouse wheel events to go to whatever control is under the cursor... just like right-click does (it would be bewildering if right-click went to the focus control rather than the control under the cursor... I argue the same is true for the mouse wheel, except we've gotten used to it).

    Anyway, the answer is super easy. Just add a PreFilterMessage to your application and have it redirect mouse wheel events to the control under the mouse:

        public bool PreFilterMessage(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_MOUSEWHEEL:   // 0x020A
                case WM_MOUSEHWHEEL:  // 0x020E
                    IntPtr hControlUnderMouse = WindowFromPoint(new Point((int)m.LParam));
                    if (hControlUnderMouse == m.HWnd)
                        return false; // already headed for the right control
                    else
                    {
                        // redirect the message to the control under the mouse
                        SendMessage(hControlUnderMouse, m.Msg, m.WParam, m.LParam);
                        return true;
                    } 
                 default: 
                    return false; 
               } 
    }
    

提交回复
热议问题