C/C++ Windows API——获取鼠标位置的窗口句柄

举报
福州司马懿 发表于 2021/11/19 02:23:55 2021/11/19
【摘要】 // SpyDemo.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <Windows.h> #define MAX_TEXT_L...
// SpyDemo.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Windows.h>

#define MAX_TEXT_LEN 255

BOOL CALLBACK EnumChildProcess(HWND hwnd, LPARAM lParam)
{
    if (hwnd == NULL) {
        return FALSE;
    }
    BOOL ret;
    RECT rect;
    ret = GetWindowRect(hwnd, &rect);
    if (!ret) {
        printf("GetWindowRect hwnd=%p -> fail(%ld)\n", hwnd, GetLastError());
    }
    else {
        //printf("GetWindowRect hwnd = %p -> rect=(left=%ld, top=%ld, right=%ld, bottom=%ld)\n", hwnd, rect.left, rect.top, rect.right, rect.bottom);
        ret = PtInRect(&rect, *(POINT *)lParam);
        if (ret) {
            printf("GetWindowRect hwnd = %p -> rect=(left=%ld, top=%ld, right=%ld, bottom=%ld)\n", hwnd, rect.left, rect.top, rect.right, rect.bottom);
            //printf("PtInRect\n");

            /*
            WINUSERAPI int WINAPI GetWindowText(
            _In_ HWND hWnd,
            _Out_writes_(nMaxCount) LPTSTR lpString,    //可能是标题名或者file:///打头的文件完整路径
            _In_ int nMaxCount
            );
            如果函数成功,返回值是拷贝的字符串的字符个数,不包括中断的空字符;如果窗口无标题栏或文本,或标题栏为空,或窗口或控制的句柄无效,则返回值为零。若想获得更多错误信息,请调用GetLastError函数。
            */
            TCHAR windowText[MAX_TEXT_LEN];
            int lenRet = GetWindowText(hwnd, windowText, MAX_TEXT_LEN);
            if (lenRet == 0 && GetLastError() != 0) {
                //GetLastError()〖0〗-操作成功完成
                printf("GetWindowText hwnd=%p -> fail(%ld)\n", hwnd, GetLastError());
            }
            else {
                _tprintf(_T("GetWindowText hwnd=%p -> windowText=%s, lenRet=%d\n"), hwnd, windowText, lenRet);
            }

            /*
            WINUSERAPI int WINAPI GetClassNameW(
                _In_ HWND hWnd,
                _Out_writes_to_(nMaxCount, return) LPWSTR lpClassName,
                _In_ int nMaxCount
            );

            如果函数成功,返回值为拷贝到指定缓冲区的字符个数:如果函数失败,返回值为0。若想获得更多错误信息,请调用GetLastError函数。
            */
            TCHAR className[MAX_TEXT_LEN];
            lenRet = GetClassName(hwnd, className, MAX_TEXT_LEN);
            if (lenRet == 0) {
                printf("GetClassName hwnd=%p -> fail(%ld)\n", hwnd, GetLastError());
            }
            else {
                _tprintf(_T("GetClassName hwnd=%p -> className=%s, lenRet=%d\n"), hwnd, className, lenRet);
            }

            /*
            找出某个窗口的创建者(线程或进程),返回创建者的标志符
            哪个线程创建了这个窗口,返回的就是这个线程的id号 (进程只有一个线程的话,那么线程标志符与进程标志符就是指同一个标志符)
            WINUSERAPI DWORD WINAPI GetWindowThreadProcessId(
                _In_ HWND hWnd,
                _Out_opt_ LPDWORD lpdwProcessId //进程号的存放地址(变量地址)
            );
            返回线程号
            */
            DWORD dwProcessId;
            DWORD dwThreadId = GetWindowThreadProcessId(hwnd, &dwProcessId);
            printf("GetWindowThreadProcessId hwnd=%p -> processId=%ld, threadId=%ld\n", hwnd, dwProcessId, dwThreadId);

            /*
            WINUSERAPI UINT WINAPI GetWindowModuleFileName(
                _In_ HWND hwnd,
                _Out_writes_to_(cchFileNameMax, return) LPTSTR pszFileName, //模块完整路径
                _In_ UINT cchFileNameMax
            );
            返回值是复制到缓冲区的字符总数。
            */
            TCHAR fileName[MAX_PATH];
            lenRet = GetWindowModuleFileName(hwnd, fileName, MAX_PATH);
            if (lenRet == 0) {
                //错误码〖126〗-找不到指定的模块。
                printf("GetWindowModuleFileName hwnd=%p -> fail(%ld)\n", hwnd, GetLastError());
            } else {
                _tprintf(_T("GetWindowModuleFileName hwnd=%p -> fileName=%s\n"), hwnd, fileName);
            }

            /*
            WINUSERAPI BOOL WINAPI GetWindowInfo(
                _In_ HWND hwnd,
                _Inout_ PWINDOWINFO pwi
            );

            typedef struct tagWINDOWINFO
            {
                DWORD cbSize;
                RECT rcWindow;
                RECT rcClient;
                DWORD dwStyle;
                DWORD dwExStyle;
                DWORD dwWindowStatus;
                UINT cxWindowBorders;
                UINT cyWindowBorders;
                ATOM atomWindowType;
                WORD wCreatorVersion;
            } WINDOWINFO, *PWINDOWINFO, *LPWINDOWINFO;
            */
            WINDOWINFO windowInfo;
            windowInfo.cbSize = sizeof(WINDOWINFO);
            ret = GetWindowInfo(hwnd, &windowInfo);
            if (!ret) {
                printf("GetWindowInfo hwnd=%p -> fail(%ld)\n", hwnd, GetLastError());
            }
            else {
                printf("GetWindowInfo hwnd=%p -> dwStyle=%ld, dwExStyle=%ld, dwWindowStatus=%ld, cxWindowBorders=%d, cyWindowBorders=%d, wCreatorVersion=%d\n", hwnd, windowInfo.dwStyle, windowInfo.dwExStyle, windowInfo.dwWindowStatus, windowInfo.cxWindowBorders, windowInfo.cyWindowBorders, windowInfo.wCreatorVersion);
            }
            printf("\n");
        }
    }

    return TRUE;
}

