gpt4 book ai didi

winforms - Winforms,记录所有点击?

转载 作者:行者123 更新时间:2023-12-02 22:16:53 30 4
gpt4 key购买 nike

有没有办法记录 Win Forms 应用程序中的所有点击?我想拦截点击并记录该操作以及导致该操作的控件的名称。

这可能吗?

提前致谢。

更新:我正在寻找一个应用程序范围的解决方案,是否没有办法将监听器添加到 Windows 事件队列(或它的名称)?

最佳答案

您可以通过让应用程序的主窗体实现 IMessageFilter 接口(interface)来实现此目的。您可以筛选它收到的 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;
}
}

请注意,这会记录应用程序任何窗口中的所有点击。

关于winforms - Winforms,记录所有点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2584105/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com