从另一个窗口上的控件获取文本时出现问题
Problems getting text from control on another window
我正在尝试使用来自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;
}
相关文章:
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- 绘制文本问题
- 用户定义的文本运算符(在原始模式下)存在问题
- 在 cout 中将线条表单文件与自定义文本组合时出现问题
- 在文本中查找半个单词"ABBA"的问题
- 从文件中读取文本时出现编码问题
- QTreeWidgetItem 突出显示文本问题
- 在 Qt C++ 中在自定义项委托上绘制文本时的性能问题
- opengl 飞行模拟器应用程序中的文本呈现问题
- 使用 c++ ifstream 读取文本文件问题
- 在C++中使用 Visual Studio 读取文本文件时遇到问题
- 我在编写从文本文件中读取数据并按升序打印的代码时遇到问题
- 使用 substr 提取文本文件时出现问题
- 问题 - 递归函数以返回文本文件排列
- 我在让我的代码输出整个链表时遇到问题.它只是输出我修改过的一些文本文件,而不是整个东西
- 解析基于逗号的文本文件时出现问题 (C++)
- UTF-16LE 编码问题与 Qt 文本编辑器用 C++ 编写
- 从二进制文件中抓取文本时,为什么 xdg_vtnr=8 是我的结果
- SDL2 呈现文本问题
- 从Windows应用程序的文本框中抓取文本