在我的 C# 3.5 Windows 窗体应用程序中,我有几个 SplitContainer。每个内部都有一个列表控件(码头填充)。当焦点位于其中一个控件上并且我移动鼠标滚轮时,现在焦点所在的列表将滚动。
我的任务是滚动当前由鼠标悬停的列表,而不是选中的列表。是否可以在 Windows 窗体中使用?如果没有,是否可以使用 PInvoke?
在我的 C# 3.5 Windows 窗体应用程序中,我有几个 SplitContainer。每个内部都有一个列表控件(码头填充)。当焦点位于其中一个控件上并且我移动鼠标滚轮时,现在焦点所在的列表将滚动。
我的任务是滚动当前由鼠标悬停的列表,而不是选中的列表。是否可以在 Windows 窗体中使用?如果没有,是否可以使用 PInvoke?
看起来您可以使用IMessageFilter
和 PInvoke 来处理这个问题。可以在将鼠标滚轮事件重定向到未聚焦的 Windows 窗体控件中找到 VB 中的示例。您应该能够轻松地将其转换为 C#。
兴趣点
此类对给定任务使用以下技术:
使用VB.NET 到 C# 转换器,您最终会得到以下结果:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class MouseWheelRedirector : IMessageFilter
{
private static MouseWheelRedirector instance = null;
private static bool _active = false;
public static bool Active
{
get { return _active; }
set
{
if (_active != value)
{
_active = value;
if (_active)
{
if (instance == null)
{
instance = new MouseWheelRedirector();
}
Application.AddMessageFilter(instance);
}
else
{
if (instance != null)
{
Application.RemoveMessageFilter(instance);
}
}
}
}
}
public static void Attach(Control control)
{
if (!_active)
Active = true;
control.MouseEnter += instance.ControlMouseEnter;
control.MouseLeave += instance.ControlMouseLeaveOrDisposed;
control.Disposed += instance.ControlMouseLeaveOrDisposed;
}
public static void Detach(Control control)
{
if (instance == null)
return;
control.MouseEnter -= instance.ControlMouseEnter;
control.MouseLeave -= instance.ControlMouseLeaveOrDisposed;
control.Disposed -= instance.ControlMouseLeaveOrDisposed;
if (object.ReferenceEquals(instance.currentControl, control))
instance.currentControl = null;
}
private MouseWheelRedirector()
{
}
private Control currentControl;
private void ControlMouseEnter(object sender, System.EventArgs e)
{
var control = (Control)sender;
if (!control.Focused)
{
currentControl = control;
}
else
{
currentControl = null;
}
}
private void ControlMouseLeaveOrDisposed(object sender, System.EventArgs e)
{
if (object.ReferenceEquals(currentControl, sender))
{
currentControl = null;
}
}
private const int WM_MOUSEWHEEL = 0x20a;
public bool PreFilterMessage(ref System.Windows.Forms.Message m)
{
if (currentControl != null && m.Msg == WM_MOUSEWHEEL)
{
SendMessage(currentControl.Handle, m.Msg, m.WParam, m.LParam);
return true;
}
else
{
return false;
}
}
[DllImport("user32.dll", SetLastError = false)]
private static extern IntPtr SendMessage(
IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
}
我有类似的问题并找到了这个线程......所以将我迟来的答案发布给可能找到这个线程的其他人。就我而言,我只希望鼠标滚轮事件转到光标下的任何控件...就像右键单击一样(如果右键单击转到焦点控件而不是光标下的控件,那将是令人困惑的...我认为鼠标滚轮也是如此,除非我们已经习惯了)。
无论如何,答案非常简单。只需向您的应用程序添加一个 PreFilterMessage 并将鼠标滚轮事件重定向到鼠标下的控件:
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;
}
}
这是Brian Kennedy的回答,并附有Hank Schultz评论:
首先你应该让一个类实现IMessageFilter:
public class MessageFilter : IMessageFilter
{
private const int WM_MOUSEWHEEL = 0x020A;
private const int WM_MOUSEHWHEEL = 0x020E;
[DllImport("user32.dll")]
static extern IntPtr WindowFromPoint(Point p);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
public bool PreFilterMessage(ref Message m)
{
switch (m.Msg)
{
case WM_MOUSEWHEEL:
case WM_MOUSEHWHEEL:
IntPtr hControlUnderMouse = WindowFromPoint(new Point((int)m.LParam));
if (hControlUnderMouse == m.HWnd)
{
//Do nothing because it's already headed for the right control
return false;
}
else
{
//Send the scroll message to the control under the mouse
uint u = Convert.ToUInt32(m.Msg);
SendMessage(hControlUnderMouse, u, m.WParam, m.LParam);
return true;
}
default:
return false;
}
}
}
示例用法:
public partial class MyForm : Form
{
MessageFilter mf = null;
public MyForm
{
Load += MyForm_Load;
FormClosing += MyForm_FormClosing;
}
private void MyForm_Load(object sender, EventArgs e)
{
mf= new MessageFilter();
Application.AddMessageFilter(mf);
}
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
Application.RemoveMessageFilter(mf);
}
}
使用Control.MouseEnter 事件将焦点设置到控件。例如使用ActiveControl 属性