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

发布于 2024-07-29 10:53:02 字数 734 浏览 7 评论 0原文

首先,鼠标滚轮事件没有列在 Visual Studio 2008 的事件窗格中,这非常烦人。

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

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

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

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

namespace BlahBlah
{
    public partial class Form1 : Form
    {

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

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

如果我不得不猜测,我想我没有正确地将代码链接到表单(又名:Visual Studio 会做的所有事情对我来说,如果我通过设计器的事件面板添加此事件)。 但我可能是错的,或者只是犯了一些愚蠢的错误。

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

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

垂暮老矣 2024-08-05 10:53:02

需要设置鼠标滚轮事件。

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

this.MouseWheel+= new MouseEventHandler(Form1_MouseWheel);

The mousewheel event needs to be set up.

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

this.MouseWheel+= new MouseEventHandler(Form1_MouseWheel);
小清晰的声音 2024-08-05 10:53:02

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

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.

ゞ记忆︶ㄣ 2024-08-05 10:53:02

在这种情况下,最好重写 OnMouseWheel 成员函数,而不是注册该事件。 例如:

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

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

public class MyFrm : Form
{
    protected override void OnMouseWheel(MouseEventArgs e)
    {
        /*Handle the mouse wheel here*/
        base.OnMouseWheel(e);
    }
}
月亮是我掰弯的 2024-08-05 10:53:02

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文