int main()
{
    BOOL ret;
    while (true) {
        /*
        typedef struct tagPOINT
        {
            LONG  x;
            LONG  y;
        } POINT, *PPOINT, NEAR *NPPOINT, FAR *LPPOINT;
        */
        POINT point;
        ret = GetCursorPos(&point);
        if (!ret) {
            printf("GetCursorPos -> fail(%ld)\n", GetLastError());
        }
        else {
            printf("GetCursorPos -> (%ld, %ld)\n", point.x, point.y);

            //获取桌面句柄
            HWND desktopHwnd = GetDesktopWindow();

            /*
            BOOL EnumChildWindows(
                HWND hWndParent,         // handle to parent window // 父窗口句柄
                WNDENUMPROC lpEnumFunc,  // callback function // 回调函数的地址
                LPARAM lParam            // application-defined value // 你自已定义的参数
            );
            直到调用到最个一个子窗口被枚举或回调函数返回一个false,否则将一直自动枚举下去。
            */
            ret = EnumChildWindows(desktopHwnd, EnumChildProcess, (LPARAM)&point);
        }

        /*
        WINBASEAPI VOID WINAPI Sleep(
            _In_ DWORD dwMilliseconds
        );
        Sleep会将线程挂起,把CPU让给其它线程,单位是毫秒
        */
        Sleep(20000);
    }

    system("pause");
    return 0;
}
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173

这里写图片描述

在评论里有人说道,可以用“WindowFromPoint”来实现这个功能,于是就有了下列代码。

