通过鼠标单击在屏幕上的任何位置获取窗口句柄 [英] Getting window handle from mouse click anywhere on screen

查看:575
本文介绍了通过鼠标单击在屏幕上的任何位置获取窗口句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想创建一个程序,该程序在运行时会在用户屏幕上的任何位置跟踪所有用户的鼠标左键点击.

然后,程序将检测鼠标单击是否在窗口内(可以是任何应用程序-IE窗口,AIM窗口等).

如果是这样,我将检查鼠标单击是否在上述窗口中的点P1,P2定义的矩形之间.如果是的话,我将抓住窗口手柄并将窗口移到X,Y位置.仅此.

我知道如果有句柄(使用WinAPI),如何移动/调整窗口大小.

我在编码时遇到的问题是,如何在应用程序外部返回所有鼠标左键单击的坐标?以及如何检查窗口是否位于这些坐标处?

谢谢!

Hello,

I want to create a program that, any time it is running, will track all user left mouse clicks anywhere on the user''s screen.

The program will then detect if the mouse click was inside a window (can be any application - IE window, AIM window, etc.).

If it was, I will check to see if the mouse click was between the rectangle defined by points P1, P2 in the aforementioned window. If it is, I will grab the window handle and move the window to location X, Y. That is all.

I know how to move/resize the window if I have the handle (using WinAPI).

What I am having problems coding is, how to return the coordinates of all mouse left-clicks outside of the application? And how do I check if a window is located at those coordinates?

Thanks!

推荐答案

您可以查看以下链接:
1. 在C#中处理全局鼠标和键盘挂钩 [如何在Visual C#.NET中设置Windows挂钩 [
You can check out these links:
1. Processing Global Mouse and Keyboard Hooks in C#[^]
2. How to set a Windows hook in Visual C# .NET[^]


我从未尝试过您要问的问题,但您应该可以通过在每种表单上覆盖WndProc方法来做到这一点.这是表单处理Windows消息(例如鼠标单击)的地方.请记住,解释消息将需要将其编组到非托管库.从好的方面来说,网络上有很多代码示例和其他资源来帮助您实现此目的.

基本上,您需要检查Message参数以查看正在传递什么消息.如果是鼠标左键按下事件,请调用API以找出光标在窗口中的位置.完成后,请确保调用MyBase.WndProc(m),以便可以正常处理消息.

如果只有几种形式,则可以在每一种形式上覆盖该方法.如果有很多,您可以将实际的处理代码放在全局模块中,并使用覆盖的方法进行调用.或者,使用覆盖创建MyBaseForm对象可能会更容易,并使所有表单都继承自该对象.

让我们知道它是如何工作的,听起来您完成后可能会在这里找到文章材料.

已添加:好的,我重新阅读了您的问题.看起来您想要的是能够捕获应用程序外部发生的鼠标单击的功能.这就是所谓的全局钩子,编写起来很麻烦.环顾代码项目,我确定您会找到几篇文章,这些文章将提供深入的解决方案.
I''ve never tried what you are asking, but you should be able to do this by overriding the WndProc method on each form. This is where the form processes the Windows messages such as mouse clicks. Keep in mind that interpreting the messages will require marshalling to unmanaged libraries. On the upside, there are a lot of code examples and other resources on the web for how to do this.

Basically, you need to check the Message parameter to see what message is being passed. If it is a left mouse down event, call the API to find out the cursor''s location on the window. When you are done, make sure to call MyBase.WndProc(m) so the messages can be processed normally.

If you have only a few forms, you can override the method on each one. If you have a lot, you can put the actual processing code in a global module and have the overridden method call that. Or it may be easier to create a MyBaseForm object with the override, and have all your forms inherit from that.

Let us know how it works out, it sounds like you might have article material here when you are done.

Added: Ok, I reread your question. It looks like what you want is the ability to capture mouse clicks that occur outside of your application. That is called a global hook, which is a lot messier to write. Look around the Code Project and I''m sure you can find several articles that will provide an in-depth solution.


这篇关于通过鼠标单击在屏幕上的任何位置获取窗口句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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