从另一个窗口上的控件获取文本时出现问题

Problems getting text from control on another window

本文关键字:取文本 问题 获取 控件 另一个 窗口      更新时间:2023-10-16

我正在尝试使用来自win32 api的SendMessage获取记事本窗口文本框上的文本。我首先找到窗口句柄,然后使用SendMessage(hwndEdit,WM_GETTEXT,(WPARAM)bufferSize,(LPARAM)textBuffer)抓取文本。出于某种原因,即使它可以告诉我文本的正确长度,程序也只返回记事本文本的 1 个字符,即使我有 1024 作为我的缓冲区大小,它应该返回。我查看了我找到的例子,我这样做的方式与示例相同。我不知道为什么会发生这种情况,有人可以帮助我或指出我的错误吗?

#include <Windows.h>
#include <iostream>
int main()
{
    printf("finding notepad windown");
    HWND hwndNotepad = FindWindow(NULL, L"Untitled - Notepad");
    if(NULL != hwndNotepad)
    {
        printf("Find edit control windown");
        HWND hwndEdit = FindWindowEx(hwndNotepad, NULL, L"EDIT", NULL);
        if(NULL != hwndEdit)
        {
            printf("- get text lengthn");
            int textLen = (int)SendMessage(hwndEdit, WM_GETTEXTLENGTH, 0, 0);
            printf("textlength: %dn", textLen);
            if(0 < textLen)
            {
                const int bufferSize = 1024;
                char textBuffer[bufferSize] = "";
                SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer);
                printf("getting text:n");
                printf("%sn", textBuffer);
            }
            else
            {
                printf("its emptyn");
            }
        }
        else
        {
            printf("I cant find this controln");
        }
    }
    else
    {
        printf("I cant find notepad window. n");
    }
    return 0;
}

截图:https://i.stack.imgur.com/quv71.png

这可能是由于记事本使用的是UNICODE。试试这个

int copied = SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer);
printf("Copied %d chars.n", copied);

找出您的被叫方认为它复制了多少个字符。请尝试以下操作来打印 UNICODE 文本:

const int bufferSize = 1024;
wchar_t textBuffer[bufferSize] = "";
int copied = SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer);
printf("Copied %d chars.n", copied);
printf("getting text:n");
wprintf(L"%ls n", textBuffer);

如果您使用的是Visual Studio,则可能值得尝试进入项目选项(右键单击项目->配置属性->常规->字符集)并将其设置为ANSI("未设置")。

无需更改项目设置。您可以使用TCHAR而不是使用char

#include <Windows.h>
#include <iostream>
int main()
{
    printf("finding notepad windown");
    HWND hwndNotepad = FindWindow(NULL, L"Untitled - Notepad");
    if(NULL != hwndNotepad)
    {
        printf("Find edit control windown");
        HWND hwndEdit = FindWindowEx(hwndNotepad, NULL, L"EDIT", NULL);
        if(NULL != hwndEdit)
        {
            printf("- get text lengthn");
            int textLen = (int)SendMessage(hwndEdit, WM_GETTEXTLENGTH, 0, 0);
            printf("textlength: %dn", textLen);
            if(0 < textLen)
            {
                const int bufferSize = 1024;
                TCHAR textBuffer[bufferSize];
                SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer);
                printf("getting text:n");
                printf("%sn", textBuffer);
            }
            else
            {
                printf("its emptyn");
            }
        }
        else
        {
            printf("I cant find this controln");
        }
    }
    else
    {
        printf("I cant find notepad window. n");
    }
    return 0;
}