// SpyDemo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>

int main()
{
    BOOL ret;
    UINT count;
    /*
    typedef struct tagPOINT
    {
        LONG  x;
        LONG  y;
    } POINT, *PPOINT, NEAR *NPPOINT, FAR *LPPOINT;
    */
    POINT point;
    TCHAR pszFileName[MAX_PATH];
    TCHAR lpClassName[MAX_PATH];
    TCHAR windowText[MAX_PATH];
    while(true) {
        ret = GetCursorPos(&point);
        if (!ret) {
            printf("GetCursorPos -> fail(%ld)\n", GetLastError());
        } else {
            printf("GetCursorPos -> (%ld, %ld)\n", point.x, point.y);
            HWND hwnd = WindowFromPoint(point);
            if (hwnd == NULL || hwnd == INVALID_HANDLE_VALUE) {
                printf("WindowFromPoint point=(%ld, %ld) -> hwnd=%p -> fail(%ld)\n", point.x, point.y, hwnd, GetLastError());
            }
            else {
                printf("WindowFromPoint -> hwnd=%p\n", hwnd);

                /*
                检索与指定的窗口句柄关联的模块的完整路径和文件名称
                WINUSERAPI UINT WINAPI GetWindowModuleFileNameW(
                    _In_ HWND hwnd,
                    _Out_writes_to_(cchFileNameMax, return) LPWSTR pszFileName,
                    _In_ UINT cchFileNameMax
                );
                返回值是复制到缓冲区的字符总数。
                */
                count = GetWindowModuleFileName(hwnd, pszFileName, MAX_PATH);
                _tprintf(_T("GetWindowModuleFileName hwnd=%p -> count=%d, fileName=%s\n"), hwnd, count, pszFileName);

                /*
                WINUSERAPI int WINAPI GetClassName(
                    _In_ HWND hWnd,
                    _Out_writes_to_(nMaxCount, return) LPTSTR lpClassName,
                    _In_ int nMaxCount
                );
                */
                count = GetClassName(hwnd, lpClassName, MAX_PATH);
                _tprintf(_T("GetClassName hwnd=%p -> count=%d, lpClassName=%s\n"), hwnd, count, lpClassName);


                count = GetWindowText(hwnd, windowText, MAX_PATH);
                _tprintf(_T("GetWindowText hwnd=%p -> count=%d, lpClassName=%s\n"), hwnd, count, windowText);
            }

            //Screen(屏幕坐标)到Client(客户区坐标)的转换。
            ret = ScreenToClient(hwnd, &point);
            if (!ret) {
                printf("ScreenToClient hwnd=%p -> fail(%ld)\n", hwnd, GetLastError());
            }
            else {
                printf("ScreenToClient hwnd=%p -> point=(%ld, %ld)\n", hwnd, point.x, point.y);

                //返回父窗口中包含了指定点的第一个子窗口的句柄
                HWND childHwnd = ChildWindowFromPoint(hwnd, point);
                printf("ChildWindowFromPoint hwnd=%p, point=(%ld, %ld) -> childHwnd=%p\n", hwnd, point.x, point.y, childHwnd);

                count = GetClassName(hwnd, lpClassName, MAX_PATH);
                _tprintf(_T("GetClassName hwnd=%p -> count=%d, lpClassName=%s\n"), hwnd, count, lpClassName);

                count = GetWindowText(hwnd, windowText, MAX_PATH);
                _tprintf(_T("GetWindowText hwnd=%p -> count=%d, lpClassName=%s\n"), hwnd, count, windowText);
            }
        }
        Sleep(5000);
    }
    return 0;
}
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84

这里写图片描述


但是有一个问题:
  为什么 GetWindowModuleFileName 虽然句柄每次都不一样,但是获取到的模块文件路径每次都是一样的当前的路径。
  希望有大神能帮我解答一下,感激不尽。

文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/chy555chy/article/details/52900785

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200