无法在 C# Windows 窗体中触发 MouseWheel 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1190147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:25:06  来源:igfitidea点击:

Can't fire MouseWheel event in C# Windows Forms

c#winformsvisual-studio-2008mousewheel

提问by ck_

First off, the mousewheel event is not listed in Visual Studio 2008's events pane which is very annoying.

首先,鼠标滚轮事件未在 Visual Studio 2008 的事件窗格中列出,这很烦人。

I found the correct format online though, and wrote this into my code:

不过我在网上找到了正确的格式,并将其写入我的代码中:

    private void Form1_MouseWheel(object sender, MouseEventArgs e)
    {
        Debug.WriteLine("Foo");
    }

...from which I'm getting no response when the mousewheel is rotated.

...当鼠标滚轮旋转时,我没有收到任何响应。

I'm doing this in my code's main class area, and the designer contains only the one form/window/whatever so the mouse isn't losing focus.

我在代码的主类区域中执行此操作,并且设计器仅包含一个表单/窗口/任何内容,因此鼠标不会失去焦点。

namespace BlahBlah
{
    public partial class Form1 : Form
    {

And by contrast, I have this method right above the mousewheel one and it works fine:

相比之下,我在鼠标滚轮正上方有这个方法,它工作正常:

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        Debug.WriteLine("Foo");
    }

If I had to guess, I'm thinking I'm not correctly linking the code to the form (aka: all the stuff that visual studio would do for me if I added this event through the designer's events panel). But I could be wrong or just be making some silly error.

如果我不得不猜测,我认为我没有正确地将代码链接到表单(也就是:如果我通过设计器的事件面板添加此事件,visual studio 将为我做的所有事情)。但我可能是错的,或者只是犯了一些愚蠢的错误。

Can you help me get ANY kind of response when the mouse wheel is rotated? Thanks!

当鼠标滚轮旋转时,你能帮我得到任何类型的响应吗?谢谢!

采纳答案by Kurisu

The mousewheel event needs to be set up.

需要设置鼠标滚轮事件。

Add this to Form1's constructor (After InitalizeComponents();)

将此添加到 Form1 的构造函数(在 InitalizeComponents(); 之后)

this.MouseWheel+= new MouseEventHandler(Form1_MouseWheel);

回答by Mark Simpson

I don't know how to subscribe to that particular event but you can override the appropriate method on the form, instead.

我不知道如何订阅该特定事件,但您可以改写表单上的相应方法。

回答by marco0009

I don't have enough reputation to respond with a comment, but the answer to your side question is that the delegates do need to be setup. However when you create one by double clicking it in the properties pane the code is automatically generated and placed in the *.designer.cs file in the InitializeComponent method.

我没有足够的声誉来回复评论,但是您的附带问题的答案是确实需要设置代表。但是,当您通过在属性窗格中双击来创建一个时,代码会自动生成并放置在 InitializeComponent 方法中的 *.designer.cs 文件中。

回答by Limited Atonement

It's best in this case to override the OnMouseWheelmember function rather than register to the event. For example:

在这种情况下最好覆盖OnMouseWheel成员函数而不是注册到事件。例如:

public class MyFrm : Form
{
    protected override void OnMouseWheel(MouseEventArgs e)
    {
        /*Handle the mouse wheel here*/
        base.OnMouseWheel(e);
    }
}