我无法在主窗体中获取鼠标滚轮事件。
作为演示,我提出了一个简单的例子:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
this.panel1.MouseMove += new MouseEventHandler(panel1_MouseWheel);
Form2 f2 = new Form2();
f2.Show(this);
}
private void panel1_MouseWheel(object sender, MouseEventArgs e)
{
if(e.Delta != 0)
Console.Out.WriteLine(e.Delta);
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.MouseMove += new MouseEventHandler(Form2_MouseMove);
this.MouseWheel += new MouseEventHandler(Form2_MouseMove);
}
private void Form2_MouseMove(object sender, MouseEventArgs e)
{
if(e.Delta != 0)
Console.Out.WriteLine(e.Delta);
}
}
我在Form2中得到了鼠标滚轮事件但没有Form1任何想法?
干杯,
詹姆斯
答案 0 :(得分:38)
我怀疑只要鼠标悬停在面板上,即使面板没有焦点,OP也希望获得滚动事件。
此处解释了实现此行为的方法:
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/eb922ed2-1036-41ca-bd15-49daed7b637c/
在这里:
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/6bfb9287-986d-4c60-bbcc-23486e239384/
从链接论坛获取的代码片段之一:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1 {
public partial class Form1 : Form, IMessageFilter {
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) {
SendMessage(hWnd, m.Msg, m.WParam, m.LParam);
return true;
}
}
return false;
}
// 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);
}
}
此代码将基本拦截所有wm_mousewheel事件,并将它们重定向到鼠标当前悬停的控件。面板不再需要具有焦点来接收车轮事件。
答案 1 :(得分:16)
你的问题来自于form1具有焦点,而不是panel1。 ...哪个方面意味着将触发form1的事件,而不是panel1的事件。
我使用对Form1中构造函数的以下更改重新创建了您的场景,并验证它是否会触发滚轮事件。
public Form1()
{
InitializeComponent();
/* --- Old code that don't work ---
this.panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
this.panel1.MouseMove += new MouseEventHandler(panel1_MouseWheel);
*/
this.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
this.MouseMove += new MouseEventHandler(panel1_MouseWheel);
Form2 f2 = new Form2();
f2.Show(this);
}
}
答案 2 :(得分:12)
添加面板MouseEnter
的另一个事件,并在其回调函数中获取输入焦点:
void MouseEnterEvent()
{
this.Panel.Focus();
}
答案 3 :(得分:3)
感谢@nitrogenycs的回答,我写了一个简单的通用类来轻松解决这个问题:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
namespace MyNamespace
{
public class MouseWheelManagedForm : Form, IMessageFilter
{
private bool managed;
public MouseWheelManagedForm () : this (true) {
}
public MouseWheelManagedForm (bool start) {
managed = false;
if (start)
ManagedMouseWheelStart();
}
protected override void Dispose (bool disposing) {
if (disposing)
ManagedMouseWheelStop();
base.Dispose(disposing);
}
/************************************
* IMessageFilter implementation
* *********************************/
private const int WM_MOUSEWHEEL = 0x20a;
// 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);
private bool IsChild (Control ctrl) {
Control loopCtrl = ctrl;
while (loopCtrl != null && loopCtrl != this)
loopCtrl = loopCtrl.Parent;
return (loopCtrl == this);
}
public bool PreFilterMessage (ref Message m) {
if (m.Msg == WM_MOUSEWHEEL) {
//Ensure the message was sent to a child of the current form
if (IsChild(Control.FromHandle(m.HWnd))) {
// Find the control at screen position m.LParam
Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
//Ensure control under the mouse is valid and is not the target control
//otherwise we'd be trap in a loop.
IntPtr hWnd = WindowFromPoint(pos);
if (hWnd != IntPtr.Zero && hWnd != m.HWnd && Control.FromHandle(hWnd) != null) {
SendMessage(hWnd, m.Msg, m.WParam, m.LParam);
return true;
}
}
}
return false;
}
/****************************************
* MouseWheelManagedForm specific methods
* **************************************/
public void ManagedMouseWheelStart () {
if (!managed) {
managed = true;
Application.AddMessageFilter(this);
}
}
public void ManagedMouseWheelStop () {
if (managed) {
managed = false;
Application.RemoveMessageFilter(this);
}
}
}
}
从那里,你只需要从这个类继承你的Form而不是Form,因为你需要MouseWheel管理的每个表单":
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace MyApp
{
public partial class MyForm : MyNamespace.MouseWheelManagedForm
{
public MyForm ()
{
InitializeComponent();
}
}
}
希望这会帮助别人(比我)。
答案 4 :(得分:0)
也许这对你有用吗?
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
Form2 f2 = new Form2();
f2.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
f2.MouseMove += new MouseEventHandler(panel1_MouseWheel);
f2.Show(this);
}
private void panel1_MouseWheel(object sender, MouseEventArgs e)
{
if(e.Delta != 0) Console.Out.WriteLine(e.Delta);
}
}
答案 5 :(得分:0)
我认为你有点误解了我的问题。在我的主要表单上使用以下代码我不会收到MouseWheel事件:
public Form1()
{
InitializeComponent();
this.panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
this.panel1.MouseMove += new MouseEventHandler(panel1_MouseWheel);
}
但我确实得到了这个事件:
public Form1()
{
InitializeComponent();
this.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
}
我希望在没有Form2混淆的情况下更清楚。我只是试图以我的主要形式在Panel上获取MouseWheel事件。
干杯,
詹姆斯
答案 6 :(得分:0)
面板本身不具有焦点,只有放置在面板内的项目才能具有焦点。只有在内部放置了一些东西并且该东西具有焦点时,该面板才会收到MouseWheel事件。只需在面板上方移动并移动鼠标滚轮,即可将事件发送到表单,而不是面板。
这是两个例子之间的区别。
答案 7 :(得分:-1)
this.MouseWheel += pictureBox1_MouseWheel; //tanımlama
void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
if (Convert.ToString(e.Delta) == "120")
{
//yukarı
}
else if (Convert.ToString(e.Delta) == "-120")
{
//aşağı
}
}