C#应用程序范围内的鼠标左键单击事件 [英] C# Application-Wide Left Mouse Click Event

查看:72
本文介绍了C#应用程序范围内的鼠标左键单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在窗体的任何位置单击鼠标左键时播放声音,而不必在窗体的每个控件上放置鼠标单击事件.有没有办法做到这一点?

I want to play a sound when the left mouse button is clicked anywhere in my form without having to place Mouse click events on every single control in the form. Is there a way to accomplish this?

推荐答案

您可以在Windows通知通过IMessageFilter接口分派给具有焦点的控件之前对其进行检测.使它看起来像这样:

You can detect the Windows notification before it is dispatched to the control with the focus with the IMessageFilter interface. Make it look similar to this:

public partial class Form1 : Form, IMessageFilter {
    public Form1() {
        InitializeComponent();
        Application.AddMessageFilter(this);
        this.FormClosed += delegate { Application.RemoveMessageFilter(this); };
    }

    public bool PreFilterMessage(ref Message m) {
        // Trap WM_LBUTTONDOWN
        if (m.Msg == 0x201) {
            System.Diagnostics.Debug.WriteLine("BEEP!");
        }
        return false;
    }
}

这适用于您项目中的任何形式,而不仅仅是主要形式.

This works for any form in your project, not just the main one.

这篇关于C#应用程序范围内的鼠标左键单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