C# ListView マウス ホイール スクロール (フォーカスなし)
質問
複数の列を表示できるように ListView を詳細に設定して WinForms アプリを作成しています。
マウスがコントロール上にあり、ユーザーがマウス スクロール ホイールを使用したときに、このリストがスクロールするようにしたいと考えています。現時点では、ListView にフォーカスがある場合にのみスクロールが行われます。
ListView にフォーカスがない場合でも、ListView をスクロールさせるにはどうすればよいですか?
解決
通常、ウィンドウまたはコントロールにマウス/キーボード イベントを取得できるのは、フォーカスがある場合のみです。フォーカスなしでそれらを表示したい場合は、下位レベルのフックを設置する必要があります。
他のヒント
「シンプル」で実用的なソリューション:
public class FormContainingListView : Form, IMessageFilter
{
public FormContainingListView()
{
// ...
Application.AddMessageFilter(this);
}
#region mouse wheel without focus
// 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 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 && System.Windows.Forms.Control.FromHandle(hWnd) != null)
{
SendMessage(hWnd, m.Msg, m.WParam, m.LParam);
return true;
}
}
return false;
}
#endregion
}
所属していません StackOverflow