c# 通过鼠标获取窗口句柄?

c#通过鼠标点击窗口后获取窗口句柄方法?使用鼠标钩子+WindowFromPoint在64系统上不行,求其它解法?... c# 通过鼠标点击窗口后获取窗口句柄方法?
使用 鼠标钩子+WindowFromPoint 在64 系统上不行,求其它解法?
展开
 我来答
lrh3321
推荐于2018-03-26 · TA获得超过2182个赞
知道大有可为答主
回答量:2296
采纳率:75%
帮助的人:2085万
展开全部
using System;
using System.Runtime.InteropServices;

namespace FrmZhidao
{
    public struct POINT {
        int x;
        int y;
    }
    public static class APIMethod
    {
        [DllImport("user32.dll")]
        static extern IntPtr WindowFromPoint(POINT Point);

        [DllImport("user32.dll")]
        static extern IntPtr WindowFromPoint(int xPoint, int yPoint);

        [DllImport("user32.dll")]
        static extern bool GetCursorPos(out POINT lpPoint);

        [DllImport("user32.dll")]
        static extern bool SetWindowText(IntPtr hWnd, string lpString);

        public static POINT GetCursorPos(){
            POINT p;
            if (GetCursorPos(out p)){
                return p;
            }
            throw new Exception();
        }
        
        public static IntPtr WindowFromPoint(){
            POINT p=GetCursorPos();
            return WindowFromPoint(p);
        }
    }
}

 下面是我去获取Clicker Hero这个游戏的句柄,用print Screen键截的图,所以没鼠标指针。Spy++也在图上,指针是对上的。

main函数,和我的系统信息。 我64位的win10都能做到,就更别说win7了

山水阿锐
推荐于2016-07-02 · TA获得超过34.3万个赞
知道顶级答主
回答量:23.7万
采纳率:91%
帮助的人:3.1亿
展开全部
您好,获取鼠标位置处窗口句柄,需要使用到Win32Api函数WindowFromPoint,用来根据坐标获取窗口句柄,C#引用如下:
[DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄
public static extern int WindowFromPoint(
int xPoint,
int yPoint
);
只要能够获取鼠标的位置,然后调用该函数就可以得到窗口句柄。
2、获取鼠标位置,需要使用鼠标钩子,本文使用已经设计好的鼠标钩子类,关于该类的详细信息见参考资料。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式