26 c# mousewheel winforms
我无法在主窗体中获取鼠标滚轮事件.
作为演示,我想出了一个简单的例子:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我在Form2中得到了鼠标滚轮事件但没有Form1任何想法?
干杯,
詹姆士
nit*_*ycs 39
我怀疑只要鼠标悬停在面板上,即使面板没有焦点,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);
}
}
Run Code Online (Sandbox Code Playgroud)
此代码将基本拦截所有wm_mousewheel事件,并将它们重定向到鼠标当前悬停的控件.面板不再需要具有焦点来接收车轮事件.
Pre*_*ten 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);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 12
添加另一个面板事件,MouseEnter并在其回调函数中获取输入焦点:
void MouseEnterEvent()
{
this.Panel.Focus();
}
Run Code Online (Sandbox Code Playgroud)
小智 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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
76934 次 |
| 最近记录: |