【问题标题】:Getting a windows text in C++?在 C++ 中获取 Windows 文本?
【发布时间】:2016-03-28 17:36:36
【问题描述】:

你好,最近我一直在用 C++ 编程。请注意,我使用 -std=c++11 进行编译,并在 MingW 编译器中使用 Code::Blocks,因为某些程序需要它。

我的问题很简单:谁能给我提供一个函数来获取输入栏的文本?我问这个是因为我似乎找不到 WM_GETTEXT 或 GetWindowText 的工作实现。

编辑:

我有这些错误:

||=== 构建:在 ElitezLua 中发布(编译器:GNU GCC 编译器)===| C:\Users\PC\Desktop\ElitezLua\Main.cpp||在函数 'std::string GetText(HWND, int)':| C:\Users\PC\Desktop\ElitezLua\Main.cpp|35|error: cannot convert 'wchar_t*' to 'LPSTR {aka char*}' for argument '2' to 'int GetWindowTextA(HWND, LPSTR, int) '|
||=== 构建失败:1 个错误,0 个警告(0 分钟,4 秒)===|

使用此代码:

string GetText(HWND Box, int THN) {
    int Length = GetWindowTextLength(GetDlgItem(Box, THN));
    wchar_t * Text = new wchar_t[Length + 1];
    return GetWindowText(GetDlgItem(Box, THN), Text, (Length + 1));
};

(错误在返回线上)

【问题讨论】:

标签: c++ c++11 winapi


【解决方案1】:
string GetText(HWND Box, int THN) 
{
    int Length = GetWindowTextLength(GetDlgItem(Box, THN));
    wchar_t * Text = new wchar_t[Length + 1];
    return GetWindowText(GetDlgItem(Box, THN), Text, (Length + 1));
};

上面的代码有几个错误:

  • 第一,混合 ANSI/Unicode

  • 第二,由于new char_t[] 导致的内存泄漏。

  • 第三个,当需要std::string时返回int

让我们从简单的开始:

wchar_t Text[300] = {0};
GetWindowTextW(GetDlgItem(Box, THN), Text, 300);
MessageBoxW(0, Text, 0, 0);

这应该总是有效的,因为长文本长度小于 300(否则它会被截断)这足以获取按钮名称或从小编辑框中获取文本。

函数末尾的W 强制使用Unicode。或者您可以在 cpp 文件顶部添加#define UNICODE

#define UNICODE
#include <Windows.h>

...
int len = GetWindowTextLen(GetDlgItem(Box, THN));
if (len > 0)
{
    //add 1 for null-terminating char
    len += 1; 
    wchar_t Text *new wchar_t[len];
    GetWindowText(GetDlgItem(Box, THN), Text, len);
    MessageBox(0, Text, 0, 0);
    delete[]Text;
}

接下来,你可以把它放在一个函数中返回std::wstring

std::wstring foo()
{
    std::wstring::str;
    int len = GetWindowTextLen(GetDlgItem(Box, THN));
    if (len > 0)
    {
        len += 1; 
        wchar_t Text *new wchar_t[len];
        GetWindowText(GetDlgItem(Box, THN), Text, len);
        str = text;
        delete[]Text;
    }
    return str;
}

一旦你掌握了这个窍门,你就可以直接使用std::wstring

std::wstring foo()
{
    int len = 1 + GetWindowTextLength(GetDlgItem(Box, THN));

    //initialize a string large enough to read data
    std::wstring str(len, L'\0');
    GetWindowText(GetDlgItem(Box, THN), &str[0], len);
    return str;
}

【讨论】:

    【解决方案2】:

    先获取文本的长度:

    int l = GetWindowTextLength(GetDlgItem(hWnd, ID_OF_YOUR_CONTROL));
    

    现在获取文本(hWnd 是持有控件的窗口的处理程序):

    wchar_t* txt = new wchar_t[l + 1];
    GetWindowText(GetDlgItem(hWnd, ID_OF_YOUR_CONTROL), txt, l + 1);
    

    希望对你有帮助

    编辑 ------

    std::wstring GetText(HWND Box, int THN) {
        int Length = GetWindowTextLength(GetDlgItem(Box, THN));
        wchar_t * Text = new wchar_t[Length + 1];
        GetWindowText(GetDlgItem(Box, THN), Text, (Length + 1));
        return wstring(Text);
    };
    

    【讨论】:

    • string GetText(HWND Box) { int Length = GetWindowTextLength(GetDlgItem(Box, InputBar)); wchar_t * 文本 = 新 wchar_t[长度 + 1]; return GetWindowText(GetDlgItem(Box, InputBar), Text, (Length + 1)); };为什么它不起作用?
    • 你的意思是它不起作用? BoxInputBar 是什么?
    • Box 是从中获取文本的文本框(您需要其中的代码吗?),而 InputBar 是我用来检测它们何时被点击的 (HMENU)Number。
    • 我收到以下错误:||=== Build: Release in ElitezLua(编译器:GNU GCC Compiler)===| C:\Users\PC\Desktop\ElitezLua\Main.cpp||在函数 'std::string GetText(HWND, int)':| C:\Users\PC\Desktop\ElitezLua\Main.cpp|35|error: cannot convert 'wchar_t*' to 'LPSTR {aka char*}' for argument '2' to 'int GetWindowTextA(HWND, LPSTR, int) '| ||=== 构建失败:1 个错误,0 个警告(0 分钟,4 秒)===|在第 35 行(返回线)
    • 请更新您的问题并添加一些代码,以便我可以为您提供更多帮助
    猜你喜欢
    • 2020-12-19
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多