有没有办法记录Win Forms应用程序中的所有点击?我想拦截点击并记录操作和导致它的控件的名称.
这可能吗?
提前致谢.
更新:我正在寻找一个应用程序范围的解决方案,是否没有办法向Windows事件队列添加一个监听器(或者它被称为什么)?
您可以通过让应用程序的主窗体实现IMessageFilter接口来完成此操作.您可以筛选它获取的Window消息并查找点击次数.例如:
public partial class Form1 : Form, IMessageFilter {
public Form1() {
InitializeComponent();
Application.AddMessageFilter(this);
this.FormClosed += (o, e) => Application.RemoveMessageFilter(this);
}
public bool PreFilterMessage(ref Message m) {
if (m.Msg == 0x201 || m.Msg == 0x203) { // Trap left click + double-click
string name = "Unknown";
Control ctl = Control.FromHandle(m.HWnd);
if (ctl != null) name = ctl.Name;
Point pos = new Point(m.LParam.ToInt32());
Console.WriteLine("Click {0} at {1}", name, pos);
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这会在应用的任何窗口中记录所有点击.